I created a database using SQLiteOpenHelper and I'm trying to query
it. It returns the cursor but when I call moveToFirst() it crashes
with the error SQL Logic Error or Missing Database.

My code for the database:

public class Database{
        private static final String DATABASE_NAME = "MyDatabase";
        private static final int DATABASE_VERSION = 1;
        private static final String TABLE_NAME = "MyTable";

        // Column Names
        public static final String ID = "_id";                                  
                // INTEGER
        public static final String NUMBER = "number";                           
        // TEXT

        private SQLiteDatabase database;
        private DatabaseHelper mDatabaseOpenHelper;

        public Database(Context context){
                mDatabaseOpenHelper = new DatabaseHelper(context);
                database = mDatabaseOpenHelper.getWritableDatabase();
        }

        public void close(){
                database.close();
        }

        public long addNumber(String number){
                ContentValues values = new ContentValues();
                values.put(NUMBER, number);
                return database.insert(TABLE_NAME, null, values);
        }

        public Cursor getTrUpdate(String number){
                String selection = NUMBER + " MATCH ?";
                String[] selectionArgs = new String[] {number};
                return database.query(TABLE_NAME, null, selection, 
selectionArgs,
null, null, null);
        }


        private class DatabaseHelper extends SQLiteOpenHelper{

                private static final String CREATE_TABLE = "CREATE TABLE " +
TABLE_NAME +
                                                                                
                        "(_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
                                                                                
                        NUMBER + " TEXT)";

                public DatabaseHelper(Context context){
                        super(context, DATABASE_NAME, null, DATABASE_VERSION);
                }

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

                @Override
                public void onUpgrade(SQLiteDatabase db, int oldVersion, int
newVersion) {
                        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
                        onCreate(db);
                }

        }
}

And how I query:
Database database = new Database(this);

Cursor cursor = database.getTrUpdate("1234");

if (cursor.moveToFirst()){
        do{
                int d = cursor.getInt(cursor.getColumnIndex(Database.ID));
        } while(cursor.moveToNext());
}

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to