2013-08-02 130 views
-1

似乎數據庫已創建,但當我嘗試訪問創建的數據庫時出現錯誤。每當調用doQuery()時,應用程序崩潰。你能幫我弄清楚我做錯了什麼嗎?在Android中訪問數據庫的問題,表未創建

private class LoadCursorTask extends AsyncTask<Void, Void, Void> 
    { 
     Cursor constantCursor=null;  

    @Override  
    protected Void doInBackground(Void... params) 
    { 

     // TODO Auto-generated method stub 
     Log.d("Action1","in doInBacground()"); 
     constantCursor=doQuery(); 
     Log.d("Action1","after doQuery "); 
     constantCursor.getCount(); 

     return null; 
    } 


} 


private Cursor doQuery() 
{ 

    Log.d("Action1","in doQuery()"); 
    Cursor localCursor= db.getReadableDatabase().rawQuery("SELECT _id, title, value"+" FROM constants ORDER BY title; ", null); 
    Log.d("Action1","before returning from doQuery()"); 
    return localCursor; 
} 

這樣的stacktrack顯示錯誤報告:

08-02 11:23:31.012: E/SQLiteLog(1829): (1) no such table: constants 
08-02 11:23:31.040: W/dalvikvm(1829): threadid=11: thread exiting with uncaught exception (group=0x40a71930) 
08-02 11:23:31.173: E/AndroidRuntime(1829): FATAL EXCEPTION: AsyncTask #1 
08-02 11:23:31.173: E/AndroidRuntime(1829): java.lang.RuntimeException: An error occured while executing doInBackground() 
08-02 11:23:31.173: E/AndroidRuntime(1829):  at android.os.AsyncTask$3.done(AsyncTask.java:299) 
08-02 11:23:31.173: E/AndroidRuntime(1829):  at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352) 
08-02 11:23:31.173: E/AndroidRuntime(1829):  at java.util.concurrent.FutureTask.setException(FutureTask.java:219) 
08-02 11:23:31.173: E/AndroidRuntime(1829):  at java.util.concurrent.FutureTask.run(FutureTask.java:239) 
08-02 11:23:31.173: E/AndroidRuntime(1829):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 
08-02 11:23:31.173: E/AndroidRuntime(1829):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 
08-02 11:23:31.173: E/AndroidRuntime(1829):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 
08-02 11:23:31.173: E/AndroidRuntime(1829):  at java.lang.Thread.run(Thread.java:856) 
08-02 11:23:31.173: E/AndroidRuntime(1829): Caused by: android.database.sqlite.SQLiteException: no such table: constants (code 1): , while compiling: SELECT _id, title, value FROM constants ORDER BY title; 
08-02 11:23:31.173: E/AndroidRuntime(1829):  at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) 
08-02 11:23:31.173: E/AndroidRuntime(1829):  at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882) 
08-02 11:23:31.173: E/AndroidRuntime(1829):  at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493) 
08-02 11:23:31.173: E/AndroidRuntime(1829):  at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) 
08-02 11:23:31.173: E/AndroidRuntime(1829):  at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58) 
08-02 11:23:31.173: E/AndroidRuntime(1829):  at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37) 
08-02 11:23:31.173: E/AndroidRuntime(1829):  at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44) 
08-02 11:23:31.173: E/AndroidRuntime(1829):  at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314) 
08-02 11:23:31.173: E/AndroidRuntime(1829):  at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253) 
08-02 11:23:31.173: E/AndroidRuntime(1829):  at com.example.database_constants.ConstantsFragment.doQuery(ConstantsFragment.java:28) 
08-02 11:23:31.173: E/AndroidRuntime(1829):  at com.example.database_constants.ConstantsFragment.access$0(ConstantsFragment.java:25) 
08-02 11:23:31.173: E/AndroidRuntime(1829):  at com.example.database_constants.ConstantsFragment$LoadCursorTask.doInBackground(ConstantsFragment.java:47) 
08-02 11:23:31.173: E/AndroidRuntime(1829):  at com.example.database_constants.ConstantsFragment$LoadCursorTask.doInBackground(ConstantsFragment.java:1) 
08-02 11:23:31.173: E/AndroidRuntime(1829):  at android.os.AsyncTask$2.call(AsyncTask.java:287) 
08-02 11:23:31.173: E/AndroidRuntime(1829):  at java.util.concurrent.FutureTask.run(FutureTask.java:234) 
08-02 11:23:31.173: E/AndroidRuntime(1829):  ... 4 more 

下面是我創建的數據庫:

db.execSQL("CREATE TABLE constants (_id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT, value REAL);"); 

[更新1] 這裏的DatabaseHelper.class

public class DatabaseHelper extends SQLiteOpenHelper 
{ 
    private static final String DATABASE_NAME="constants.db"; 
    private static final int SCHEMA=1; 
    static final String TITLE="title"; 
    static final String VALUE="value"; 
    static final String TABLE="constants"; 


public DatabaseHelper(Context context) { 
    super(context, DATABASE_NAME, null, SCHEMA); 
    // TODO Auto-generated constructor stub 
    Log.d("Action1","in DatabaseHelper constructor"); 
} 

@Override 
public void onCreate(SQLiteDatabase db) 
{ 
    Log.d("Action1","in onCreate"); 
    try 
    { 
     db.beginTransaction(); 
     db.execSQL("CREATE TABLE constants (_id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT, value REAL)"); 

     ContentValues cv=new ContentValues(); 

     cv.put(TITLE, "Gravity, Death Star I"); 
     cv.put(VALUE, SensorManager.GRAVITY_DEATH_STAR_I); 
     db.insert("constants", TITLE, cv); 

     cv.put(TITLE, "Gravity, Earth"); 
     cv.put(VALUE, SensorManager.GRAVITY_EARTH); 
     db.insert("constants", TITLE, cv); 

     cv.put(TITLE, "Gravity, Jupiter"); 
     cv.put(VALUE, SensorManager.GRAVITY_JUPITER); 
      db.insert("constants", TITLE, cv); 
    } 
     finally 
    { 

     db.endTransaction(); 
    } 

} 
} 
+0

嗯... 「產生的原因:android.database.sqlite.SQLiteException:沒有這樣的表:常數(碼1)」 –

+0

沒有這樣的表:常數。創建常量表 – Sri

+0

刪除「;」在你的數據庫中創建一行。 – Andrain

回答

0

它可能不會找到您的數據庫文件,並嘗試打開它時創建一個新的空的文件。檢查數據庫的名稱和位置。如果您使用代碼創建它,請務必在嘗試訪問它之前創建該表。

編輯:

找到您的問題,您需要endTransaction之前將事務標記爲全成,否則它就會被回滾。

@Override 
public void onCreate(SQLiteDatabase db) 
{ 
    Log.d("Action1","in onCreate"); 
    try 
    { 
     db.beginTransaction(); 
     db.execSQL("CREATE TABLE constants (_id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT, value REAL)"); 

     ContentValues cv=new ContentValues(); 

     cv.put(TITLE, "Gravity, Death Star I"); 
     cv.put(VALUE, SensorManager.GRAVITY_DEATH_STAR_I); 
     db.insert("constants", TITLE, cv); 

     cv.put(TITLE, "Gravity, Earth"); 
     cv.put(VALUE, SensorManager.GRAVITY_EARTH); 
     db.insert("constants", TITLE, cv); 

     cv.put(TITLE, "Gravity, Jupiter"); 
     cv.put(VALUE, SensorManager.GRAVITY_JUPITER); 
     db.insert("constants", TITLE, cv); 
     db.setTransactionSuccessful(); 
    } 
     finally 
    { 

     db.endTransaction(); 
    } 

} 

Source

+0

好吧,所以我檢查數據庫是否通過DDMS文件資源管理器創建,是的,數據庫文件在那裏。在檢查數據庫時,表格似乎沒有被創建,所以存在如你所說的問題。 對不起,如果我聽起來天真,但我似乎無法弄清楚爲什麼表沒有得到創造,你能嗎?把我拉出來嗎? :\ – user2499998

+0

在使用endTransaction之前使用db.setTransactionSuccessful()。編輯答案。 –

+0

最後。現在就像魅力一樣。非常感謝你:) – user2499998