【问题标题】:android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open databaseandroid.database.sqlite.SQLiteCantOpenDatabaseException:未知错误(代码 14):无法打开数据库
【发布时间】:2020-03-10 08:46:12
【问题描述】:

我已在网站上阅读了有关此问题的各种信息,但我无法弄清楚。我正在为此应用程序使用预构建数据库。我正在为这个应用程序使用 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;
    }
    */
}

【问题讨论】:

  • 请提供更多信息。特别是,你做了什么导致这个错误?
  • 我在模拟器上运行它并且它崩溃了。不幸的是,应用程序已停止。
  • 我没有使用 SQLiteAssetHelper
  • 我在运行 ActivityInstrumentationTestCase2 时遇到了同样的问题
  • 如果你在onCreate中创建了一个数据库呢?

标签: android android-sqlite


【解决方案1】:

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

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

【讨论】:

  • 哦!完全忘记了这个!!非常感谢
  • 注意:使用android marshmallow,你必须先实现一个权限请求,因为读写外部存储是一个危险的权限。更多信息:developer.android.com/training/permissions/requesting.html
  • 路径是“/data/data/com.example.quotes/databases”内部存储,为什么需要external_storage权限?
  • 为什么它的投票率如此之高? @nAkhmedov 是对的,我们不需要许可。 Felix Edelmann 说我们需要从 Android M 开始的请求权限。在这种情况下,每个应用程序都会在第一次启动时请求读/写存储权限
  • 您必须在第一次获得许可。除此之外,上述权限必须在清单文件中。
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    如果您在 Android 的 Marshmallow 或更高版本(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;
        }
    }
    

    这里有很好的学习资源requesting-runtime-permissions-in-android-marshmallow/

    【讨论】:

    • 我应该在代码的哪一部分请求权限?
    • 根据developer.android.com/reference/android/…,“从 API 级别 19 开始,读取/写入应用程序特定目录中的文件不需要此权限”。那么数据库不是在应用程序特定区域中创建的吗?
    【解决方案4】:

    这可能是由错误的权限或 SELinux 引起的。确保正确设置权限和所有者/组。然后运行:

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

    restorecon 恢复文件默认 SELinux 安全上下文

    【讨论】:

    • Android 7.0 需要 -F 标志才能生效,直到它起作用。 restorecon -F
    【解决方案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";
    

    另外,您应该在 Main 活动中打开数据库,并分别使用 database.close()database.open() 实现 onPause()onResume() 方法。这是我的 Main Activity 示例(与 Cordova、网页设计和所有东西一起使用 =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();
        }
    }
    

    祝你好运!!!

    【讨论】:

      【解决方案6】:

      DB_PATH 指向不同的数据库。在数据库助手类和我的代码工作中更改它。

      private static String DB_PATH = "/data/data/com.example.abc";
      

      【讨论】:

        【解决方案7】:

        @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
        

        【讨论】:

          【解决方案8】:

          我认为这不是您的问题,但对我来说仍然很糟糕。您的项目中有一个重复的 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 下。

          我不确定这是否会导致您的问题。但它看起来有点乱。你所有的其他东西在我看来都很标准。

          【讨论】:

            【解决方案9】:

            我对这个问题的小毛病是错误的目录权限:

            良好的权限:

            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,如果权限错误产生了有意义的错误消息,或者如果您不必手动调整权限来使用这些工具,那将是非常好的。

            【讨论】:

              【解决方案10】:

              另外需要注意的是Environment.getExternalStorageDirectory() 在 API 29 中已被弃用,因此如果您使用它来获取数据库路径,请更改此设置:

              https://developer.android.com/reference/android/os/Environment#getExternalStorageDirectory()

              此方法在 API 级别 29 中已弃用。

              为了改善用户隐私,不推荐直接访问共享/外部存储设备。 当应用以 Build.VERSION_CODES.Q 为目标时,从此方法返回的路径不再可供应用直接访问。通过迁移到 Context#getExternalFilesDir(String)、MediaStore 或 Intent#ACTION_OPEN_DOCUMENT 等替代方案,应用可以继续访问存储在共享/外部存储上的内容。

              【讨论】:

                【解决方案11】:

                我最近想出了这个错误。但更奇怪的是。我在 Android N 上工作,一切都很顺利,直到我在 JellyBeans 和 Lollipop 上测试它。在其中我不断收到相同的数据库错误。

                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 正常工作。

                【讨论】:

                • 我希望我能早点看到这篇文章,这样可以节省我 2 天的时间。
                • getContext().getApplicationInfo().dataDir+"/databases/"; 替换为new File(getContext().getApplicationInfo().dataDir, "databases").getAbsolutePath(); 将处理添加必要的路径分隔符。
                【解决方案12】:

                在运行时出现此类问题,如果您使用的是 android studio,请先打开 logcat,尝试分析跟踪树,从异常开始上升的地方开始,因为这通常是问题的根源。现在检查两件事:

                1. 在设备文件资源管理器(右下角)中检查存在您创建的数据库。 大多数情况下,您可以在 DATA -> DATA -> com.example.hpc.demo(your pakage name) -> DATABASE -> demo.db

                2. 中找到它
                3. 检查您是否在帮助程序类中添加了必需的“/”,例如如下所示
                  DB_location = "data/data/" + mcontext.getPackageName() + "/database/";

                【讨论】:

                  【解决方案13】:

                  在 OpenDatabase 之前添加这行:

                  File outFile = new File(Environment.getDataDirectory(), outFileName);
                  outFile.setWritable(true);
                  SQLiteDatabase.openDatabase(outFile.getAbsolutePath(), null, SQLiteDatabase.OPEN_READWRITE);
                  

                  【讨论】:

                    【解决方案14】:

                    正如@CommonsWare 所提到的,你会想试试android sqlite asset helper。它让我打开一个预先存在的数据库变得轻而易举。

                    在花了 3 个小时尝试手动完成所有操作后,我确实在大约半小时内完成了它。有趣的是,我以为我在做图书馆为我做的同样的事情,但是缺少了一些东西!

                    【讨论】:

                      【解决方案15】:

                      清除应用程序数据对我有帮助。它可能会帮助你..

                      【讨论】:

                        【解决方案16】:

                        Android 应用程序数据库的默认系统路径是 /data/data/YOUR_PACKAGE/databases/YOUR_DB_NAME

                        您的 logcat 清楚地表明 无法打开数据库'/data/data/com.example.quotes/databasesQuotesdb'

                        这意味着给定路径上不存在文件或您为数据文件提供了错误的路径。如我所见,数据库文件夹后应该有“/”。

                        您的 DB_PATH 变量应以“/”结尾。

                        私有静态字符串 DB_PATH = "/data/data/com.example.quotes/databases/";

                        您的正确路径将是现在 "/data/data/com.example.quotes/databases/Quotesdb"

                        【讨论】:

                          【解决方案17】:

                          你必须使用这种方式作为路径:

                          DB_PATH=  context.getDatabasePath(DB_NAME).getPath();
                          

                          【讨论】:

                            【解决方案18】:

                            android.database.sqlite.SQLiteCantOpenDatabaseException:未知错误(代码 14):无法打开数据库

                            如果您在使用设备文件资源管理器替换数据库文件后出现此问题,则有时会由于权限和 SELinux 安全上下文而发生这种情况。设备文件资源管理器(从 AS3.6.3 开始)将上传它们,并为所有 (777) 设置 root 权限/rwxrwxrwx。在您的应用程序文件夹中查找用于您的应用程序的用户 ID。在终端执行以下命令:

                               adb devices
                               emulator-5554
                               adb root
                               adb -s emulator-5554 shell
                               cd /data/data/com.yourorg.yourapp
                               ls -l
                            

                            您将获得如下列表:

                            drwxrwx--x 2 u0_a85 u0_a85 4096 2020-04-23 16:09 cache
                            drwxrwx--x 2 u0_a85 u0_a85 4096 2020-04-23 16:09 code_cache
                            drwxrwx--x 2 u0_a85 u0_a85 4096 2020-04-23 16:09 databases
                            drwxrwx--x 2 u0_a85 u0_a85 4096 2020-04-23 16:09 shared_prefs
                            

                            u0_a85(或任何可能)将是应用程序所有者和组 ID。 删除设备上的数据库,使用设备文件资源管理器放入替换数据库文件,然后在终端中执行以下操作(记住所有者 ID):

                            cd databases
                            chown u0_a85 *.db; chgrp u0_a85 *.db; chmod 600 *.db; restorecon *.db
                            

                            这会将数据库文件更改为相应的所有者/组 ID、RW 权限,并重置这些文件的 SELinux 安全上下文。尽管我已经成功地替换了不必这样做的数据库文件,但这个问题似乎是随机发生的,因此这样做很有效。

                            【讨论】:

                              【解决方案19】:

                              我作为用户而不是开发人员遇到了这个问题。并且在我的情况下原因不同。

                              https://del.dog/ufinunacul.txt
                              原来data 文件夹添加了一个损坏的link 指向我存储app data 的存储卡第二个分区 删除 broken link 一切都很好

                              【讨论】:

                                【解决方案20】:

                                这是因为您在初始化 SQLitedatabase 对象时没有外部存储写入权限。即

                                db2=SQLiteDatabase.openOrCreateDatabase(DBPATH, null);
                                

                                这可能是因为您正在请求权限,但您现在尚未允许权限,但数据库实例已初始化。请记住,创建数据库文件需要外部存储权限。只有在您允许后才能进行所有写操作。您可以在 onRequestPermissionsResult 中执行此操作。

                                【讨论】:

                                  【解决方案21】:

                                  请确保您没有尝试从主线程或后台线程重复打开和关闭数据库。

                                  在您的应用程序中创建一个单例类,并尝试仅从该类创建和打开数据。

                                  保证只有在数据库不存在时才会调用数据库。

                                  在您的整个应用程序中,在需要时使用获取 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;        
                                      }      
                                  }    
                                  

                                  【讨论】:

                                    猜你喜欢
                                    • 1970-01-01
                                    • 1970-01-01
                                    • 1970-01-01
                                    • 2015-02-16
                                    • 1970-01-01
                                    • 1970-01-01
                                    • 2014-04-28
                                    • 1970-01-01
                                    相关资源
                                    最近更新 更多