初学android,达人忽略,欢迎扔石头.
android中使用sqlite、复制assets下的数据库到SD卡、支持大于1M的文件
如果使用SD卡,需要在AndroidManifest.xml中设置权限
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission>
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> 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 //第一个文件名后缀
49 private static final int ASSETS_SUFFIX_BEGIN = 101;
50 //最后一个文件名后缀
51 private static final int ASSETS_SUFFIX_END = 103;
52
53 /**
54 * 在SQLiteOpenHelper的子类当中,必须有该构造函数
55 * @param context 上下文对象
56 * @param name 数据库名称
57 * @param factory 一般都是null
58 * @param version 当前数据库的版本,值必须是整数并且是递增的状态
59 */
60 public DBHelper(Context context, String name, CursorFactory factory, int version) {
61 //必须通过super调用父类当中的构造函数
62 super(context, name, null, version);
63 this.myContext = context;
64 }
65
66 public DBHelper(Context context, String name, int version){
67 this(context,name,null,version);
68 }
69
70 public DBHelper(Context context, String name){
71 this(context,name,DB_VERSION);
72 }
73
74 public DBHelper (Context context) {
75 this(context, DB_PATH + DB_NAME);
76 }
77
78 public void createDataBase() throws IOException{
79 boolean dbExist = checkDataBase();
80 if(dbExist){
81 //数据库已存在,do nothing.
82 }else{
83 //创建数据库
84 try {
85 File dir = new File(DB_PATH);
86 if(!dir.exists()){
87 dir.mkdirs();
88 }
89 File dbf = new File(DB_PATH + DB_NAME);
90 if(dbf.exists()){
91 dbf.delete();
92 }
93 SQLiteDatabase.openOrCreateDatabase(dbf, null);
94 // 复制asseets中的db文件到DB_PATH下
95 copyDataBase();
96 } catch (IOException e) {
97 throw new Error("数据库创建失败");
98 }
99 }
100 }
101
102 //检查数据库是否有效
103 private boolean checkDataBase(){
104 SQLiteDatabase checkDB = null;
105 String myPath = DB_PATH + DB_NAME;
106 try{
107 checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
108 }catch(SQLiteException e){
109 //database does't exist yet.
110 }
111 if(checkDB != null){
112 checkDB.close();
113 }
114 return checkDB != null ? true : false;
115 }
116
117 /**
118 * Copies your database from your local assets-folder to the just created empty database in the
119 * system folder, from where it can be accessed and handled.
120 * This is done by transfering bytestream.
121 * */
122 private void copyDataBase() throws IOException{
123 //Open your local db as the input stream
124 InputStream myInput = myContext.getAssets().open(ASSETS_NAME);
125 // Path to the just created empty db
126 String outFileName = DB_PATH + DB_NAME;
127 //Open the empty db as the output stream
128 OutputStream myOutput = new FileOutputStream(outFileName);
129 //transfer bytes from the inputfile to the outputfile
130 byte[] buffer = new byte[1024];
131 int length;
132 while ((length = myInput.read(buffer))>0){
133 myOutput.write(buffer, 0, length);
134 }
135 //Close the streams
136 myOutput.flush();
137 myOutput.close();
138 myInput.close();
139 }
140
141 //复制assets下的大数据库文件时用这个
142 private void copyBigDataBase() throws IOException{
143 InputStream myInput;
144 String outFileName = DB_PATH + DB_NAME;
145 OutputStream myOutput = new FileOutputStream(outFileName);
146 for (int i = ASSETS_SUFFIX_BEGIN; i < ASSETS_SUFFIX_END+1; i++) {
147 myInput = myContext.getAssets().open(ASSETS_NAME + "." + i);
148 byte[] buffer = new byte[1024];
149 int length;
150 while ((length = myInput.read(buffer))>0){
151 myOutput.write(buffer, 0, length);
152 }
153 myOutput.flush();
154 myInput.close();
155 }
156 myOutput.close();
157 }
158
159 @Override
160 public synchronized void close() {
161 if(myDataBase != null){
162 myDataBase.close();
163 }
164 super.close();
165 }
166
167 /**
168 * 该函数是在第一次创建的时候执行,
169 * 实际上是第一次得到SQLiteDatabase对象的时候才会调用这个方法
170 */
171 @Override
172 public void onCreate(SQLiteDatabase db) {
173 }
174
175 /**
176 * 数据库表结构有变化时采用
177 */
178 @Override
179 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
180 }
181
182 }
183
分享到:
相关推荐
Android 登录注册 数据库SQLite 验证,导出数据库到SD卡 Android 登录注册 数据库SQLite 验证,导出数据库到SD卡
Android 数据库SQLite 详解,简单学好SQLite
一个完整的 SQLite 数据库是存储在一个单一的跨平台的磁盘文件。 SQLite 是非常小的,是轻量级的,完全配置时小于 400KiB,省略可选功能配置时小于250KiB。 SQLite 是自给自足的,这意味着不需要任何外部的依赖。 ...
android应用,将外部的sqlite数据库导入到应用的私有空间中,源db文件置于assets目录下,如果源文件大小超过1M,使用Filesplit分割为不超过1M的小文件,那么就将所有分小文件置于assets目录下,本工程里已带...
Android Studio SQLite数据库使用
Android 开发中使用 SQLite 数据库 简介: SQLite 是一款非常流行的嵌入式数据库,它支持 SQL 查询,并且只用很少的内存。Android 在运行时集成了 SQLite,所以每个 Android 应用程序都可以使用 SQLite 数据库。...
Android实验报告Sqlite数据库操作.pdf
Android使用SQLite数据库进行增删改查详细案例...
Android 开发中使用 SQLite 数据库
Android应用程序中使用自己的SQLite数据库Android应用程序中使用自己的SQLite数据库Android应用程序中使用自己的SQLite数据库Android应用程序中使用自己的SQLite数据库
主要介绍了Android操作存放在assets文件夹下SQLite数据库的方法,实例分析了Android操作SQLite数据库的相关技巧,需要的朋友可以参考下
SQLite使用单个文件存储数据,Android标准库包含SQLite库以及配套使用的一些Java辅助类。主要特点:轻量级,单一文件,跨平台,开源。 二. Android中SQLite数据库的使用 1、创建SQLite数据库 SQLiteDatabase db= ...
Delphi使用SQLite3,包括本地数据库和内存数据库,本地数据库加载到内存,内存数据库备份到本地,使用sqlite simple delphi包装类。
用sqlite实现学生成绩数据库的增删改查用sqlite实现学生成绩数据库的增删改查用sqlite实现学生成绩数据库的增删改查用sqlite实现学生成绩数据库的增删改查用sqlite实现学生成绩数据库的增删改查用sqlite实现学生成绩...
对Android数据库SQlite的基本操作,打开或新建数据库,增,删,改,查。 SQLiteOpenHelper类的运用。 ListView的运用和适配器的使用。 Menu键的使用。
很好的android轻量级数据库sqlite使用入门教程
前情提要:Android 数据库(SQLite) 【简介、创建、使用(增删改查、事务、实战演练)、数据显示控件(ListView、Adapter、实战演练)】 https://blog.csdn.net/weixin_44949135/article/details/105955663 Android ...
我们在使用ListView的时候需要和数据进行绑定,那么问题来了,如何获取SQLite数据库中的数据并动态的显示到ListView当中呢?其实过程很简单:首先要获取SQLite数据(当然首先你要创建一个SQLite数据库并填写了一些...