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

时间:2013-06-10 23:40:51

标签: android android-sqlite

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

21 个答案:

答案 0 :(得分:35)

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

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

答案 1 :(得分:25)

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

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

答案 2 :(得分:17)

如果您在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/

答案 3 :(得分:11)

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

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

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

答案 4 :(得分: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活动中打开数据库,并分别使用onPause()onResume()实施database.close()database.open()方法。这是我的主要活动的例子(与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();
    }
}

祝你好运!!!

答案 5 :(得分:4)

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

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

答案 6 :(得分:3)

@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

使用以下命令设置正确的所有权和组。 u0_a80我从ls -al获取了目录

中的其他文件
chown u0_a80 test.sqlite
chgrp u0_a80 test.sqlite

答案 7 :(得分:3)

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

我不确定这是否会导致您的问题。但它看起来有点搞砸了。所有其他东西对我来说都很标准。

答案 8 :(得分:2)

我的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工作。显然我做错了。

嘿谷歌,如果权限错误产生了有意义的错误消息,那将是非常好的,或者如果您不必手动调整权限以使用这些工具,则会失败。

答案 9 :(得分:1)

要注意的另一件事是,API 29中不推荐使用Rectangle activeScreenDimensions = Screen.FromControl(this).Bounds; this.Size = new Size(activeScreenDimensions.Width + activeScreenDimensions.X, activeScreenDimensions.Height + activeScreenDimensions.Y); ,因此,如果要使用它来获取数据库路径,请进行以下更改:

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

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

为了提高用户隐私,不建议直接访问共享/外部存储设备。 当应用程序定位到Build.VERSION_CODES.Q时,此方法返回的路径将不再可供应用程序直接访问。通过迁移到Context#getExternalFilesDir(String),MediaStore或Intent#ACTION_OPEN_DOCUMENT之类的替代方案,应用程序可以继续访问共享/外部存储中存储的内容。

答案 10 :(得分:1)

我最近想出了这个错误。但它更奇怪。我正在使用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。

答案 11 :(得分:1)

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

  1. 签入设备文件浏览器(在右下角),存在一个由您创建的数据库。 大多数情况下,您可以在DATA-> DATA-> com.example.hpc.demo(您的包装名称)-> DATABASE-> demo.db

  2. 中找到它
  3. 检查您的帮助器类中是否添加了必需的“ /”,例如下面的
    DB_location =“数据/数据/” + mcontext.getPackageName()+“ /数据库/”;

答案 12 :(得分:0)

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

db2=SQLiteDatabase.openOrCreateDatabase(DBPATH, null);

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

答案 13 :(得分:0)

我作为用户而不是开发人员遇到了这个问题。和我的情况不同的原因。
https://del.dog/ufinunacul.txt
原来,data 文件夹添加了一个损坏的 link ,指向我存储 app data 的存储卡第二个分区 在删除 broken link 一切都很好

答案 14 :(得分:0)

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

如果使用设备文件资源管理器替换数据库文件后得到此消息,则有时由于权限和SELinux安全上下文而发生这种情况。设备文件资源管理器(自AS3.6.3起)将使用所有用户的root权限/ rwxrwxrwx设置上传它们(777)。在您的应用程序文件夹中四处寻找用于您的应用程序的用户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安全上下文。尽管我已经成功地替换了数据库文件,而不必这样做,但是这个问题似乎随机发生了,因此可以正常工作。

答案 15 :(得分:0)

您必须对路径使用这种方式:

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

答案 16 :(得分:0)

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 :(得分:0)

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

答案 18 :(得分:0)

如@CommonsWare所述,您需要尝试android sqlite asset helper。它为我打开一个预先存在的数据库是一块蛋糕。

我花了3个小时尝试全部手动完成后,大约半小时内完成了工作。有趣的是,我以为我在做同样的事情,图书馆为我做了,但有些东西丢失了!

答案 19 :(得分:0)

在OpenDatabase之前添加以下行:

#include <stdint.h>
...
uint8_t v[3];    /* Array to store a 24 bit value byte-by-byte. */
int32_t value;   /* Converted 32-bit value in machine endianness. */

if (fread (v, 1, sizeof v, fp1) == sizeof v) {
    /* Convert big-endian to machine endianness. */
    value = (v[0] << 16) + (v[1] << 8) + v[2];
    /* Sign extend when negative. */
    if (value & 0x00800000u)
        /* Fancy, non-undefined way to compute value |= 0xFF000000. */
        value = -((value ^ 0xFFFFFFu) + 1);
} else {
   /* fread found eof or error */
}

答案 20 :(得分:-1)

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

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

保证只有在数据库打开调用不存在时才进行数据库打开调用。

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