2017-02-10 122 views
-1

我的問題是,我不能使用遊標從數據庫檢索的數據,並在所有情況下的SQLite將引發異常SQLite和光標

db.execSQL("CREATE TABLE BOOKS (" 
    + "_ID INTEGER PRIMARY KEY AUTOINCREMENT, " 
    + "Name TEXT, " 
    + "Sub_Field TEXT, " 
    + "Cover INTEGER, " 
    + "Priority INTEGER);"); 

ContentValues BookValue = new ContentValues(); 

BookValue.put("Name", "Cisco"); 
BookValue.put("Sub_Field", "Net"); 
BookValue.put("Cover", R.drawable.cappuccino); 
BookValue.put("Priority", 1); 

db.insert("BOOKS", null, BookValue); 

MainActivity:

try { 
    SQLiteOpenHelper myDatabase = new myDatabase(this); 
    db = myDatabase.getWritableDatabase(); 
    myText = (TextView) findViewById(R.id.txt); 

    cursor = db.query("BOOK", new String[] {"Name", "Sub_Field"}, null,null, null, null,null); 

    if (cursor != null) { 
    cursor.moveToFirst(); 
    String name = cursor.getString(0); 
    myText.setText(name); 
    } 

} catch (SQLiteException e) { 
    myText = (TextView) findViewById(R.id.txt); 
    myText.setText("Try Again, you're soon to achieve your Goal..."); 
    myText.setTextSize(20); 
} 
+1

添加你的正在發生的問題plz –

+0

'Slqite異常錯誤'意味着你有一些SQLite的問題。有關更多信息,您應該閱讀錯誤消息和堆棧跟蹤。 –

+0

錯誤是什麼?嘗試添加'Log.d(「TAG」,「exception =」+ e.toString());'在catch(SQLiteException e){}'中記錄錯誤。 –

回答

0

在你查詢(選擇)你的表名是「BOOK」,但你創建表「BOOKS」並將數據插入到「BOOKS」中