So 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.
I follow this websites code to create and query my database: http://www.vogella.de/articles/AndroidSQLite/article.html
EDIT:
My 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 getNumber(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);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
}
And this is 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());
}
I think the error has something to do with selection and selectionArgs in my getNumber method, cause if I remove them there is no problem.
Have you checked if the array it returns is not empty?
精彩评论