2

In my app, I tried to open existing android database for modifying its content by following code:

    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"

my 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!

Toan Tran Van
  • 695
  • 1
  • 7
  • 25

1 Answers1

1

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,

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

  • I did it, now PRAGMA journal_mode is not shown, But still error: "unknown error (code 14): Could not open database :| – Toan Tran Van Jun 16 '14 at 09:40
  • whether the db path and the db name is coorect & check whether the db is existing in the device path –  Jun 16 '14 at 09:58
  • @Achuthan I checked, db path and db name are correct and db is exist. I have rooted deivce, so I can check /data/data folder – Toan Tran Van Jun 17 '14 at 01:26
  • Thank for all your help. BUT, this error may comes from kernel since i saw this in Logcat "(14) os_unix.c:30241: " This issue may can not be fixed. Thank you anyway!!! – Toan Tran Van Jun 18 '14 at 16:59