使用我自己的SQLite数据库

时间:2010-03-16 03:52:13

标签: android database sqlite

我有一个sqlite数据库,我把这个文件放在“assets”文件夹中。

下面的代码,请帮助并告知此代码中的错误,

如何使用我自己的sqlite数据库。

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; 
    } 
} 

错误消息:

  

错误/数据库(725):sqlite3_open_v2(“/ data / data / com.SGMalls / databases /   mallMapv2.sqlite“,&amp; handle,1,NULL)失败

     

03-15 22:34:11.747:ERROR / AndroidRuntime(725):未捕获的处理程序:   线程主要由于未捕获的异常而退出

     

03-15 22:34:11.766:ERROR / AndroidRuntime(725):java.lang.Error:错误   复制数据库

非常感谢


所有日志错误消息

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)

2 个答案:

答案 0 :(得分:2)

已经为此提出了一个错误。

Bug Report Android

使用此api作为解决方法:

Context.openOrCreateDatabase("mmyown.db", MODE_PRIVATE, null);

答案 1 :(得分:2)

使用以下代码替换checkDataBase()方法:

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

这将解决您的问题。 :)