Android编程心得-在Assets文件夹中放入.sql文件实现创建SQlite表的操作
2015-01-24 12:13
531 查看
当我们在使用SQLiteOpenHelper时,经常使用db.execSQL(String sql)方法写入对应语句实现创建表的操作,这样的确可以实现业务逻辑。与此同时还有一种更灵活的方法,从assets文件夹下读取对应的.sql文件,然后创建表。
1.首先在工程的assets文件夹下,添加对应的.sql文件
2.配置一个Configuration类,用于保存固定路径变量
[java] view plaincopy
public class Configuration {
public static final String DB_PATH = "schema";
public static final String DB_NAME = "test.db";
public static final int DB_VERSION = 1;
public static int oldVersion = -1;
}
3.逻辑实现类,executeAssetsSQL方法用于向Assets文件夹对应的路径读取SQL语句然后执行创建操作
[java] view plaincopy
public class DBHelper extends SQLiteOpenHelper {
private Context mContext;
public DBHelper(Context context, String databaseName,
CursorFactory factory, int version) {
super(context, databaseName, factory, version);
mContext = context;
}
/**
* 数据库第一次创建时调用
* */
@Override
public void onCreate(SQLiteDatabase db) {
executeAssetsSQL(db, "schema.sql");
System.out.println("创建表");
}
/**
* 数据库升级时调用
* */
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//数据库不升级
if (newVersion <= oldVersion) {
return;
}
Configuration.oldVersion = oldVersion;
int changeCnt = newVersion - oldVersion;
for (int i = 0; i < changeCnt; i++) {
// 依次执行updatei_i+1文件 由1更新到2 [1-2],2更新到3 [2-3]
String schemaName = "update" + (oldVersion + i) + "_"
+ (oldVersion + i + 1) + ".sql";
executeAssetsSQL(db, schemaName);
}
}
/**
* 读取数据库文件(.sql),并执行sql语句
* */
private void executeAssetsSQL(SQLiteDatabase db, String schemaName) {
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(mContext.getAssets()
.open(Configuration.DB_PATH + "/" + schemaName)));
System.out.println("路径:"+Configuration.DB_PATH + "/" + schemaName);
String line;
String buffer = "";
while ((line = in.readLine()) != null) {
buffer += line;
if (line.trim().endsWith(";")) {
db.execSQL(buffer.replace(";", ""));
buffer = "";
}
}
} catch (IOException e) {
Log.e("db-error", e.toString());
} finally {
try {
if (in != null)
in.close();
} catch (IOException e) {
Log.e("db-error", e.toString());
}
}
}
}
1.首先在工程的assets文件夹下,添加对应的.sql文件
2.配置一个Configuration类,用于保存固定路径变量
[java] view plaincopy
public class Configuration {
public static final String DB_PATH = "schema";
public static final String DB_NAME = "test.db";
public static final int DB_VERSION = 1;
public static int oldVersion = -1;
}
3.逻辑实现类,executeAssetsSQL方法用于向Assets文件夹对应的路径读取SQL语句然后执行创建操作
[java] view plaincopy
public class DBHelper extends SQLiteOpenHelper {
private Context mContext;
public DBHelper(Context context, String databaseName,
CursorFactory factory, int version) {
super(context, databaseName, factory, version);
mContext = context;
}
/**
* 数据库第一次创建时调用
* */
@Override
public void onCreate(SQLiteDatabase db) {
executeAssetsSQL(db, "schema.sql");
System.out.println("创建表");
}
/**
* 数据库升级时调用
* */
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//数据库不升级
if (newVersion <= oldVersion) {
return;
}
Configuration.oldVersion = oldVersion;
int changeCnt = newVersion - oldVersion;
for (int i = 0; i < changeCnt; i++) {
// 依次执行updatei_i+1文件 由1更新到2 [1-2],2更新到3 [2-3]
String schemaName = "update" + (oldVersion + i) + "_"
+ (oldVersion + i + 1) + ".sql";
executeAssetsSQL(db, schemaName);
}
}
/**
* 读取数据库文件(.sql),并执行sql语句
* */
private void executeAssetsSQL(SQLiteDatabase db, String schemaName) {
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(mContext.getAssets()
.open(Configuration.DB_PATH + "/" + schemaName)));
System.out.println("路径:"+Configuration.DB_PATH + "/" + schemaName);
String line;
String buffer = "";
while ((line = in.readLine()) != null) {
buffer += line;
if (line.trim().endsWith(";")) {
db.execSQL(buffer.replace(";", ""));
buffer = "";
}
}
} catch (IOException e) {
Log.e("db-error", e.toString());
} finally {
try {
if (in != null)
in.close();
} catch (IOException e) {
Log.e("db-error", e.toString());
}
}
}
}
相关文章推荐
- Android编程心得-在Assets文件夹中放入.sql文件实现创建SQlite表的操作
- 在Assets文件夹中放入.sql或.db文件实现创建SQlite表的操作
- Qt(五)--- 文件操作以及实现文件的复制操作并且在目的目录创建相应文件夹
- 文件操作以及实现文件的复制操作并且在目的目录创建相应文件夹
- php(创建、删除、移动、复制)文件及文件夹操作的实现方法
- java实现对hdfs文件系统的上传,下载,删除,创建文件夹的操作演示
- asp结合fso实现文件或文件夹创建删除等操作的函数
- asp结合fso实现文件或文件夹创建删除等操作的函数
- 该文件没有程序与之关联来执行操作,请在控制面板的文件夹选项中创建关联的解决办法
- asp下利用fso实现文件夹或文件移动改名等操作函数
- 关于:SQL 以前的某个程序安装已在安装计算机上创建挂起的文件操作 解决办法
- 如何应用T-SQL实现本地文件操作
- SQL安装时出现“以前的某个程序安装已在安装计算机上创建挂起的文件操作。运作安装程序之前必须重新启动计算机”
- SQL安装时出现“以前的某个程序安装已在安装计算机上创建挂起的文件操作……”
- SQL安装时 以前的某个程序安装已在安装计算机上创建挂起的文件操作。运行安装程序之前必须重新启动计算机
- SQL 安装提示:以前的某个程序安装已在安装计算机上创建挂起的文件操作。运行安装程序之前必须重新启动计算机!。
- 如何在C#中用程序执行指定的SQL脚本文件,实现自动安装创建数据库.txt
- SQL安装:以前的某个程序安装已在安装计算机上创建挂起的文件操作
- asp.net如何实现删除文件夹及文件内容操作
- SQL安装错误"以前的某个程序安装已在安装计算机上创建挂起的文件操作。运行安装程序之前必须重新启动计算机"