SQLite database: unknown error (code 14): Could not open database
Asked Answered
B

3

6

I'm making an App that copies a db file from another App and then edits the db.

When I try to open de SQLite db I get an error:

android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database

Tried a lot of different approaches I saw on the INTERNET and nothing works.

Here is my code:

public class FirstRunService extends IntentService{
    /**
     * Creates an IntentService.  Invoked by your subclass's constructor.
     *
     * Service Used to name the worker thread, important only for debugging.
     */
    public FirstRunService() {
        super("Service");
    }
    String filesdir;
    SharedPreferences sp;
    @Override
    protected void onHandleIntent(Intent intent) {
        //Cargo SharedPreferences
        sp = this.getSharedPreferences("FirstRun", Context.MODE_PRIVATE);
        //Asigno filesdir
        filesdir = intent.getStringExtra("filesdir");
        //Subo XX
        Boolean XX = sp.getBoolean("XX",false);
        if(!XX) {
            uploadxxs(intent);
        }


    }

    protected void uploadXX (Intent intent){
        //Preparo
        SQLiteDatabase db;
        myFTP myftp = new myFTP();
        FTPClient client;

        //Copio la Base de datos

        RootTools.deleteFileOrDirectory(filesdir + "/XX.db", true);
        RootTools.copyFile("/data/data/com.XX/databases/XX.db", filesdir+"/xx.db",true,false);
        try {
            RootTools.getShell(true).add(cmd);
        } catch (TimeoutException e) {
            e.printStackTrace();
            Log.e("Root XX.db","Fallo");
            FirstRunAlarm.completeWakefulIntent(intent);
            stopService(intent);
        } catch (RootDeniedException e) {
            e.printStackTrace();
            Log.e("Root XX.db","Fallo");
            FirstRunAlarm.completeWakefulIntent(intent);
            stopService(intent);
        } catch (IOException e) {
            e.printStackTrace();
            Log.e("Root XX.db","Fallo");
            FirstRunAlarm.completeWakefulIntent(intent);
            stopService(intent);
        }
        //Edito db
           //Edito db
    File file = new File(filesdir+"/wa.db");
    if (file.exists()){
        Log.e("File","Existe");
        db = SQLiteDatabase.openDatabase(filesdir+"/wa.db", null,SQLiteDatabase.OPEN_READWRITE);//Error in this line
    }else{
        db = null;
        Log.e("File","No Existe");
    }
        db.execSQL("DROP TABLE android_metadata;");

Logcat:

12-17 22:38:17.401  15227-15242/com.lstupenengo.primeraapp E/File﹕ Existe
12-17 22:38:17.402    3076-3366/com.lstupenengo.primeraapp E/SQLiteLog﹕ (14) cannot open file at line 30192 of [00bb9c9ce4]
12-17 22:38:17.402    3076-3366/com.lstupenengo.primeraapp E/SQLiteLog﹕ (14) os_unix.c:30192: (13) open(//data/data/com.lstupenengo.primeraapp/files/XX.db) -
12-17 22:38:17.412    3076-3366/com.lstupenengo.primeraapp E/SQLiteDatabase﹕ Failed to open database 'data/data/com.lstupenengo.primeraapp/files/XX.db'.
    android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
            at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
            at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209)
            at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
            at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
            at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
            at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
            at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:804)
            at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:789)
            at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
            at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:669)
            at com.lstupenengo.primeraapp.FirstRunService.uploadXX(FirstRunService.java:105)
            at com.lstupenengo.primeraapp.FirstRunService.onHandleIntent(FirstRunService.java:49)
            at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:212)
            at android.os.HandlerThread.run(HandlerThread.java:61)
12-17 22:38:17.412    3076-3366/com.lstupenengo.primeraapp W/dalvikvm﹕ threadid=11: thread exiting with uncaught exception (group=0x41648d88)
12-17 22:38:17.422    3076-3366/com.lstupenengo.primeraapp E/AndroidRuntime﹕ FATAL EXCEPTION: IntentService[Service]
    Process: com.lstupenengo.primeraapp, PID: 3076
    android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
            at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
            at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209)
            at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
            at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
            at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
            at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
            at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:804)
            at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:789)
            at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
            at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:669)
            at com.lstupenengo.primeraapp.FirstRunService.uploadXX(FirstRunService.java:105)
            at com.lstupenengo.primeraapp.FirstRunService.onHandleIntent(FirstRunService.java:49)
            at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:212)
            at android.os.HandlerThread.run(HandlerThread.java:61)

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.lstupenengo.primeraapp" >
    <uses-permission android:name="android.permission.WAKE_LOCK"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_SUPERUSER"/>
    <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=".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>
        <receiver android:name=".FirstRunAlarm"/>
        <service android:name=".FirstRunService"/>
    </application>


</manifest>
Berthold answered 18/12, 2014 at 3:52 Comment(8)
Try checking if the file gets created or if it exists.. and also add WRITE_EXTERNAL_STORAGE PERMISSION in the manifestDunne
Added manifest so you can read, I have permissions. Tried a if (file.exist) and the file exists. Any other ideas?Berthold
could you refer to this ? it says the super method shud reference the path correctly.Dunne
Added more code for you to see if this is the case. I realy don't know how to reference in Super the db when inside a service.Berthold
What we generally do is create a class that extends Sqliteopenhelper.. in it the super method gets called... so then all the copying uv done can be done there, and all u have to do is using the obj of this class call a method inside it from a service..Dunne
Do you have an example of this? I get the idea but have quite a hard time imagining how to code this.Berthold
yes, you could get an idea from this and also thisDunne
Could not open database: Simply the file you download is not a valid SQLite database. Or it's encrypted.Lentamente
B
3

Solved by creating a class, then calling that class directly from the service.

Class

public class xxHandler extends SQLiteOpenHelper {

    Context context;
    SQLiteDatabase xxdb;

    public wadbHandler(Context context1) {
        super(context1,"xx.db", null, 1);
        context = context1;
    }

    // Creating Tables
    @Override
    public void onCreate(SQLiteDatabase db) {

    }

    // Upgrading database
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    public void accion(SQLiteDatabase db){
        wadb = SQLiteDatabase.openDatabase(context.getFilesDir()+"/xx.db", null,
                SQLiteDatabase.OPEN_READWRITE);
        wadb.execSQL("SELECT * FROM table;")

     }
Berthold answered 19/12, 2014 at 13:32 Comment(0)
E
2

First check whether database file exist or not use:

File file = new File(myPath);    
if (file.exists() && !file.isDirectory())
Extracellular answered 18/12, 2014 at 4:59 Comment(1)
I've added the line an same error comes up. File exist returns TRUEBerthold
A
0

Add before OpenDatabase:

file.setWritable(true);
Ashby answered 3/12, 2015 at 10:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.