android可执行sql文件的做法

如下:

private class DatabaseHelper extends SQLiteOpenHelper {

private List<String> tables = new ArrayList<String>();


DatabaseHelper(Context context, String dbName, CursorFactory factory,
int version) {
super(context, dbName, null, version);
}


public void onCreate(SQLiteDatabase db) {


InputStream input = mContext.getResources().openRawResource(
dataScriptId);
BufferedReader reader = null;
try {
logger.error(":::onCreate+DatabaseHelper");
reader = new BufferedReader(new InputStreamReader(input));
executeSqlScript(db, reader);


} catch (IOException e) {
logger.error(":::onCreate+DatabaseHelper:error");
e.printStackTrace();
} finally {
try {
if (input != null) {
input.close();
}
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}


public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
for (String table : tables) {
db.execSQL("DROP TABLE IF EXISTS  " + table);
}
onCreate(db);
}


public void executeSqlScript(SQLiteDatabase db, BufferedReader reader)
throws IOException {
StringBuilder sql = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
if (TextUtils.isEmpty(line) || line.startsWith("#")) {
continue;
}
if (TextUtils.isEmpty(line) || line.startsWith("=")) {
line = line.replaceAll("=", "");
tables.add(line);
continue;
}
logger.error("###### line : " + line);
line = line.trim();
int index = line.indexOf(';');
if (index >= 0) {
String firstStr = line.substring(0, index + 1);
sql.append(firstStr).append('\n');
try {
db.execSQL(sql.toString()); // make database
} catch (SQLException e) {
e.printStackTrace();
}


sql = new StringBuilder();
if (index < line.length()) {
String lastStr = line.substring(index + 1);
if (!TextUtils.isEmpty(lastStr)) {
sql.append(lastStr);
}
}
} else {
sql.append(line).append('\n');
}
}
if (sql.length() > 0) {
try {
db.execSQL(sql.toString());
} catch (SQLException e) {
e.printStackTrace();
}
}
}

}

但是这种做法,sql文件应该如下:

#BEGIN TRANSACTION;


======channel_list======
CREATE TABLE "channel_list" 
(
    "c_id" INTEGER PRIMARY KEY,
    "c_name" TEXT
);
INSERT INTO "channel_list"("c_name") VALUES ("头条");   
INSERT INTO "channel_list"("c_name") VALUES ("国内");
INSERT INTO "channel_list"("c_name") VALUES ("国外");
INSERT INTO "channel_list"("c_name") VALUES ("社会");
INSERT INTO "channel_list"("c_name") VALUES ("财经");
CREATE INDEX "IDX_CHANNEL_LIST_ID" ON "channel_list" ("c_id");


======news_info=======
CREATE TABLE "news_info" 
(
    "n_id" INTEGER,
    "n_cid" INTEGER,
    "n_title" TEXT,
    "n_intro" TEXT,
    "n_publish_time" DATA,
    "n_source" TEXT,
    "n_pic_url" TEXT,
    "n_content" TEXT,
"n_create_time" DATA,
"flag" INTEGER
);
CREATE INDEX "IDX_NEWS_INFO_ID" ON "news_info" ("n_id");


#COMMIT;

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值