Database from a file.

Hi
I have a problem with loading the database from asset folder. He writes me that the database does not exist. Do you know where is the problem?

This is the file AssetDatabaseOpenHelper
(I downloaded it here: https://gist.github.com/donaldlittlepie/1271795 )


package com.example.dbt7;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

public class AssetDatabaseOpenHelper {

    private static final String DB_NAME = "data/data/com.example.dbt7/pokus.sqlite";

    private Context context;

    public AssetDatabaseOpenHelper(Context context) {
        this.context = context;
    }

    public SQLiteDatabase openDatabase() {
        File dbFile = context.getDatabasePath(DB_NAME);
        

        if (!dbFile.exists()) {
           try {
                copyDatabase(dbFile);
            } catch (IOException e) {
                throw new RuntimeException("Error creating source database", e);
            }
        }

        return SQLiteDatabase.openDatabase(dbFile.getPath(), null, SQLiteDatabase.OPEN_READONLY);
    }

    private void copyDatabase(File dbFile) throws IOException {
        InputStream is = context.getAssets().open(DB_NAME);
        OutputStream os = new FileOutputStream(dbFile);

        byte[] buffer = new byte[1024];
        while (is.read(buffer) > 0) {
            os.write(buffer);
        }

        os.flush();
        os.close();
        is.close();
    } 
}

I changed it:

private static final String DB_NAME = "pokus.sqlite";

for

private static final String DB_NAME = "data/data/com.example.dbt7/pokus.sqlite";

but neither does not work

The error is on this line (by stepping in eclipse):

 File dbFile = context.getDatabasePath(DB_NAME);

And here I call the main activity (MainActivity.java) and AssetDOH.


package com.example.dbt7;

import android.os.Bundle;
import android.app.Activity;


public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		
		
		AssetDatabaseOpenHelper otevreno = new AssetDatabaseOpenHelper(this);
		otevreno.openDatabase();
		
		
	}
}

And there is an error in the console


07-01 01:25:15.517: E/AndroidRuntime(32039): FATAL EXCEPTION: main
07-01 01:25:15.517: E/AndroidRuntime(32039): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dbt7/com.example.dbt7.MainActivity}: java.lang.RuntimeException: Error creating source database
07-01 01:25:15.517: E/AndroidRuntime(32039):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
07-01 01:25:15.517: E/AndroidRuntime(32039):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-01 01:25:15.517: E/AndroidRuntime(32039):    at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-01 01:25:15.517: E/AndroidRuntime(32039):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-01 01:25:15.517: E/AndroidRuntime(32039):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-01 01:25:15.517: E/AndroidRuntime(32039):    at android.os.Looper.loop(Looper.java:123)
07-01 01:25:15.517: E/AndroidRuntime(32039):    at android.app.ActivityThread.main(ActivityThread.java:4627)
07-01 01:25:15.517: E/AndroidRuntime(32039):    at java.lang.reflect.Method.invokeNative(Native Method)
07-01 01:25:15.517: E/AndroidRuntime(32039):    at java.lang.reflect.Method.invoke(Method.java:521)
07-01 01:25:15.517: E/AndroidRuntime(32039):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-01 01:25:15.517: E/AndroidRuntime(32039):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-01 01:25:15.517: E/AndroidRuntime(32039):    at dalvik.system.NativeStart.main(Native Method)
07-01 01:25:15.517: E/AndroidRuntime(32039): Caused by: java.lang.RuntimeException: Error creating source database
07-01 01:25:15.517: E/AndroidRuntime(32039):    at com.example.dbt7.AssetDatabaseOpenHelper.openDatabase(AssetDatabaseOpenHelper.java:29)
07-01 01:25:15.517: E/AndroidRuntime(32039):    at com.example.dbt7.MainActivity.onCreate(MainActivity.java:17)
07-01 01:25:15.517: E/AndroidRuntime(32039):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-01 01:25:15.517: E/AndroidRuntime(32039):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
07-01 01:25:15.517: E/AndroidRuntime(32039):    ... 11 more
07-01 01:25:15.517: E/AndroidRuntime(32039): Caused by: java.io.FileNotFoundException: /data/data/com.example.dbt7/databases/dbm.db (No such file or directory)
07-01 01:25:15.517: E/AndroidRuntime(32039):    at org.apache.harmony.luni.platform.OSFileSystem.openImpl(Native Method)
07-01 01:25:15.517: E/AndroidRuntime(32039):    at org.apache.harmony.luni.platform.OSFileSystem.open(OSFileSystem.java:152)
07-01 01:25:15.517: E/AndroidRuntime(32039):    at java.io.FileOutputStream.<init>(FileOutputStream.java:97)
07-01 01:25:15.517: E/AndroidRuntime(32039):    at java.io.FileOutputStream.<init>(FileOutputStream.java:69)
07-01 01:25:15.517: E/AndroidRuntime(32039):    at com.example.dbt7.AssetDatabaseOpenHelper.copyDatabase(AssetDatabaseOpenHelper.java:38)
07-01 01:25:15.517: E/AndroidRuntime(32039):    at com.example.dbt7.AssetDatabaseOpenHelper.openDatabase(AssetDatabaseOpenHelper.java:27)
07-01 01:25:15.517: E/AndroidRuntime(32039):    ... 14 more