2011-06-02 92 views
0

所以我創建了一個數據庫使用SQLiteOpenHelper,我試圖查詢它。它返回遊標,但是當我打電話moveToFirst()它與錯誤SQL Logic Error or Missing Database.Android SQLiteDatabase查詢SQL邏輯錯誤或丟失的數據庫

我按照這個網站的代碼來創建和查詢我的數據庫崩潰了: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()); 
} 
+0

你還可以發佈你的DataBaseHelper代碼嗎? – Albinoswordfish 2011-06-02 15:24:01

回答

-1

你檢查過它返回的數組是不是空的?

+0

你在說什麼? – Hank 2011-06-03 16:43:58

0

我認爲這個錯誤與我的getNumber方法中的selection和selectionArgs有關,因爲如果我將它們移除,沒有任何問題。

相關問題