在 Eclipse 里新建好工程后,默認(rèn)會(huì)有一個(gè)assets目錄,在 Eclipse 中直接將準(zhǔn)備好的 SQLite 數(shù)據(jù)庫復(fù)制到該目錄中,然后在主 Activity 里面編碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
package com.test.db; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import android.app.Activity; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; public class DbtestActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.main); // com.test.db 是程序的包名,請(qǐng)根據(jù)自己的程序調(diào)整 // /data/data/com.test.db/ // databases 目錄是準(zhǔn)備放 SQLite 數(shù)據(jù)庫的地方,也是 Android 程序默認(rèn)的數(shù)據(jù)庫存儲(chǔ)目錄 // 數(shù)據(jù)庫名為 test.db String DB_PATH = "/data/data/com.test.db/databases/" ; String DB_NAME = "test.db" ; // 檢查 SQLite 數(shù)據(jù)庫文件是否存在 if (( new File(DB_PATH + DB_NAME)).exists() == false ) { // 如 SQLite 數(shù)據(jù)庫文件不存在,再檢查一下 database 目錄是否存在 File f = new File(DB_PATH); // 如 database 目錄不存在,新建該目錄 if (!f.exists()) { f.mkdir(); } try { // 得到 assets 目錄下我們實(shí)現(xiàn)準(zhǔn)備好的 SQLite 數(shù)據(jù)庫作為輸入流 InputStream is = getBaseContext().getAssets().open(DB_NAME); // 輸出流 OutputStream os = new FileOutputStream(DB_PATH + DB_NAME); // 文件寫入 byte [] buffer = new byte [ 1024 ]; int length; while ((length = is.read(buffer)) > 0 ) { os.write(buffer, 0 , length); } // 關(guān)閉文件流 os.flush(); os.close(); is.close(); } catch (Exception e) { e.printStackTrace(); } } // 下面測(cè)試 /data/data/com.test.db/databases/ 下的數(shù)據(jù)庫是否能正常工作 SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null ); Cursor cursor = database.rawQuery( "select * from test" , null ); if (cursor.getCount() > 0 ) { cursor.moveToFirst(); try { // 解決中文亂碼問題 byte test[] = cursor.getBlob( 0 ); String strtest = new String(test, "utf-8" ).trim(); // 看輸出的信息是否正確 System.out.println(strtest); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } } cursor.close(); } } |
程序啟動(dòng)時(shí)候回去檢查數(shù)據(jù)庫文件在不在,如果不存在,就會(huì)把我們準(zhǔn)備好的數(shù)據(jù)庫復(fù)制到哪個(gè) databases 目錄下,而且如果用戶卸載了這個(gè)程序,那么這個(gè)目錄和數(shù)據(jù)庫也將隨之卸載。
再來一個(gè)示例。
正常的應(yīng)用數(shù)據(jù)庫放在/data/data/包名/database/test.db,應(yīng)用發(fā)布時(shí),這個(gè)數(shù)據(jù)庫不會(huì)隨著應(yīng)用一起發(fā)布,
所以為了讓我們已經(jīng)準(zhǔn)備好的數(shù)據(jù)正常使用,必須能實(shí)現(xiàn)數(shù)據(jù)庫自身復(fù)制到sd卡下面,
實(shí)現(xiàn)拷貝res/raw/test.db下資源拷貝到SD卡下的/mnt/sdcard/test/test.db
代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
package zcping.syan.DBDefinition; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import zcping.syan.DragonBaby.R; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.util.Log; public class ReleaseDataBaseActivity{ /** Called when the activity is first created. */ //SD卡下的目錄 private final String DATABASE_PATH = android.os.Environment .getExternalStorageDirectory().getAbsolutePath() + "/db_exam" ; //數(shù)據(jù)庫名 private final String DATABASE_FILENAME = "db_exam.db" ; //這個(gè)context是必需的,沒有context,怎么都不能實(shí)現(xiàn)數(shù)據(jù)庫的拷貝操作; private Context context; //構(gòu)造函數(shù)必需傳入Context,數(shù)據(jù)庫的操作都帶有這個(gè)參數(shù)的傳入 public ReleaseDataBaseActivity(Context ctx) { this .context = ctx; } public SQLiteDatabase OpenDataBase() { try { String databaseFilename = DATABASE_PATH + "/" + DATABASE_FILENAME; File dir = new File(DATABASE_PATH); //判斷SD卡下是否存在存放數(shù)據(jù)庫的目錄,如果不存在,新建目錄 if (!dir.exists()) { dir.mkdir(); Log.i( "ReleaseDataBaseActivity" , "dir made:" + DATABASE_PATH); } else { Log.i( "ReleaseDataBaseActivity" , "dir exist:" + DATABASE_PATH); } try { //如果數(shù)據(jù)庫已經(jīng)在SD卡的目錄下存在,那么不需要重新創(chuàng)建,否則創(chuàng)建文件,并拷貝/res/raw下面的數(shù)據(jù)庫文件 if (!( new File(databaseFilename)).exists()) { Log.i( "ReleaseDataBaseActivity" , "file not exist:" + databaseFilename); ///res/raw數(shù)據(jù)庫作為輸出流 InputStream is = this .context.getResources().openRawResource( R.raw.db_exam); //測(cè)試用 int size = is.available(); Log.i( "ReleaseDataBaseActivity" , "DATABASE_SIZE:" + 1 ); Log.i( "ReleaseDataBaseActivity" , "count:" + 0); //用于存放數(shù)據(jù)庫信息的數(shù)據(jù)流 FileOutputStream fos = new FileOutputStream( databaseFilename); byte[] buffer = new byte[8192]; int count = 0; Log.i( "ReleaseDataBaseActivity" , "count:" + count); //把數(shù)據(jù)寫入SD卡目錄下 while ((count = is.read(buffer)) > 0) { fos.write(buffer, 0, count); } fos.flush(); fos.close(); is.close(); } } catch (FileNotFoundException e) { Log.e( "Database" , "File not found" ); e.printStackTrace(); } catch (IOException e) { Log.e( "Database" , "IO exception" ); e.printStackTrace(); } //實(shí)例化sd卡上得數(shù)據(jù)庫,database作為返回值,是后面所有插入,刪除,查詢操作的借口。 SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase( databaseFilename, null ); return database; } catch (Exception e) { } return null ; } } |
經(jīng)過測(cè)試,絕對(duì)好使,希望對(duì)大家有幫助。