-1

It seems like the database gets created, but I get error when I try to access the database that was created. Whenever the doQuery() is invoked, the app crashes. Could you please help me figure out what I am doing wrong?

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

The error report on stacktrack show like this:

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

Here's how I've created the database:

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

[UPDATE 1] Here's the 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(); 
    }

}
}
4

1 回答 1

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

}

资源

于 2013-08-02T11:45:06.173 回答