简体   繁体   English

无法使用SQLiteDatabase.openDatabase()打开android数据库

[英]Can not open android database using SQLiteDatabase.openDatabase()

In my app, I tried to open existing android database for modifying its content by following code: 在我的应用中,我尝试通过以下代码打开现有的android数据库以修改其内容:

    String DB_PATH = "/data/data/com.sec.android.provider.logsprovider/databases/";
    String DB_NAME = "logs.db";

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

  SQLiteDatabase mSqLiteDatabase = SQLiteDatabase.openDatabase(DB_PATH + DB_NAME, null, SQLiteDatabase.OPEN_READWRITE);

Path to database is correct, I also changed permission for database file. 数据库路径正确,我也更改了数据库文件的权限。 But there is always an error "Fail to open database file (code 14):, while compiling: PRAGMA journal_mode" 但是始终存在错误“在编译时无法打开数据库文件(代码14):: PRAGMA journal_mode”

my manifest.xml 我的manifest.xml

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.logsfiller"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="16" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.logsfiller.LogFillerActivity"
            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>

Did I make something wrong? 我做错什么了吗? Please help! 请帮忙! Thank you! 谢谢!

Don't mention the extension of the db 不要说数据库的扩展

Try this, it will work. 试试这个,它将起作用。

String DB_NAME = "logs";

EDIT : 编辑:

Add this class to your program, 将此类添加到您的程序中,

public class DBAdapter {

final Context context;
DatabaseHelper DBHelper;
SQLiteDatabase db;

public DBAdapter(Context ctx) {
    this.context = ctx;
    DBHelper = new DatabaseHelper(context);
}

private static class DatabaseHelper extends SQLiteOpenHelper {
    DatabaseHelper(Context context) {
        super(context, "logs", null, 1);
    }
}

public DBAdapter open(String path, String dbName) throws SQLException {
        db = DBHelper.getWritableDatabase();
        String destPath = path + "/" + dbName;
        db = db.openDatabase(destPath, null, 0);
        return this;
    }
}

and call open() method like, 然后像这样调用open()方法,

DBAdapter db = new DBAdapter(this);

String DB_PATH = "/data/data/com.sec.android.provider.logsprovider/databases/";
String DB_NAME = "logs";

db.open(DB_PATH, DB_NAME);

it works for me, surely it work for you 它对我有用,肯定对你有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM