在 Eclipse 里新建好工程后,默认会有一个assets目录,在 Eclipse 中直接将准备好的 SQLite 数据库复制到该目录中,然后在主 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 是程序的包名,请根据自己的程序调整
// /data/data/com.test.db/
// databases 目录是准备放 SQLite 数据库的地方,也是 Android 程序默认的数据库存储目录
// 数据库名为 test.db
String DB_PATH = "/data/data/com.test.db/databases/" ;
String DB_NAME = "test.db" ;
// 检查 SQLite 数据库文件是否存在
if (( new File(DB_PATH + DB_NAME)).exists() == false ) {
// 如 SQLite 数据库文件不存在,再检查一下 database 目录是否存在
File f = new File(DB_PATH);
// 如 database 目录不存在,新建该目录
if (!f.exists()) {
f.mkdir();
}
try {
// 得到 assets 目录下我们实现准备好的 SQLite 数据库作为输入流
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);
}
// 关闭文件流
os.flush();
os.close();
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
// 下面测试 /data/data/com.test.db/databases/ 下的数据库是否能正常工作
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();
}
}
|
程序启动时候回去检查数据库文件在不在,如果不存在,就会把我们准备好的数据库复制到哪个 databases 目录下,而且如果用户卸载了这个程序,那么这个目录和数据库也将随之卸载。
再来一个示例。
正常的应用数据库放在/data/data/包名/database/test.db,应用发布时,这个数据库不会随着应用一起发布,
所以为了让我们已经准备好的数据正常使用,必须能实现数据库自身复制到sd卡下面,
实现拷贝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" ;
//数据库名
private final String DATABASE_FILENAME = "db_exam.db" ;
//这个context是必需的,没有context,怎么都不能实现数据库的拷贝操作;
private Context context;
//构造函数必需传入Context,数据库的操作都带有这个参数的传入
public ReleaseDataBaseActivity(Context ctx) {
this .context = ctx;
}
public SQLiteDatabase OpenDataBase() {
try {
String databaseFilename = DATABASE_PATH + "/" + DATABASE_FILENAME;
File dir = new File(DATABASE_PATH);
//判断SD卡下是否存在存放数据库的目录,如果不存在,新建目录
if (!dir.exists()) {
dir.mkdir();
Log.i( "ReleaseDataBaseActivity" , "dir made:" + DATABASE_PATH);
} else {
Log.i( "ReleaseDataBaseActivity" , "dir exist:" + DATABASE_PATH);
}
try {
//如果数据库已经在SD卡的目录下存在,那么不需要重新创建,否则创建文件,并拷贝/res/raw下面的数据库文件
if (!( new File(databaseFilename)).exists()) {
Log.i( "ReleaseDataBaseActivity" , "file not exist:"
+ databaseFilename);
///res/raw数据库作为输出流
InputStream is = this .context.getResources().openRawResource(
R.raw.db_exam);
//测试用
int size = is.available();
Log.i( "ReleaseDataBaseActivity" , "DATABASE_SIZE:" + 1 );
Log.i( "ReleaseDataBaseActivity" , "count:" + 0);
//用于存放数据库信息的数据流
FileOutputStream fos = new FileOutputStream(
databaseFilename);
byte[] buffer = new byte[8192];
int count = 0;
Log.i( "ReleaseDataBaseActivity" , "count:" + count);
//把数据写入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();
}
//实例化sd卡上得数据库,database作为返回值,是后面所有插入,删除,查询操作的借口。
SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(
databaseFilename, null );
return database;
} catch (Exception e) {
}
return null ;
}
}
|
经过测试,绝对好使,希望对大家有帮助。