74

我已经在网站上阅读了有关此问题的各种信息,但我无法弄清楚。我正在为此应用程序使用预构建数据库。我正在为这个应用程序使用果冻豆。

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

数据库助手.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;
    }
    */
}
4

21 回答 21

40

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

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
于 2013-11-05T06:32:42.757 回答
27

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

File dbFile = myContext.getDatabasePath(DB_NAME);
return dbFile.exists();
于 2013-07-24T23:52:53.373 回答
18

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

于 2016-01-18T14:22:22.790 回答
12

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

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

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

于 2016-04-16T06:36:29.633 回答
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()方法。这是我的主要活动示例(与 Cordova、网页设计和所有东西一起使用 =P):database.close()database.open()

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

祝你好运!!!

于 2013-10-11T23:05:01.533 回答
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

使用以下命令设置正确的所有权和组。u0_a80我从目录中的ls -al其他文件中得到的

chown u0_a80 test.sqlite
chgrp u0_a80 test.sqlite
于 2014-05-05T10:53:32.377 回答
4

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

private static String DB_PATH = "/data/data/com.example.abc";
于 2013-08-06T20:20:58.327 回答
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 下。

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

于 2013-06-11T05:47:56.667 回答
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上班。显然我做错了。

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

于 2014-01-11T22:39:20.297 回答
2

需要注意的另一件事是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 等替代方案,应用程序可以继续访问存储在共享/外部存储上的内容。

于 2020-02-16T15:39:47.423 回答
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 可以正常工作。

于 2018-01-12T04:17:04.323 回答
1

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

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

  2. 检查您的助手类中是否添加了必需的'/',例如如下
    DB_location = "data/data/" + mcontext.getPackageName() + "/database/";

于 2018-09-16T11:38:18.870 回答
0

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

DB_PATH=  context.getDatabasePath(DB_NAME).getPath();
于 2018-12-13T20:33:15.680 回答
0

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

db2=SQLiteDatabase.openOrCreateDatabase(DBPATH, null);

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

于 2021-03-25T06:22:41.840 回答
0

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

您的 logcat 清楚地说 Failed to open database '/data/data/com.example.quotes/databasesQuotesdb'

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

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

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

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

于 2018-09-05T20:06:16.677 回答
0

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 安全上下文。尽管我已经成功地替换了不必这样做的数据库文件,但这个问题似乎是随机发生的,以至于这样做是可行的。

于 2020-04-23T21:50:25.077 回答
0

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

https://del.dog/ufinunacul.txt
事实证明,该 data 文件夹添加了一个损坏的link 指向我的存储卡第二个分区,我app data 在其中存储删除 broken link一切都很好

于 2021-03-22T07:24:13.177 回答
0

正如@CommonsWare 所提到的,您将想要尝试android sqlite 资产助手。它使打开一个预先存在的数据库对我来说是小菜一碟。

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

于 2016-01-06T18:08:00.807 回答
0

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

于 2016-02-17T15:37:51.047 回答
0

在 OpenDatabase 之前添加以下行:

File outFile = new File(Environment.getDataDirectory(), outFileName);
outFile.setWritable(true);
SQLiteDatabase.openDatabase(outFile.getAbsolutePath(), null, SQLiteDatabase.OPEN_READWRITE);
于 2015-12-03T10:20:41.123 回答
-1

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

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

是保证您仅在数据库不存在时才进行数据库打开调用。

在您的整个应用程序中,在需要时使用获取 sqLiteDatabase 对象的相同方法。

我使用了下面的代码,我的问题在 1.5 天后得到了解决。

...............................................................

在您的活动类 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;        
    }      
}    
于 2016-09-30T15:03:32.657 回答