Android studio在带有android assethelper的assets文件夹中打开sqlite db

时间:2014-03-03 14:05:42

标签: android database sqlite

我在资源文件夹中有我的数据库,我尝试打开它,但无法正常工作。我尝试过一些OpenHelper自定义类,其代码取自其他stackoverflow的问题,但不起作用(无法打开db)。最后我用Android SQLiteAssetHelper打开了数据库,但它返回“没有这样的表”错误。 openHelper类:

public class DBOpenHelper extends SQLiteAssetHelper
{
    private static final String DATABASE_NAME="dbSinonimi.db";

    public DBOpenHelper(Context context) {
        super(context, DATABASE_NAME, null, 1);
    }
}

数据库管理员:

public class DBManager {

SQLiteDatabase database;
DBOpenHelper dbOpenHelper;
Context context;

public DBManager(Context ctx)
{
    this.context = ctx;
    dbOpenHelper = new DBOpenHelper(context);
    database = dbOpenHelper.getReadableDatabase();
}

public boolean checkParola(String parola)
{

    Cursor c = database.rawQuery(
         "select * from parole where nome = '" + parola + "'", null);

    if (c != null) {
        c.moveToFirst();
        return true;
    } else return false;
}

在rawquery执行时出现错误:

03-03 13:15:13.707      648-648/com.app.Sinonimous E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.Sinonimous/com.app.Sinonimous.SearchResult}: android.database.sqlite.SQLiteException: no such table: parole: , while compiling: select * from parole where nome = 'hhol'
  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
  at android.app.ActivityThread.access$600(ActivityThread.java:123)
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
  at android.os.Handler.dispatchMessage(Handler.java:99)
  at android.os.Looper.loop(Looper.java:137)
  at android.app.ActivityThread.main(ActivityThread.java:4424)
  at java.lang.reflect.Method.invokeNative(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:511)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: android.database.sqlite.SQLiteException: no such table: parole: , while compiling: select * from parole where nome = 'hhol'
        at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
        at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:68)
        at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:143)
        at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:361)
        at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:127)
        at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:94)
        at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:53)
        at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47)
        at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1564)
        at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1538)
        at com.app.Sinonimous.database.DBManager.checkParola(DBManager.java:30)
        at com.app.Sinonimous.SearchResult.handleIntent(SearchResult.java:91)
        at com.app.Sinonimous.SearchResult.onCreate(SearchResult.java:61)
        at android.app.Activity.performCreate(Activity.java:4465)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
        at android.app.ActivityThread.access$600(ActivityThread.java:123)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
        at android.os.Handler.dispatchMessage(Handler.java:99)
   at android.os.Looper.loop(Looper.java:137)
   at android.app.ActivityThread.main(ActivityThread.java:4424)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:511)

我也试过使用Android SQLiteAssetHelper示例db(“northwind.db”),它运行得很好。为什么?我的数据库是用sqlite3创建的,它包含在assets / databases文件夹中。

0 个答案:

没有答案