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

标签 android database-connection android-sqlite android-contentprovider

在我的应用程序中,我尝试通过以下代码打开现有的 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);

数据库路径正确,我还更改了数据库文件的权限。但是总是出现错误“无法打开数据库文件(代码14):,编译时:PRAGMA journal_mode”

我的 list .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>

我做错了什么吗?请帮忙! 谢谢!

最佳答案

不要提及数据库的扩展

试试这个,它会起作用。

String DB_NAME = "logs";

编辑:

将这个类添加到你的程序中,

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

然后调用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);

对我有用,当然对你也有用

关于android - 无法使用 SQLiteDatabase.openDatabase() 打开 android 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24238396/

相关文章:

php - 无法选择数据库

c# - 在底层 mysql 连接丢失后,有什么方法可以恢复(长)事务吗?

android - 更新了 Android SDK,现在无法将 apk 安装到真实设备

android - 在 Google Maps Android API V2 中从我的位置到多个目的地的行车路线

android - Runtime.getRuntime().exec() 的问题

java - 从 AsyncTask 实例快速更新 sqlite 数据库是否可取?我在服务中收到 ANR

android - 使用或不使用 (typeAffinity = ColumnInfo.BLOB) 将图像数据存储在 Room 数据库中

android - 如何使用导航体系结构组件向后或向上导航添加日志记录?

java - 当我已经有 JDBC 连接池时如何使用 Vaadin SQLContainer

java - 行未从 SQLite 数据库中删除