android:Android中用文件初始化sqlite数据库(zz)
2017-05-15 22:01
183 查看
很多时候在应用安装初始化时,需要创建本地数据库,同时为数据库添加数据,之后再从数据库中读取数据。
这里有2个思路
1.先在本地创建一个能支持android使用的sqlite数据库文件,启动时,用现成的sqlite的二进制文件进行直接copy到Android系统的数据库路径
2.可以考虑在第一次启动时,执行数据库初始化的sql文件.
1.在本地准备android能使用的sqlite数据库文件
使用sqlite数据库管理工具,看个人爱好(SQLite Database Browser ,Navicat Premium,)
打开数据库,创建"android_metadata"数据表
Sql代码
![](https://images2015.cnblogs.com/blog/12617/201705/12617-20170515220053041-701619800.png)
CREATE TABLE "android_metadata" ("_id" INTEGER PRIMARY KEY AUTOINCREMENT,"locale" TEXT DEFAULT 'en_US');//创建表
INSERT INTO "android_metadata" VALUES (1,'en_US');//插入值
创建其他应用需要的表..此处省略.
2.复制文件到应用中.
把第一步创建的数据库文件复制到应用中的assets文件夹, asserts文件夹的路径如下:
![](https://images2015.cnblogs.com/blog/12617/201705/12617-20170515220055244-1567620136.png)
然后创建DateBaseHelper extends SQLiteOpenHelper的类文件.
代码如下:
Java代码
![](https://images2015.cnblogs.com/blog/12617/201705/12617-20170515220056041-1365903774.png)
public class DataBaseHelper extends SQLiteOpenHelper {
//The Android's default system path of your application database.
private static String DB_PATH = "/data/data/YOUR_PACKAGE/databases/";
private static String DB_NAME = "myDBName";
private SQLiteDatabase myDataBase;
private final Context myContext;
/**
* Constructor
* Takes and keeps a reference of the passed context in order to access to the application assets and resources.
* @param context
*/
public DataBaseHelper(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
}
/**
* Creates a empty database on the system and rewrites it with your own database.
* */
public void createDataBase()throws IOException {
boolean dbExist = checkDataBase();
if (dbExist) {
//do nothing - database already exist
} else {
//By calling this method and empty database will be created into the default system path
//of your application so we are gonna be able to overwrite that database with our database.
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
/**
* Check if the database already exist to avoid re-copying the file each time you open the application.
* @return true if it exists, false if it doesn't
*/
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
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) {
checkDB.close();
}
return checkDB != null ? true : false;
}
/**
* Copies your database from your local assets-folder to the just created empty database in the
* system folder, from where it can be accessed and handled.
* This is done by transfering bytestream.
* */
private void copyDataBase()throws IOException {
//Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DB_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
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) {}
// Add your public helper methods to access and get content from the database.
// You could return cursors by doing "return myDataBase.query(....)" so it'd be easy
// to you to create adapters for your views.
}
3.现在我们可以创建DateBaseHelper的实现操作了.
createDataBase() //创建
openDataBase()//打开只读数据库
记得要更改"YOUR_PACKAGE"为你的应用的包名
如:com.examplename.myapp
大概代码如下:
Java代码
![](https://images2015.cnblogs.com/blog/12617/201705/12617-20170515220056619-1507689285.png)
...
DataBaseHelper myDbHelper = new DataBaseHelper();
myDbHelper = new DataBaseHelper(this);
try {
myDbHelper.createDataBase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
try {
myDbHelper.openDataBase();
} catch (SQLException sqle) {
throw sqle;
}
...
来源: http://zhangfan822.iteye.com/blog/1883829
这里有2个思路
1.先在本地创建一个能支持android使用的sqlite数据库文件,启动时,用现成的sqlite的二进制文件进行直接copy到Android系统的数据库路径
2.可以考虑在第一次启动时,执行数据库初始化的sql文件.
1.在本地准备android能使用的sqlite数据库文件
使用sqlite数据库管理工具,看个人爱好(SQLite Database Browser ,Navicat Premium,)
打开数据库,创建"android_metadata"数据表
Sql代码
![](https://images2015.cnblogs.com/blog/12617/201705/12617-20170515220053041-701619800.png)
CREATE TABLE "android_metadata" ("_id" INTEGER PRIMARY KEY AUTOINCREMENT,"locale" TEXT DEFAULT 'en_US');//创建表
INSERT INTO "android_metadata" VALUES (1,'en_US');//插入值
创建其他应用需要的表..此处省略.
2.复制文件到应用中.
把第一步创建的数据库文件复制到应用中的assets文件夹, asserts文件夹的路径如下:
![](https://images2015.cnblogs.com/blog/12617/201705/12617-20170515220055244-1567620136.png)
然后创建DateBaseHelper extends SQLiteOpenHelper的类文件.
代码如下:
Java代码
![](https://images2015.cnblogs.com/blog/12617/201705/12617-20170515220056041-1365903774.png)
public class DataBaseHelper extends SQLiteOpenHelper {
//The Android's default system path of your application database.
private static String DB_PATH = "/data/data/YOUR_PACKAGE/databases/";
private static String DB_NAME = "myDBName";
private SQLiteDatabase myDataBase;
private final Context myContext;
/**
* Constructor
* Takes and keeps a reference of the passed context in order to access to the application assets and resources.
* @param context
*/
public DataBaseHelper(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
}
/**
* Creates a empty database on the system and rewrites it with your own database.
* */
public void createDataBase()throws IOException {
boolean dbExist = checkDataBase();
if (dbExist) {
//do nothing - database already exist
} else {
//By calling this method and empty database will be created into the default system path
//of your application so we are gonna be able to overwrite that database with our database.
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
/**
* Check if the database already exist to avoid re-copying the file each time you open the application.
* @return true if it exists, false if it doesn't
*/
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
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) {
checkDB.close();
}
return checkDB != null ? true : false;
}
/**
* Copies your database from your local assets-folder to the just created empty database in the
* system folder, from where it can be accessed and handled.
* This is done by transfering bytestream.
* */
private void copyDataBase()throws IOException {
//Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DB_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
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) {}
// Add your public helper methods to access and get content from the database.
// You could return cursors by doing "return myDataBase.query(....)" so it'd be easy
// to you to create adapters for your views.
}
3.现在我们可以创建DateBaseHelper的实现操作了.
createDataBase() //创建
openDataBase()//打开只读数据库
记得要更改"YOUR_PACKAGE"为你的应用的包名
如:com.examplename.myapp
大概代码如下:
Java代码
![](https://images2015.cnblogs.com/blog/12617/201705/12617-20170515220056619-1507689285.png)
...
DataBaseHelper myDbHelper = new DataBaseHelper();
myDbHelper = new DataBaseHelper(this);
try {
myDbHelper.createDataBase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
try {
myDbHelper.openDataBase();
} catch (SQLException sqle) {
throw sqle;
}
...
来源: http://zhangfan822.iteye.com/blog/1883829
相关文章推荐
- Android研究-Android系统初始化程序init和初始化配置文件init.rc分析[zz]
- Android研究-Android系统初始化程序init和初始化配置文件init.rc分析[zz]
- Android研究-Android系统初始化程序init和初始化配置文件init.rc分析[zz]
- Android初始化语言 (init.*.rc、init.conf文件格式)
- android开发之-查看、编辑手机sqlite数据库文件-实测
- android开发之使用SQLite数据库(db文件)
- Android中用文件初始化sqlite 数据库(二)
- Android初始化语言 (init.*.rc、init.conf文件格式)
- Android.SQLite数据库:初始化数据库并插入默认数据记录注意点
- zz android 系统 makefile文件(Android.mk)组织结构
- android 将SQLite数据库的表格导出为csv格式,并解析csv文件
- N年不开张了,写一个关于Android中用文件初始化sqlite 数据库的文章
- zz Android中SQLite数据库存储方式
- 【Android】通过控制台将sqlite数据库文件导出
- Android中用文件初始化sqlite 数据库的文(一) (转)
- android在布局文件中自定义参数并在初始化时获取
- Android 初始化语言(init.*.rc、init.conf文件格式)
- [zz] 分析Android 根文件系统启动过程(init守护进程分析)
- Android初始化语言(init.*.rc、init.conf文件格式)
- Android配置文件,所有权限ZZ