开发者

Android SQLite crashes after trying retrieving data from it

Hey everyone. I'm kinda new to android programming so please bear with me. I'm having some problems with retrieving records from the db. Basically, all I want to do is to store latitudes and longitudes which GPS positioning functions outputs and display them in a list using ListActivity on different tab later on. This is how the code for my DBAdapter helper class looks like:

public class DBAdapter 
{
    public static final String KEY_ROWID = "_id";
    public static final String KEY_LATITUDE = "latitude";
    public static final String KEY_LONGITUDE = "longitude";
    private static final String TAG = "DBAdapter";

    private static final String DATABASE_NAME = "coords";
    private static final String DATABASE_TABLE = "coordsStorage";
    private static final int DATABASE_VERSION = 1;

    private static final String DATABASE_CREATE =
        "create table coordsStorage (_id integer primary key autoincrement, "
        + "latitude integer not null, longitude integer not null);";

    private final Context context; 

    private DatabaseHelper DBHelper;
    private SQLiteDatabase db;

    public DBAdapter(Context ctx) 
    {
        this.context = ctx;
        DBHelper = new DatabaseHelper(context);
    }

    private static class DatabaseHelper extends SQLiteOpenHelper 
    {
        DatabaseHelper(Context context) 
        {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) 
        {
            db.execSQL(DATABASE_CREATE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion,开发者_如何学编程 
        int newVersion) 
        {
            Log.w(TAG, "Upgrading database from version " + oldVersion 
                    + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS titles");
            onCreate(db);
        }
    }    

    //---opens the database---
    public DBAdapter open() throws SQLException 
    {
        db = DBHelper.getWritableDatabase();
        return this;
    }

    //---closes the database---    
    public void close() 
    {
        DBHelper.close();
    }

    //---insert a title into the database---
    public long insertCoords(int latitude, int longitude) 
    {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_LATITUDE, latitude);
        initialValues.put(KEY_LONGITUDE, longitude);
        return db.insert(DATABASE_TABLE, null, initialValues);
    }

    //---deletes a particular title---
    public boolean deleteTitle(long rowId) 
    {
        return db.delete(DATABASE_TABLE, KEY_ROWID + 
                "=" + rowId, null) > 0;
    }

    //---retrieves all the titles---
    public Cursor getAllTitles() 
    {
        return db.query(DATABASE_TABLE, new String[] {
                KEY_ROWID, 
                KEY_LATITUDE,
                KEY_LONGITUDE}, 
                null, 
                null, 
                null, 
                null, 
                null);
    }

    //---retrieves a particular title---
    public Cursor getTitle(long rowId) throws SQLException 
    {
        Cursor mCursor =
                db.query(true, DATABASE_TABLE, new String[] {
                        KEY_ROWID,
                        KEY_LATITUDE, 
                        KEY_LONGITUDE}, 
                        KEY_ROWID + "=" + rowId, 
                        null,
                        null, 
                        null, 
                        null, 
                        null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }

    //---updates a title---
    /*public boolean updateTitle(long rowId, int latitude, 
    int longitude) 
    {
        ContentValues args = new ContentValues();
        args.put(KEY_LATITUDE, latitude);
        args.put(KEY_LONGITUDE, longitude);
        return db.update(DATABASE_TABLE, args, 
                         KEY_ROWID + "=" + rowId, null) > 0;
    }*/
}

Now my question is - am I doing something wrong here? If yes could someone please tell me what? And how I should retrieve the records in organised list manner? Help would be greatly appreciated!!

This are the error it throws:

06-06 14:49:32.434: ERROR/Database(276): Error inserting latitude=0 longitude=0
06-06 14:49:32.434: ERROR/Database(276): android.database.sqlite.SQLiteException: no such table: coordsStorage: , while compiling: INSERT INTO coordsStorage(latitude, longitude) VALUES(?, ?);
06-06 14:49:32.434: ERROR/Database(276):     at android.database.sqlite.SQLiteProgram.native_compile(Native Method)
06-06 14:49:32.434: ERROR/Database(276):     at android.database.sqlite.SQLiteProgram.compile(SQLiteProgram.java:110)
06-06 14:49:32.434: ERROR/Database(276):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:59)
06-06 14:49:32.434: ERROR/Database(276):     at android.database.sqlite.SQLiteStatement.(SQLiteStatement.java:41)
06-06 14:49:32.434: ERROR/Database(276):     at android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:925)
06-06 14:49:32.434: ERROR/Database(276):     at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1300)
06-06 14:49:32.434: ERROR/Database(276):     at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1173)
06-06 14:49:32.434: ERROR/Database(276):     at tabs.app.DBAdapter.insertCoords(DBAdapter.java:81)
06-06 14:49:32.434: ERROR/Database(276):     at tabs.app.Tribocracy$1.onClick(Tribocracy.java:118)
06-06 14:49:32.434: ERROR/Database(276):     at android.view.View.performClick(View.java:2344)

and then when I'm trying to retrieve it it throws this:

06-06 14:49:52.734: INFO/Process(51): Sending signal. PID: 276 SIG: 3
06-06 14:49:52.734: INFO/dalvikvm(276): threadid=7: reacting to signal 3
06-06 14:49:52.734: ERROR/dalvikvm(276): Unable to open stack trace file /data/anr/traces.txt': 
Permission denied


It looks like the table doesn't exist for some reason.

You may want to try increasing your version number, to force an upgrade; if the database itself exists then the onCreate method won't be called - increasing the version will force onUpgrade to be called, and your implementation drops the table and recreates it. This should either fix your error, or give you a new error to work with!


Change your Latitude and Longitude datatypes to double

double Latitude;
double Longitude;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜