Android SQLiteDatabase查询SQL逻辑错误或缺少数据库

时间:2011-06-02 14:38:22

标签: android sqlite

所以我使用SQLiteOpenHelper创建了一个数据库,我正在尝试查询它。它返回光标但是当我调用moveToFirst()时它会因错误SQL Logic Error or Missing Database.

而崩溃

我按照此网站代码创建和查询我的数据库:http://www.vogella.de/articles/AndroidSQLite/article.html

编辑:

我的数据库:

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);
          }
      }
 }

这就是我查询的方式:

 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());
 }

2 个答案:

答案 0 :(得分:0)

我认为错误与我的getNumber方法中的selection和selectionArgs有关,因为如果我删除它们没有问题。

答案 1 :(得分:-1)

您是否检查过它返回的数组是否为空?