1

I have a sqlite database, and i put this file in "assets" folder.

The code like below, Pls help and tell what's wrong in this code,

How to use my own sqlite database.

public class DataBaseHelper extends SQLiteOpenHelper { 

    private static String DB_PATH = "/data/data/com.SGMalls/databases/"; 

    private static String DB_NAME = "mallMapv2.sqlite"; 

    private SQLiteDatabase myDataBase; 

    private final Context myContext; 
    public DataBaseHelper(Context context) { 
            super(context, DB_NAME, null, 1); 
            this.myContext = context; 
    } 
    public void createDataBase() throws IOException { 
              File dbDir = new File(DB_PATH); 
            if (!dbDir.exists()) { 
                dbDir.mkdir(); 
            } 
            boolean dbExist = checkDataBase(); 
            if (dbExist) { 
            } else { 
                    this.getReadableDatabase(); 
                    try { 
                            copyDataBase(); 
                    } catch (IOException e) { 
                            throw new Error("Error copying database"); 
                    } 
            } 
            close(); 
    } 
    private boolean checkDataBase() { 
            SQLiteDatabase checkDB = null; 
     boolean isnull=false; 
            try { 
                    String myPath = DB_PATH + DB_NAME; 
                    checkDB = SQLiteDatabase.openDatabase(myPath, null, 
                                    SQLiteDatabase.OPEN_READONLY); 
            } catch (SQLiteException e) { 
                    // database does't exist yet. 
            } 
            if (checkDB != null) { 
       isnull=true; 
                    checkDB.close(); 
            } 
            return isnull; 
    } 
    private void copyDataBase() throws IOException { 
            InputStream myInput = myContext.getAssets().open(DB_NAME); 
            String outFileName = DB_PATH + DB_NAME; 
            OutputStream myOutput = new FileOutputStream(outFileName); 
            byte[] buffer = new byte[1024]; 
            int length; 
            while ((length = myInput.read(buffer)) > 0) { 
                    myOutput.write(buffer, 0, length); 
            } 
            // Close the streams 
            myOutput.flush(); 
            myOutput.close(); 
            myInput.close(); 
    } 
    public void openDataBase() throws SQLException { 
            // Open the database 
            String myPath = DB_PATH + DB_NAME; 
            myDataBase = SQLiteDatabase.openDatabase(myPath, null, 
                            SQLiteDatabase.OPEN_READONLY); 
    } 
    @Override 
    public synchronized void close() { 
            if (myDataBase != null) 
                    myDataBase.close(); 
            super.close(); 
    } 
    @Override 
    public void onCreate(SQLiteDatabase db) { 
    } 
    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 

    } 
 }

public class GetData {     

 private static String DB_PATH = "/data/data/com.SGMalls/databases/mallMapv2.sqlite"; 
        // private static String DB_NAME = "mallMapv2.sqlite"; 

    public static ArrayList<Mall> getMalls() { 

            ArrayList<Mall> mallsList = new ArrayList<Mall>(); 

            SQLiteDatabase malldatabase = SQLiteDatabase.openDatabase(DB_PATH, 
                            null, SQLiteDatabase.OPEN_READONLY); 

             String queryString="select id,title from malls order by title"; 

             Cursor cursor=malldatabase.rawQuery(queryString, null); 

              if(cursor!=null){ 
                       cursor.moveToFirst(); 
                       while(!cursor.isLast()){ 
                               Mall mall=new Mall(); 
                              mall.setMallid(cursor.getInt(0)); 
                               mall.setMallname(cursor.getString(0)); 
                               mallsList.add(mall); 
                               cursor.moveToNext(); 
                       } 
               } 
              malldatabase.close(); 
            return mallsList; 
    } 
} 

The error message:

ERROR/Database(725): sqlite3_open_v2("/data/data/com.SGMalls/databases/ mallMapv2.sqlite", &handle, 1, NULL) failed

03-15 22:34:11.747: ERROR/AndroidRuntime(725): Uncaught handler: thread main exiting due to uncaught exception

03-15 22:34:11.766: ERROR/AndroidRuntime(725): java.lang.Error: Error copying database

Thanks very much


The all log error message

03-16 16:19:18.871: ERROR/vold(550): Error opening switch name path 

'/sys/class/switch/test2' (No such file or directory)

03-16 16:19:18.871: ERROR/vold(550): Error bootstrapping switch '/sys/class/switch/test2' (m)

03-16 16:19:18.871: ERROR/vold(550): Error opening switch name path '/sys/class/switch/test' (No such file or directory)

03-16 16:19:18.871: ERROR/vold(550): Error bootstrapping switch '/sys/class/switch/test' (m)
03-16 16:19:19.091: ERROR/flash_image(558): can't find recovery partition

03-16 16:19:33.100: ERROR/MemoryHeapBase(577): error opening /dev/pmem: No such file or directory

03-16 16:19:33.100: ERROR/SurfaceFlinger(577): Couldn't open /sys/power/wait_for_fb_sleep or /sys/power/wait_for_fb_wake

03-16 16:19:33.151: ERROR/GLLogger(577): couldn't load <libhgl.so> library (Cannot find library)

03-16 16:19:33.261: ERROR/GLLogger(577): couldn't load <libhgl.so> library (Cannot find library)

03-16 16:19:36.471: ERROR/BatteryService(577): Could not open '/sys/class/power_supply/usb/online'

03-16 16:19:36.471: ERROR/BatteryService(577): Could not open '/sys/class/power_supply/battery/batt_vol'

03-16 16:19:36.471: ERROR/BatteryService(577): Could not open '/sys/class/power_supply/battery/batt_temp'

03-16 16:19:36.850: ERROR/EventHub(577): could not get driver version for /dev/input/mouse0, Not a typewriter

03-16 16:19:36.880: ERROR/EventHub(577): could not get driver version for /dev/input/mice, Not a typewriter

03-16 16:19:36.950: ERROR/System(577): Failure starting core service

03-16 16:19:36.950: ERROR/System(577): java.lang.SecurityException

03-16 16:19:36.950: ERROR/System(577):     at android.os.BinderProxy.transact(Native Method)

03-16 16:19:36.950: ERROR/System(577):     at 
android.os.ServiceManagerProxy.addService(ServiceManagerNative.java:146)

03-16 16:19:36.950: ERROR/System(577):     at android.os.ServiceManager.addService(ServiceManager.java:72)

03-16 16:19:36.950: ERROR/System(577):     at com.android.server.ServerThread.run(SystemServer.java:163)

03-16 16:19:36.959: ERROR/AndroidRuntime(577): Crash logging skipped, no checkin service

03-16 16:19:38.219: ERROR/LockPatternKeyguardView(577): Failed to bind to GLS while 
checking for account
03-16 16:19:39.790: ERROR/jdwp(623): Failed sending req to debugger: Broken pipe (-1 of 27)
03-16 16:19:39.790: ERROR/jdwp(623): Failed sending reply to debugger: Broken pipe
03-16 16:19:42.960: ERROR/ApplicationContext(577): Couldn't create directory for SharedPreferences file shared_prefs/wallpaper-hints.xml
03-16 16:19:44.051: ERROR/ActivityThread(621): Failed to find provider info for android.server.checkin
03-16 16:19:45.341: ERROR/ActivityThread(621): Failed to find provider info for android.server.checkin

03-16 16:19:45.432: ERROR/ActivityThread(621): Failed to find provider info for android.server.checkin
03-16 16:19:51.651: ERROR/Database(715): sqlite3_open_v2("/data/data/com.SGMalls/databases/mallMapv2.sqlite", &handle, 1, NULL) failed
03-16 16:19:51.701: ERROR/AndroidRuntime(715): Uncaught handler: thread main exiting due to uncaught exception

03-16 16:19:51.710: ERROR/AndroidRuntime(715): java.lang.Error: Error copying database

03-16 16:19:51.710: ERROR/AndroidRuntime(715):     at Unit.DataBaseHelper.createDataBase(DataBaseHelper.java:47)

03-16 16:19:51.710: ERROR/AndroidRuntime(715):     at com.Test.Test.onCreate(Test.java:23)

03-16 16:19:51.710: ERROR/AndroidRuntime(715):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)

03-16 16:19:51.710: ERROR/AndroidRuntime(715):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231)

03-16 16:19:51.710: ERROR/AndroidRuntime(715):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2284)

03-16 16:19:51.710: ERROR/AndroidRuntime(715):     at android.app.ActivityThread.access$1800(ActivityThread.java:112)

03-16 16:19:51.710: ERROR/AndroidRuntime(715):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1692)

03-16 16:19:51.710: ERROR/AndroidRuntime(715):     at android.os.Handler.dispatchMessage(Handler.java:99)

03-16 16:19:51.710: ERROR/AndroidRuntime(715):     at android.os.Looper.loop(Looper.java:123)

03-16 16:19:51.710: ERROR/AndroidRuntime(715):     at android.app.ActivityThread.main(ActivityThread.java:3948)

03-16 16:19:51.710: ERROR/AndroidRuntime(715):     at java.lang.reflect.Method.invokeNative(Native Method)

03-16 16:19:51.710: ERROR/AndroidRuntime(715):     at java.lang.reflect.Method.invoke(Method.java:521)

03-16 16:19:51.710: ERROR/AndroidRuntime(715):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
03-16 16:19:51.710: ERROR/AndroidRuntime(715):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)

03-16 16:19:51.710: ERROR/AndroidRuntime(715):     at dalvik.system.NativeStart.main(Native Method)
3
  • Can you replace throw new Error("Error copying database"); with throw new RuntimeException(e.getMessage()); to tell us what the specific IOException message is? Commented Mar 16, 2010 at 5:38
  • HI. thanks very much. I already paste the all error message. Pls help me. Thank you Commented Mar 16, 2010 at 11:39
  • java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Test/com.Test.Test}: android.database.sqlite.SQLiteException: unable to open database file i test it again. Show the exception message like this. Commented Mar 17, 2010 at 13:43

2 Answers 2

2

A bug has already been raised for this.

Bug Report Android

Use this api as a workaround:

Context.openOrCreateDatabase("mmyown.db", MODE_PRIVATE, null);
Sign up to request clarification or add additional context in comments.

Comments

2

Replace your checkDataBase() method with this code:

public boolean databaseExist()
{
    File dbFile = new File(DB_PATH + DB_NAME);
    return dbFile.exists();
}

this will solve your problem. :)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.