现在的位置: 首页 > 综合 > 正文

android复制数据库到SD卡

2013年03月19日 ⁄ 综合 ⁄ 共 1731字 ⁄ 字号 评论关闭

android中使用sqlite、复制assets下的数据库到SD卡、支持大于1M的文件

如果使用SD卡,需要在AndroidManifest.xml中设置权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission>

 

 

1 package cn.arthur.common;
2
3  import java.io.File;
4  import java.io.FileOutputStream;
5  import java.io.IOException;
6  import java.io.InputStream;
7  import java.io.OutputStream;
8
9  import android.content.Context;
10  import android.database.sqlite.SQLiteDatabase;
11  import android.database.sqlite.SQLiteDatabase.CursorFactory;
12 import android.database.sqlite.SQLiteException;
13 import android.database.sqlite.SQLiteOpenHelper;
14
15 /**
16 * @author Joshua
17 * 用法:
18 * DBHelper dbHelper = new DBHelper(this);
19 * dbHelper.createDataBase();
20 * SQLiteDatabase db = dbHelper.getWritableDatabase();
21 * Cursor cursor = db.query()
22 * db.execSQL(sqlString);
23 * 注意:execSQL不支持带;的多条SQL语句,只能一条一条的执行,晕了很久才明白
24 * 见execSQL的源码注释 (Multiple statements separated by ;s are not supported.)
25 * 将把assets下的数据库文件直接复制到DB_PATH,但数据库文件大小限制在1M以下
26 * 如果有超过1M的大文件,则需要先分割为N个小文件,然后使用copyBigDatabase()替换copyDatabase()
27 */
28 public class DBHelper extends SQLiteOpenHelper {
29 //用户数据库文件的版本
30 private static final int DB_VERSION = 1;
31 //数据库文件目标存放路径为系统默认位置,cn.arthur.examples 是你的包名
32 private static String DB_PATH = "/data/data/cn.arthur.examples/databases/";
33 /*
34 //如果你想把数据库文件存放在SD卡的话
35 private static String DB_PATH = android.os.Environment.getExternalStorageDirectory().getAbsolutePath()
36 + "/arthurcn/drivertest/packfiles/";
37 */
38 private static String DB_NAME = "hello.db";
39 private static String ASSETS_NAME = "hello.db";
40
41 private SQLiteDatabase myDataBase = null;
42 private final Context myContext;
43
44 /**
45 * 如果数据库文件较大,使用FileSplit分割为小于1M的小文件
46 * 此例中分割为 hello.db.101 hello.db.102 hello.db.103
47 */
48 //第一个文件名后缀

抱歉!评论已关闭.