tags:

views:

74

answers:

4

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
A: 

Hey Chris sorry I found this error log. 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. (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)

06-06 14:49:32.434: ERROR/Database(276): at android.view.View.onTouchEvent(View.java:4133)

06-06 14:49:32.434: ERROR/Database(276): at android.widget.TextView.onTouchEvent(TextView.java:6510)

06-06 14:49:32.434: ERROR/Database(276): at android.view.View.dispatchTouchEvent(View.java:3672)

06-06 14:49:32.434: ERROR/Database(276): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)

06-06 14:49:32.434: ERROR/Database(276): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)

06-06 14:49:32.434: ERROR/Database(276): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)

06-06 14:49:32.434: ERROR/Database(276): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)

06-06 14:49:32.434: ERROR/Database(276): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)

06-06 14:49:32.434: ERROR/Database(276): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1712)

06-06 14:49:32.434: ERROR/Database(276): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1202)

06-06 14:49:32.434: ERROR/Database(276): at android.app.Activity.dispatchTouchEvent(Activity.java:1987)

06-06 14:49:32.434: ERROR/Database(276): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1696)

06-06 14:49:32.434: ERROR/Database(276): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)

06-06 14:49:32.434: ERROR/Database(276): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)

06-06 14:49:32.434: ERROR/Database(276): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)

06-06 14:49:32.434: ERROR/Database(276): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)

06-06 14:49:32.434: ERROR/Database(276): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)

06-06 14:49:32.434: ERROR/Database(276): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)

06-06 14:49:32.434: ERROR/Database(276): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1712)

06-06 14:49:32.434: ERROR/Database(276): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1202)

06-06 14:49:32.434: ERROR/Database(276): at android.app.Activity.dispatchTouchEvent(Activity.java:1987)

06-06 14:49:32.434: ERROR/Database(276): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1696)

06-06 14:49:32.434: ERROR/Database(276): at android.view.ViewRoot.handleMessage(ViewRoot.java:1658)

06-06 14:49:32.434: ERROR/Database(276): at android.os.Handler.dispatchMessage(Handler.java:99)

06-06 14:49:32.434: ERROR/Database(276): at android.os.Looper.loop(Looper.java:123)

06-06 14:49:32.434: ERROR/Database(276): at android.app.ActivityThread.main(ActivityThread.java:4203)

06-06 14:49:32.434: ERROR/Database(276): at java.lang.reflect.Method.invokeNative(Native Method)

06-06 14:49:32.434: ERROR/Database(276): at java.lang.reflect.Method.invoke(Method.java:521)

06-06 14:49:32.434: ERROR/Database(276): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)

06-06 14:49:32.434: ERROR/Database(276): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)

06-06 14:49:32.434: ERROR/Database(276): at dalvik.system.NativeStart.main(Native Method)

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

Can u figure out something from this?

Pavel
I've copied the relevant bits into your question for you - you've added it here as an answer.
Chris Smith
+1  A: 

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!

Chris Smith
A: 

Thx Chris - that worked just fine! Now I have another problem with that - According to the log I'm inserting records into db but when I'm retrieving them it shows only 0, 0 even though the latitude and longitude are different. How should I properly convert these values and insert them into db so I can get them later on and display? This is how I'm getting them:

latitude = (loc.getLatitude() * 1E6); longitude = (loc.getLongitude() * 1E6);

and this is how I'm converting them to int:

int intLatitude = (int)latitude; int intLongitude = (int)longitude;

and then insert them like this:

db.open();
db.insertCoords( intLatitude, intLongitude );
db.close();

Pavel
A: 

Ok I found the way out of this - I simply created a table by different name and dropped the old one. Thx again Chris for the hint.

Pavel