android.database.sqlite.SQLiteCantOpenDatabaseException:未知错误(代码14):无法打开数据库
我已经读过关于这个问题的各种各样的东西,但我无法弄清楚这一点。 我正在使用这个应用程序的预生成数据库。 我正在使用jellybean这个应用程序。
AndroidManifest.xml中
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.quotes" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.quotes.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
Logcat输出
根据logcat它是由SQLiteCantOpenDatabaseException引起的
06-10 23:07:01.831: E/Trace(4419): error opening trace file: No such file or directory (2) 06-10 23:07:03.611: E/SQLiteLog(4419): (14) cannot open file at line 30176 of [00bb9c9ce4] 06-10 23:07:03.621: E/SQLiteLog(4419): (14) os_unix.c:30176: (2) open(/data/data/com.example.quotes/databasesQuotesdb) - 06-10 23:07:03.641: E/SQLiteDatabase(4419): Failed to open database '/data/data/com.example.quotes/databasesQuotesdb'. 06-10 23:07:03.641: E/SQLiteDatabase(4419): android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database 06-10 23:07:03.641: E/SQLiteDatabase(4419): at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method) 06-10 23:07:03.641: E/SQLiteDatabase(4419): at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209) 06-10 23:07:03.641: E/SQLiteDatabase(4419): at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193) 06-10 23:07:03.641: E/SQLiteDatabase(4419): at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463) 06-10 23:07:03.641: E/SQLiteDatabase(4419): at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185) 06-10 23:07:03.641: E/SQLiteDatabase(4419): at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177) 06-10 23:07:03.641: E/SQLiteDatabase(4419): at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:804) 06-10 23:07:03.641: E/SQLiteDatabase(4419): at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:789) 06-10 23:07:03.641: E/SQLiteDatabase(4419): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694) 06-10 23:07:03.641: E/SQLiteDatabase(4419): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:669) 06-10 23:07:03.641: E/SQLiteDatabase(4419): at com.example.quotes.DataBaseHelper.checkDataBase(DataBaseHelper.java:94) 06-10 23:07:03.641: E/SQLiteDatabase(4419): at com.example.quotes.DataBaseHelper.createDataBase(DataBaseHelper.java:58) 06-10 23:07:03.641: E/SQLiteDatabase(4419): at com.example.quotes.MainActivity.onCreate(MainActivity.java:34) 06-10 23:07:03.641: E/SQLiteDatabase(4419): at android.app.Activity.performCreate(Activity.java:5104) 06-10 23:07:03.641: E/SQLiteDatabase(4419): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 06-10 23:07:03.641: E/SQLiteDatabase(4419): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 06-10 23:07:03.641: E/SQLiteDatabase(4419): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 06-10 23:07:03.641: E/SQLiteDatabase(4419): at android.app.ActivityThread.access$600(ActivityThread.java:141) 06-10 23:07:03.641: E/SQLiteDatabase(4419): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 06-10 23:07:03.641: E/SQLiteDatabase(4419): at android.os.Handler.dispatchMessage(Handler.java:99) 06-10 23:07:03.641: E/SQLiteDatabase(4419): at android.os.Looper.loop(Looper.java:137) 06-10 23:07:03.641: E/SQLiteDatabase(4419): at android.app.ActivityThread.main(ActivityThread.java:5041) 06-10 23:07:03.641: E/SQLiteDatabase(4419): at java.lang.reflect.Method.invokeNative(Native Method) 06-10 23:07:03.641: E/SQLiteDatabase(4419): at java.lang.reflect.Method.invoke(Method.java:511) 06-10 23:07:03.641: E/SQLiteDatabase(4419): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 06-10 23:07:03.641: E/SQLiteDatabase(4419): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 06-10 23:07:03.641: E/SQLiteDatabase(4419): at dalvik.system.NativeStart.main(Native Method) 06-10 23:07:03.731: E/SQLiteLog(4419): (14) cannot open file at line 30176 of [00bb9c9ce4] 06-10 23:07:03.731: E/SQLiteLog(4419): (14) os_unix.c:30176: (2) open(/data/data/com.example.quotes/databasesQuotesdb) - 06-10 23:07:03.781: E/SQLiteDatabase(4419): Failed to open database '/data/data/com.example.quotes/databasesQuotesdb'. 06-10 23:07:03.781: E/SQLiteDatabase(4419): android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database 06-10 23:07:03.781: E/SQLiteDatabase(4419): at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method) 06-10 23:07:03.781: E/SQLiteDatabase(4419): at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209) 06-10 23:07:03.781: E/SQLiteDatabase(4419): at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193) 06-10 23:07:03.781: E/SQLiteDatabase(4419): at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463) 06-10 23:07:03.781: E/SQLiteDatabase(4419): at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185) 06-10 23:07:03.781: E/SQLiteDatabase(4419): at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177) 06-10 23:07:03.781: E/SQLiteDatabase(4419): at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:804) 06-10 23:07:03.781: E/SQLiteDatabase(4419): at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:789) 06-10 23:07:03.781: E/SQLiteDatabase(4419): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694) 06-10 23:07:03.781: E/SQLiteDatabase(4419): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:669) 06-10 23:07:03.781: E/SQLiteDatabase(4419): at com.example.quotes.DataBaseHelper.openDataBase(DataBaseHelper.java:145) 06-10 23:07:03.781: E/SQLiteDatabase(4419): at com.example.quotes.MainActivity.onCreate(MainActivity.java:44) 06-10 23:07:03.781: E/SQLiteDatabase(4419): at android.app.Activity.performCreate(Activity.java:5104) 06-10 23:07:03.781: E/SQLiteDatabase(4419): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 06-10 23:07:03.781: E/SQLiteDatabase(4419): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 06-10 23:07:03.781: E/SQLiteDatabase(4419): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 06-10 23:07:03.781: E/SQLiteDatabase(4419): at android.app.ActivityThread.access$600(ActivityThread.java:141) 06-10 23:07:03.781: E/SQLiteDatabase(4419): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 06-10 23:07:03.781: E/SQLiteDatabase(4419): at android.os.Handler.dispatchMessage(Handler.java:99) 06-10 23:07:03.781: E/SQLiteDatabase(4419): at android.os.Looper.loop(Looper.java:137) 06-10 23:07:03.781: E/SQLiteDatabase(4419): at android.app.ActivityThread.main(ActivityThread.java:5041) 06-10 23:07:03.781: E/SQLiteDatabase(4419): at java.lang.reflect.Method.invokeNative(Native Method) 06-10 23:07:03.781: E/SQLiteDatabase(4419): at java.lang.reflect.Method.invoke(Method.java:511) 06-10 23:07:03.781: E/SQLiteDatabase(4419): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 06-10 23:07:03.781: E/SQLiteDatabase(4419): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 06-10 23:07:03.781: E/SQLiteDatabase(4419): at dalvik.system.NativeStart.main(Native Method) 06-10 23:07:03.791: D/AndroidRuntime(4419): Shutting down VM 06-10 23:07:03.791: W/dalvikvm(4419): threadid=1: thread exiting with uncaught exception (group=0x40a71930) 06-10 23:07:03.831: E/AndroidRuntime(4419): FATAL EXCEPTION: main 06-10 23:07:03.831: E/AndroidRuntime(4419): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.quotes/com.example.quotes.MainActivity}: android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database 06-10 23:07:03.831: E/AndroidRuntime(4419): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 06-10 23:07:03.831: E/AndroidRuntime(4419): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 06-10 23:07:03.831: E/AndroidRuntime(4419): at android.app.ActivityThread.access$600(ActivityThread.java:141) 06-10 23:07:03.831: E/AndroidRuntime(4419): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 06-10 23:07:03.831: E/AndroidRuntime(4419): at android.os.Handler.dispatchMessage(Handler.java:99) 06-10 23:07:03.831: E/AndroidRuntime(4419): at android.os.Looper.loop(Looper.java:137) 06-10 23:07:03.831: E/AndroidRuntime(4419): at android.app.ActivityThread.main(ActivityThread.java:5041) 06-10 23:07:03.831: E/AndroidRuntime(4419): at java.lang.reflect.Method.invokeNative(Native Method) 06-10 23:07:03.831: E/AndroidRuntime(4419): at java.lang.reflect.Method.invoke(Method.java:511) 06-10 23:07:03.831: E/AndroidRuntime(4419): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 06-10 23:07:03.831: E/AndroidRuntime(4419): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 06-10 23:07:03.831: E/AndroidRuntime(4419): at dalvik.system.NativeStart.main(Native Method) 06-10 23:07:03.831: E/AndroidRuntime(4419): Caused by: android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database 06-10 23:07:03.831: E/AndroidRuntime(4419): at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method) 06-10 23:07:03.831: E/AndroidRuntime(4419): at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209) 06-10 23:07:03.831: E/AndroidRuntime(4419): at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193) 06-10 23:07:03.831: E/AndroidRuntime(4419): at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463) 06-10 23:07:03.831: E/AndroidRuntime(4419): at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185) 06-10 23:07:03.831: E/AndroidRuntime(4419): at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177) 06-10 23:07:03.831: E/AndroidRuntime(4419): at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:804) 06-10 23:07:03.831: E/AndroidRuntime(4419): at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:789) 06-10 23:07:03.831: E/AndroidRuntime(4419): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694) 06-10 23:07:03.831: E/AndroidRuntime(4419): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:669) 06-10 23:07:03.831: E/AndroidRuntime(4419): at com.example.quotes.DataBaseHelper.openDataBase(DataBaseHelper.java:145) 06-10 23:07:03.831: E/AndroidRuntime(4419): at com.example.quotes.MainActivity.onCreate(MainActivity.java:44) 06-10 23:07:03.831: E/AndroidRuntime(4419): at android.app.Activity.performCreate(Activity.java:5104) 06-10 23:07:03.831: E/AndroidRuntime(4419): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 06-10 23:07:03.831: E/AndroidRuntime(4419): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 06-10 23:07:03.831: E/AndroidRuntime(4419): ... 11 more 06-10 23:07:04.083: D/dalvikvm(4419): GC_CONCURRENT freed 203K, 11% free 2676K/3000K, paused 25ms+18ms, total 306ms 06-10 23:07:07.811: I/Process(4419): Sending signal. PID: 4419 SIG: 9
DataBaseHelper.java
public class DataBaseHelper extends SQLiteOpenHelper{ //The Android's default system path of your application database. private static String DB_PATH = "/data/data/com.example.quotes/databases"; private static String DB_NAME = "Quotesdb"; // Table Names of Data Base. static final String TABLE_Name = "Quotes"; // Contacts Table Columns names //private static final String _Id = "_Id"; //private static final String quotes = "quotes"; //private static final String author = "author"; private SQLiteDatabase myDataBase; private final Context myContext; /** * Constructor * Takes and keeps a reference of the passed context in order to access to the application assets and resources. * @param context */ public DataBaseHelper(Context context) { super(context, DB_NAME, null, 1); this.myContext = context; } /** * Creates a empty database on the system and rewrites it with your own database. * */ public void createDataBase() throws IOException{ boolean dbExist = checkDataBase(); SQLiteDatabase db_Read = null; if(dbExist){ //do nothing - database already exist }else{ //By calling this method and empty database will be created into the default system path //of your application so we are gonna be able to overwrite that database with our database. db_Read = this.getReadableDatabase(); db_Read.close(); try { copyDataBase(); } catch (IOException e) { //throw new Error("Error copying database"); e.toString(); } } } /** * Check if the database already exist to avoid re-copying the file each time you open the application. * @return true if it exists, false if it doesn't */ private boolean checkDataBase(){ SQLiteDatabase checkDB = null; try{ String myPath = DB_PATH + DB_NAME; checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); }catch(SQLiteException e){ //database does't exist yet. } if(checkDB != null){ checkDB.close(); } return checkDB != null ? true : false; } /** * Copies your database from your local assets-folder to the just created empty database in the * system folder, from where it can be accessed and handled. * This is done by transfering bytestream. * */ private void copyDataBase() throws IOException{ //Open your local db as the input stream InputStream myInput = myContext.getAssets().open(DB_NAME); // Path to the just created empty db String outFileName = DB_PATH + DB_NAME; //Open the empty db as the output stream OutputStream myOutput = new FileOutputStream(outFileName); //transfer bytes from the inputfile to the outputfile byte[] buffer = new byte[1024]; int length; while ((length = myInput.read(buffer))>0){ myOutput.write(buffer, 0, length); } //Close the streams myOutput.flush(); myOutput.close(); myInput.close(); } public void openDataBase() throws SQLException{ //Open the database String myPath = DB_PATH + DB_NAME; myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); } @Override public synchronized void close() { if(myDataBase != null) myDataBase.close(); super.close(); } @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub } /*// Getting single contact public List<Quotes> getQuote(int id) { ArrayList<Quotes>(); getReadableDatabase(); Cursor cursor = db.query(TABLE_Name, new String[] { _Id, qotes,author }, _Id + "=?", new String[] { String.valueOf(id) }, null, null, null, null); //String query = "SELECT _Id, qotes, author From "+TABLE_Name; //String query = "SELECT * From Quotes"; //Cursor cursor = db.rawQuery(query, null); if (cursor != null) cursor.moveToFirst(); Quotes quotes = new Quotes(Integer.parseInt(cursor.getString(0)), cursor.getString(1),cursor.getString(2)); QuoteList.add(quotes); return QuoteList; } */ }
将这个权限添加到您的项目的AndroidManifest.xml
文件中,在manifest
标记(应该是顶级标记)中。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
用下面的代码replacecheckDataBase()代码:
File dbFile = myContext.getDatabasePath(DB_NAME); return dbFile.exists();
如果您在Android的棉花糖或更高版本(API等级23或更高版本)上运行应用程序,则可能会遇到此问题,因为此处引入了新的实时权限模型。
从Android 6.0(API级别23)开始,用户在应用程序运行时向应用程序授予权限,而不是在安装应用程序时授予应用程序权限。 此方法简化了应用程序安装过程,因为用户在安装或更新应用程序时无需授予权限。
为了在运行时获得权限,你将不得不请求用户。 你可以这样做。
首先请求权限。
String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE}; requestPermissions(permissions, WRITE_REQUEST_CODE);
然后你可以检查结果
@Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { switch (requestCode) { case WRITE_REQUEST_CODE: if(grantResults[0] == PackageManager.PERMISSION_GRANTED){ //Permission granted. //Continue with writing files... } else{ //Permission denied. } break; } }
这里是很好的学习源码请求运行时权限在Android的棉花糖/
这可能是由于权限不够或SELinux引起的。 确保权限和所有者/组设置正确。 然后运行这个:
restorecon /data/data/your.app.folder/databases/your.db
restorecon恢复文件的默认SELinux安全上下文
我认为这是因为你的DB_Path
没有“/”。 将path连接到数据库时,variablesmyPath
具有以下string: "/data/data/com.example.quotes/databasesQuotesdb"
。
我提出这些改变:
private static String DB_PATH = "/data/data/com.example.quotes/databases/"; private static String DB_NAME = "Quotesdb.db";
还有一件事,你应该在Main活动中打开数据库,分别用database.close()
和database.open()
实现onPause()
和onResume()
方法。 这是我主要活动的例子(用于cordova,网页devise和所有的东西= P):
package home.shell.accessApp; import android.os.Bundle; import android.util.Log; import org.apache.cordova.*; public class AccessApp extends CordovaActivity { private SellitDAO db; //This is the class name for my SQLiteOpenHelper @Override public void onCreate(Bundle savedInstanceState) { db = new SellitDAO(this); db.open(); super.onCreate(savedInstanceState); super.init(); // Set by <content src="index.html" /> in config.xml super.loadUrl(Config.getStartUrl()); //super.loadUrl("file:///android_asset/www/index.html") } @Override public void onPause() { super.onPause(); db.close(); } @Override public void onResume() { super.onResume(); db.open(); } }
祝你好运!!!
DB_PATH指向不同的数据库。 改变它在数据库帮助类和我的代码工作。
private static String DB_PATH = "/data/data/com.example.abc";
我不认为这是你的问题,但对我来说还是很糟糕的。 您的项目中有一个重复的com.examples.quote图层。 您的AndroidManifest.xml中的“活动”部分应该看起来更像这样:
<activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
你甚至可以把你的类放在src / com.example.quotes / com.example.quotes中,而不仅仅是在com.example.quotes中。
我不确定这是否会导致您的问题。 但看起来有点乱。 所有其他的东西看起来都很标准。
@nmr也检查sqlite文件本身的所有权和组…我已经复制我的数据库从/ SD卡作为根,所以不得不改变权限..
-rw-rw---- root root 180224 2014-05-05 11:06 test.sqlite
应该..
-rw-rw---- u0_a80 u0_a80 180224 2014-05-05 11:06 test.sqlite
使用以下命令来设置正确的所有权和组。 我从ls -al
目录中的其他文件获得u0_a80
chown u0_a80 test.sqlite chgrp u0_a80 test.sqlite
我的这个问题gremlin是不好的目录权限:
良好的权限:
drwxr-x--x u0_a20 u0_a20 2013-11-13 20:45 com.google.earth drwxr-x--x u0_a63 u0_a63 2013-11-13 20:46 com.nuance.xt9.input drwxr-x--x u0_a53 u0_a53 2013-11-13 20:45 com.tf.thinkdroid.sg drwxr-x--x u0_a68 u0_a68 2013-12-24 15:03 eu.chainfire.supersu drwxr-x--x u0_a59 u0_a59 2013-11-13 20:45 jp.co.omronsoft.iwnnime.ml drwxr-x--x u0_a60 u0_a60 2013-11-13 20:45 jp.co.omronsoft.iwnnime.ml.kbd.white drwxr-x--x u0_a69 u0_a69 2013-12-24 15:03 org.mozilla.firefox
错误的权限:
root@grouper:/data/data # ls -lad com.mypackage drw-rw-r-- u0_a70 u0_a70 2014-01-11 14:18 com.mypackage
他们是如何得到这种方式? 我设置他们,同时摆动试图让adb pull
上class。 显然我做错了。
嘿谷歌,如果一个权限错误产生了一个有意义的错误信息将是非常好的,否则,如果你不必手动调整权限来使用这些工具。
在OpenDatabase之前添加这行:
File outFile = new File(Environment.getDataDirectory(), outFileName); outFile.setWritable(true); SQLiteDatabase.openDatabase(outFile.getAbsolutePath(), null, SQLiteDatabase.OPEN_READWRITE);
正如@CommonsWare所提到的,你会想尝试android sqlite资产助手 。 它为我打开了一个预先存在的db一块蛋糕。
我花了3个小时试图手动完成所有的工作大概半个小时。 有趣的是,我以为我在做图书馆为我做的同样的事情,但是有些东西不见了!
清理应用程序数据帮助了我。 它可以帮助你..
请确保您没有试图从主线程或后台线程反复打开和closures数据库。
在您的应用程序中创build一个单例类,并尝试仅创build和打开此类中的数据。
保证你只有在数据库不存在时才打开数据库。
在您的整个应用程序中,使用与需要时获取sqLiteDatabase对象相同的方法。
我使用下面的代码,我的问题现在解决1.5天后。
...............................................................
在你的Activity类中的onCreate()方法
public class MainActivity extends AppCompatActivity { private AssetsDatabaseHelper helper; @Override protected void onCreate(Bundle savedInstanceState) { helper = AssetsDatabaseHelper.getInstance(this); sqLiteDatabase = helper.getDatabase(); } } public class AssetsDatabaseHelper { Context context; SQLiteDatabase sqLiteDatabase; DatabaseHelper databaseHelper; private static AssetsDatabaseHelper instance; private AssetsDatabaseHelper(Context context){ this.context = context; databaseHelper = new DatabaseHelper(context); if(databaseHelper.checkDatabase()){ try{ databaseHelper.openDatabase(); }catch(SQLException sqle){ Log.e("Exception in opening ", " :: database :: sqle.getCause() : "+sqle.getCause()); } }else{ try{ databaseHelper.createDatabase(); }catch(IOException ioe){ Log.d("Exception in creating ", " :: database :: ioe.getCause() : "+ioe.getCause()); } try{ databaseHelper.openDatabase(); }catch(SQLException sqle){ Log.e("Exception in opening ", " :: database :: "+sqle.getCause()); } } sqLiteDatabase = databaseHelper.getSqLiteDatabase(); } public static AssetsDatabaseHelper getInstance(Context context){ if(instance != null){ return instance; }else { instance = new AssetsDatabaseHelper(context); return instance; } } public SQLiteDatabase getDatabase(){ return sqLiteDatabase; } }