0

有人指导我plzzzz。我有一个用于执行 CRUD 操作的基本 android-sqlite-database 应用程序。当我对/data/data/package-name/mydatabase.db. 但是现在我必须将数据库文件嵌入到应用程序中。我已将文件放在.db文件assets夹中并编写了此代码。但是面临如下所述的持续问题......我的DatabaseHandler类如下......

private static final String DATABASE_PATH = "com.example.database_abc/assets/contactsDB.db" ; 
private static final String DATABASE_NAME = "contactsDB";
private  SQLiteDatabase myDatabase ;
private final Context myContext ;

public DatabaseHandler3(Context context){
    super(context, DATABASE_NAME, null, 1);
    this.myContext = context ;

}

public boolean checkDatabase(){
    SQLiteDatabase checkDB = null ;
    try{
        checkDB = SQLiteDatabase.openDatabase( DATABASE_PATH , null, SQLiteDatabase.OPEN_READWRITE);
    }catch(SQLException ioex){
        throw new Error("Database Not Found..........................");
    }

    if(checkDB != null){
        checkDB.close();
    }

    return checkDB != null ? true : false ;
}// End of checkDataBase()


public void openDataBase() throws SQLException{

    //Open the database
    String myPath = DATABASE_PATH ;
    myDatabase = SQLiteDatabase.openDatabase(DATABASE_PATH, null, SQLiteDatabase.OPEN_READWRITE);

   }

@Override
public void onCreate(SQLiteDatabase db){

}
@Override
public void onUpgrade(SQLiteDatabase db, int arg1, int arg2){

} 

Logcat的如下..

12-13 17:24:29.244: E/SQLiteLog(5563): (14) cannot open file at line 30176 of [00bb9c9ce4]
12-13 17:24:29.244: E/SQLiteLog(5563): (14) os_unix.c:30176: (2) open(//com.example.database_abc/assets/contactsDB.db3) - 
12-13 17:24:29.384: E/SQLiteDatabase(5563): Failed to open database 'com.example.database_abc/assets/contactsDB.db3'.
12-13 17:24:29.384: E/SQLiteDatabase(5563): android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
12-13 17:24:29.384: E/SQLiteDatabase(5563):     at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
12-13 17:24:29.384: E/SQLiteDatabase(5563):     at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209)
12-13 17:24:29.384: E/SQLiteDatabase(5563):     at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
12-13 17:24:29.384: E/SQLiteDatabase(5563):     at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
12-13 17:24:29.384: E/SQLiteDatabase(5563):     at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
12-13 17:24:29.384: E/SQLiteDatabase(5563):     at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
12-13 17:24:29.384: E/SQLiteDatabase(5563):     at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:804)
12-13 17:24:29.384: E/SQLiteDatabase(5563):     at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:789)
12-13 17:24:29.384: E/SQLiteDatabase(5563):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
12-13 17:24:29.384: E/SQLiteDatabase(5563):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:669)
12-13 17:24:29.384: E/SQLiteDatabase(5563):     at com.example.database_abc.DatabaseHandler3.checkDatabase(DatabaseHandler3.java:27)
12-13 17:24:29.384: E/SQLiteDatabase(5563):     at com.example.database_abc.MainActivity.onButtonClick(MainActivity.java:55)
12-13 17:24:29.384: E/SQLiteDatabase(5563):     at java.lang.reflect.Method.invokeNative(Native Method)
12-13 17:24:29.384: E/SQLiteDatabase(5563):     at java.lang.reflect.Method.invoke(Method.java:511)
12-13 17:24:29.384: E/SQLiteDatabase(5563):     at android.view.View$1.onClick(View.java:3592)
12-13 17:24:29.384: E/SQLiteDatabase(5563):     at android.view.View.performClick(View.java:4202)
12-13 17:24:29.384: E/SQLiteDatabase(5563):     at android.view.View$PerformClick.run(View.java:17340)
12-13 17:24:29.384: E/SQLiteDatabase(5563):     at android.os.Handler.handleCallback(Handler.java:725)
12-13 17:24:29.384: E/SQLiteDatabase(5563):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-13 17:24:29.384: E/SQLiteDatabase(5563):     at android.os.Looper.loop(Looper.java:137)
12-13 17:24:29.384: E/SQLiteDatabase(5563):     at android.app.ActivityThread.main(ActivityThread.java:5039)
12-13 17:24:29.384: E/SQLiteDatabase(5563):     at java.lang.reflect.Method.invokeNative(Native Method)
12-13 17:24:29.384: E/SQLiteDatabase(5563):     at java.lang.reflect.Method.invoke(Method.java:511)
12-13 17:24:29.384: E/SQLiteDatabase(5563):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-13 17:24:29.384: E/SQLiteDatabase(5563):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-13 17:24:29.384: E/SQLiteDatabase(5563):     at dalvik.system.NativeStart.main(Native Method)
12-13 17:24:29.384: D/AndroidRuntime(5563): Shutting down VM
12-13 17:24:29.394: W/dalvikvm(5563): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
12-13 17:24:29.504: E/AndroidRuntime(5563): FATAL EXCEPTION: main
12-13 17:24:29.504: E/AndroidRuntime(5563): java.lang.IllegalStateException: Could not execute method of the activity
12-13 17:24:29.504: E/AndroidRuntime(5563):     at android.view.View$1.onClick(View.java:3597)
12-13 17:24:29.504: E/AndroidRuntime(5563):     at android.view.View.performClick(View.java:4202)
12-13 17:24:29.504: E/AndroidRuntime(5563):     at android.view.View$PerformClick.run(View.java:17340)
12-13 17:24:29.504: E/AndroidRuntime(5563):     at android.os.Handler.handleCallback(Handler.java:725)
12-13 17:24:29.504: E/AndroidRuntime(5563):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-13 17:24:29.504: E/AndroidRuntime(5563):     at android.os.Looper.loop(Looper.java:137)
12-13 17:24:29.504: E/AndroidRuntime(5563):     at android.app.ActivityThread.main(ActivityThread.java:5039)
12-13 17:24:29.504: E/AndroidRuntime(5563):     at java.lang.reflect.Method.invokeNative(Native Method)
12-13 17:24:29.504: E/AndroidRuntime(5563):     at java.lang.reflect.Method.invoke(Method.java:511)
12-13 17:24:29.504: E/AndroidRuntime(5563):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-13 17:24:29.504: E/AndroidRuntime(5563):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-13 17:24:29.504: E/AndroidRuntime(5563):     at dalvik.system.NativeStart.main(Native Method)
12-13 17:24:29.504: E/AndroidRuntime(5563): Caused by: java.lang.reflect.InvocationTargetException
12-13 17:24:29.504: E/AndroidRuntime(5563):     at java.lang.reflect.Method.invokeNative(Native Method)
12-13 17:24:29.504: E/AndroidRuntime(5563):     at java.lang.reflect.Method.invoke(Method.java:511)
12-13 17:24:29.504: E/AndroidRuntime(5563):     at android.view.View$1.onClick(View.java:3592)
12-13 17:24:29.504: E/AndroidRuntime(5563):     ... 11 more
12-13 17:24:29.504: E/AndroidRuntime(5563): Caused by: java.lang.Error: Database Not Found..........................
12-13 17:24:29.504: E/AndroidRuntime(5563):     at com.example.database_abc.DatabaseHandler3.checkDatabase(DatabaseHandler3.java:29)
12-13 17:24:29.504: E/AndroidRuntime(5563):     at com.example.database_abc.MainActivity.onButtonClick(MainActivity.java:55)
12-13 17:24:29.504: E/AndroidRuntime(5563):     ... 14 more
4

3 回答 3

1

不知道你是如何创建数据库的,但是使用 Firefox SQLiteManager之类的东西来创建,然后粘贴到 assets 目录中对我来说一直很好。另一个原因可能是版本不兼容,或者扩展名(db3vs. dbor sqlite)很简单。

于 2012-12-13T18:16:40.980 回答
0

不要这样做有什么问题,但我认为你最好试试这个

SQLiteDatabase.openOrCreateDatabase(路径,工厂);

于 2012-12-13T18:19:27.013 回答
0

这是我用来创建使用只读 sqlite 数据库的应用程序的教程:

http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/

它提供了将打包在您的资产文件夹中的数据库移动到“/data/data/XX.XXX.XX/databases”位置中的最终存放位置所需的所有代码。

如果您的数据库很大,也请阅读此链接:

http://ponystyle.com/blog/2010/03/26/dealing-with-asset-compression-in-android-apps/

于 2012-12-13T18:13:47.513 回答