Opening SQLite database file in android
<p>I am developing an android app in which i have to download the sqlite database file from the server and have to fetch information from it. For server, currently i am using my dropbox account and i am performing the downloading and opening task to open database file in class AsyncTask. In doinbackground, i am downloading the database file and in postExecute ,i am opening the downloaded database. But when I execute the application, it successfully downloads the database file and shows error when the postExecute method is called. i.e when i try to open the database file. </p>

<p>This is my MainActivity.</p>

<pre><code>package com.example.avinash.sqlite_re2;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.Button;

public class MainActivity extends Activity {
    private SQLiteDatabase db;
    private Cursor c;

    private static String file_url = "https://www.dropbox.com/s/k0de92qc8hx2v6t/password.db?dl=0";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        DownloadFileFromUrl download = new DownloadFileFromUrl(MainActivity.this);
        download.execute(file_url);

    }
}
</code></pre>

<p>this is my AsyncTask class downloadFileFromUrl</p>

<pre><code>package com.example.avinash.sqlite_re2;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;


/**
 * Created by Avinash on 21-04-2016.
 */
public class DownloadFileFromUrl extends AsyncTask &lt;String, String, String&gt; {

    private SQLiteDatabase db;
    private Cursor c;
    Context context;
    DownloadFileFromUrl(Context context)
    {
        this.context= context;
    }
    File ParentDirectory = new File("data/data/com.example.avinash.sqlite_re2" + "/files/");
    File outputFile = new File(ParentDirectory, "data.db");

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        if(!ParentDirectory.exists()) {
            ParentDirectory.mkdir();
            Toast.makeText(context, "Creating folder", Toast.LENGTH_SHORT).show();
        }
        else
        {
            Toast.makeText(context, "folder exist", Toast.LENGTH_SHORT).show();
        }

    }
    @Override

    protected String doInBackground(String... f_url) {
        int count;
        try {

            URL url = new URL(f_url[0]);
            URLConnection conection = url.openConnection();
            conection.connect();

            InputStream input = new BufferedInputStream(url.openStream(), 8192);
            OutputStream output = new FileOutputStream(outputFile);

            byte data[] = new byte[1024];


            while ((count = input.read(data)) != -1) {
                output.write(data, 0, count);
            }

            // flushing output
            output.flush();

            // closing streams
            output.close();
            input.close();

        } catch (Exception e) {
            Log.e("Error: ", e.getMessage());
        }

        return null;
    }
    @Override
    protected void onPostExecute(String file_url) {



       String path = "data/data/com.example.avinash.sqlite_re2" + "/files/";
        String name = "data.db";
        Toast.makeText(context , " is there ", Toast.LENGTH_SHORT).show();
        db = SQLiteDatabase.openDatabase(path + name, null, 0);
    }
</code></pre>

<p>This is My Manifest File</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.avinash.sqlite_re2"&gt;

    &lt;application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"&gt;

        &lt;activity android:name=".MainActivity"&gt;
            &lt;intent-filter&gt;
                &lt;action android:name="android.intent.action.MAIN" /&gt;

                &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
            &lt;/intent-filter&gt;
        &lt;/activity&gt;
    &lt;/application&gt;
        &lt;uses-permission android:name="android.permission.INTERNET"/&gt;
        &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt;
        &lt;uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /&gt;

&lt;/manifest&gt;


    }
</code></pre>

<p>I have already taken care of these things:-
1. Internet Permission
2. Read / Write Permission
3.Primary Key in my database is named  _id.
4.I have created the table android_metadata in my database.
I am getting the following error</p>

<pre><code>04-26 23:30:26.999 31015-31015/? D/dalvikvm: Late-enabling CheckJNI
04-26 23:30:26.999 31015-31015/? D/dalvikvm: Try to disable coredump for pid 31015
04-26 23:30:26.999 31015-31015/? D/dalvikvm: Process 31015 nice name: com.example.avinash.sqlite_re2
04-26 23:30:26.999 31015-31015/? D/dalvikvm: Extra Options: not specified
04-26 23:30:27.229 31015-31015/com.example.avinash.sqlite_re2 E/IMGSRV: :0: PVRDRMOpen: TP3, ret = 46
04-26 23:30:27.239 31015-31015/com.example.avinash.sqlite_re2 E/IMGSRV: :0: PVRDRMOpen: TP3, ret = 50
04-26 23:30:27.239 31015-31015/com.example.avinash.sqlite_re2 E/IMGSRV: :0: PVRDRMOpen: TP3, ret = 51
04-26 23:30:27.249 31015-31015/com.example.avinash.sqlite_re2 E/IMGSRV: :0: PVRDRMOpen: TP3, ret = 51
04-26 23:30:27.249 31015-31015/com.example.avinash.sqlite_re2 E/IMGSRV: :0: PVRDRMOpen: TP3, ret = 51
04-26 23:30:27.259 31015-31015/com.example.avinash.sqlite_re2 E/IMGSRV: :0: PVRDRMOpen: TP3, ret = 53
04-26 23:30:27.289 31015-31015/com.example.avinash.sqlite_re2 D/OpenGLRenderer: Enabling debug mode 0
04-26 23:30:29.569 31015-31023/com.example.avinash.sqlite_re2 I/dalvikvm: Total arena pages for JIT: 11
04-26 23:30:29.569 31015-31023/com.example.avinash.sqlite_re2 I/dalvikvm: Total arena pages for JIT: 12
04-26 23:30:29.589 31015-31023/com.example.avinash.sqlite_re2 I/dalvikvm: Total arena pages for JIT: 13
04-26 23:30:31.499 31015-31015/com.example.avinash.sqlite_re2 E/SQLiteLog: (26) file is encrypted or is not a database
04-26 23:30:31.499 31015-31015/com.example.avinash.sqlite_re2 E/DefaultDatabaseErrorHandler: Corruption reported by sqlite on database: data/data/com.example.avinash.sqlite_re2/files/data.db
04-26 23:30:31.499 31015-31015/com.example.avinash.sqlite_re2 E/DefaultDatabaseErrorHandler: deleting the database file: data/data/com.example.avinash.sqlite_re2/files/data.db
04-26 23:30:31.499 31015-31015/com.example.avinash.sqlite_re2 E/SQLiteLog: (14) cannot open file at line 30175 of [000197cc4e]
04-26 23:30:31.499 31015-31015/com.example.avinash.sqlite_re2 E/SQLiteLog: (14) os_unix.c:30175: (2) open(//data/data/com.example.avinash.sqlite_re2/files/data.db) - 
04-26 23:30:31.509 31015-31015/com.example.avinash.sqlite_re2 E/SQLiteDatabase: Failed to open database 'data/data/com.example.avinash.sqlite_re2/files/data.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:792)
                                                                                    at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
                                                                                    at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:669)
                                                                                    at com.example.avinash.sqlite_re2.DownloadFileFromUrl.onPostExecute(DownloadFileFromUrl.java:88)
                                                                                    at com.example.avinash.sqlite_re2.DownloadFileFromUrl.onPostExecute(DownloadFileFromUrl.java:22)
                                                                                    at android.os.AsyncTask.finish(AsyncTask.java:632)
                                                                                    at android.os.AsyncTask.access$600(AsyncTask.java:177)
                                                                                    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                    at android.os.Looper.loop(Looper.java:149)
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:5257)
                                                                                    at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                    at java.lang.reflect.Method.invoke(Method.java:515)
                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
                                                                                    at dalvik.system.NativeStart.main(Native Method)
04-26 23:30:31.509 31015-31015/com.example.avinash.sqlite_re2 D/AndroidRuntime: Shutting down VM
04-26 23:30:31.509 31015-31015/com.example.avinash.sqlite_re2 W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x430f2140)
04-26 23:30:31.509 31015-31015/com.example.avinash.sqlite_re2 E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                Process: com.example.avinash.sqlite_re2, PID: 31015
                                                                                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:792)
                                                                                    at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
                                                                                    at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:669)
                                                                                    at com.example.avinash.sqlite_re2.DownloadFileFromUrl.onPostExecute(DownloadFileFromUrl.java:88)
                                                                                    at com.example.avinash.sqlite_re2.DownloadFileFromUrl.onPostExecute(DownloadFileFromUrl.java:22)
                                                                                    at android.os.AsyncTask.finish(AsyncTask.java:632)
                                                                                    at android.os.AsyncTask.access$600(AsyncTask.java:177)
                                                                                    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                    at android.os.Looper.loop(Looper.java:149)
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:5257)
                                                                                    at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                    at java.lang.reflect.Method.invoke(Method.java:515)
                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
                                                                                    at dalvik.system.NativeStart.main(Native Method)
</code></pre>