安卓数据库打开异常:未知错误(代码14):无法打开数据库。

75

我已经阅读了各种关于这个问题的现场信息,但是我无法弄清楚。我正在为这个应用程序使用预构建数据库。我正在使用Jelly Bean版本进行此应用程序。

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>

日志输出

根据日志输出显示,这是由于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;
    }
    */
}

请提供更多信息。特别是,您做了什么导致了这个错误? - Code-Apprentice
我在模拟器上运行它,但它崩溃了。不幸的是,应用程序已停止。 - user2438323
我没有使用SQLiteAssetHelper。 - user2438323
当我运行ActivityInstrumentationTestCase2时,我遇到了相同的问题。 - Tim Kist
@user2438323:你好,你的问题解决了吗?我也遇到了类似代码的相似问题。 - Amit Kumar Kannaujiya
显示剩余3条评论
21个回答

41

将此权限添加到项目的AndroidManifest.xml文件中,在manifest标记中(应为顶级标记)。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

3
哦!完全忘记了这件事!非常感谢。 - user586399
6
注意:在Android Marshmallow中,您需要先实现权限请求,因为读写外部存储是一项“危险权限”。更多信息请参见:http://developer.android.com/training/permissions/requesting.html - Felix Edelmann
12
路径为"/data/data/com.example.quotes/databases"的是内部存储,为什么我们需要外部存储权限? - nAkhmedov
为什么它被投票得如此之高?@nAkhmedov是正确的,我们不需要许可。Felix Edelmann说我们需要从Android M开始请求权限。在这种情况下,每个应用程序都会在第一次启动时请求读/写存储权限。 - Oleksandr Albul
第一次必须请求权限。此外,上述权限必须在清单文件中。 - saeed khalafinejad

27

将 checkDataBase() 代码替换为以下代码:

File dbFile = myContext.getDatabasePath(DB_NAME);
return dbFile.exists();

18

如果你在安卓6.0或更高版本(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 Marshmallow中请求运行时权限的方法。



14

这可能是由于权限不正确或SELinux引起的。请确保权限和所有者/组已正确设置,然后运行以下命令:

restorecon /data/data/your.app.folder/databases/your.db

restorecon 用于还原文件的 SELinux 安全上下文


Android 7.0需要使用-F标志来进行强制操作,直到它工作为止。restorecon -F - user1652110
@Benoit Duffez,您可以分享关于SELinux部分的更多信息吗?我正在处理系统应用程序,在一个周期中遇到了相同的错误。 - Shijil
@Shijil:不好意思,我不能。那是七年前我用的手机,我不知道今天什么还适用。 - Benoit Duffez

5
我认为问题出在你的DB_Path没有以“/”结尾。当你将路径与数据库连接时,变量myPath的字符串如下:"/data/data/com.example.quotes/databasesQuotesdb"
我建议进行以下更改:
private static String DB_PATH = "/data/data/com.example.quotes/databases/";
private static String DB_NAME = "Quotesdb.db";

另外一件事,你应该在主活动中打开数据库,并分别使用database.close()database.open()实现onPause()onResume()方法。以下是我用于主活动的示例(与Cordova、Web设计和所有相关内容一起使用):

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

Good Luck!!!


4

@nmr也要检查sqlite文件本身的所有权和组...我将我的数据库从/sdcard复制过来时使用了root权限,因此必须更改权限。

-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

4
DB_PATH指向了不同的数据库。在数据库助手类中更改它,我的代码就能正常工作了。
private static String DB_PATH = "/data/data/com.example.abc";

3
我最近遇到了这个错误,但它更加奇怪。我正在开发 Android N,并一切都进行得很顺利,直到我在 JellyBeans 和 Lollipop 上测试时。在这些版本上,我一直收到相同的 DB 错误信息。
android.database.sqlite.SQLiteCantOpenDatabaseException: 未知错误(代码14):无法打开数据库 06-10 23:07:03.641:
E/SQLiteDatabase(4419):
我的清单文件中包含了正确的权限,其中包括:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

问题出现在我的DataBaseHelper.java文件中的这一行代码:
private static String DB_PATH = "/data/data/com.example.quotes/databases";

需要通过以下方式从上下文中加载:

String DB_PATH = getContext().getApplicationInfo().dataDir+"/databases/";

现在SDK 17表单已经正常工作。


我希望早些看到这篇文章,这可以节省我两天的时间。 - Mohammedsalim Shivani
getContext().getApplicationInfo().dataDir+"/databases/"; 替换为 new File(getContext().getApplicationInfo().dataDir, "databases").getAbsolutePath(); 将处理添加必要的路径分隔符。 - david
只有这一个对我起作用。 - Ankit Saini

3

我认为这不是你的问题,但对我来说看起来还是很糟糕的。在你的项目中有一个重复的com.examples.quote图层。你的AndroidManifest.xml文件中的Activity部分应该更像这样:

        <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。
我不确定这是否导致了你的问题,但它看起来有点混乱。所有其他东西看起来对我来说都很标准。

2

我为这个问题提供的解决方案是目录权限不足:

正确的权限:

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时调整它们的方式。显然,我做错了。嘿,Google,如果权限错误产生有意义的错误消息,或者您不必手动调整权限来使用工具,那将是非常好的。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接