建库主DbHelpers2.java:

/* * Copyright 2009 eFANsoftware *  * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at *  * http://www.apache.org/licenses/LICENSE-2.0 *  * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. */package net.shoxi.database;import java.io.BufferedReader;import java.io.File;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import net.shoxi.www.view.R;import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteOpenHelper;import android.util.Log;public class DbHelper2 extends SQLiteOpenHelper {private static final String VERSION = "1.00";private static final int DB_VERSION = (int) (Float.parseFloat(VERSION) * 100);private static final String DB_PATH = "medicine.db";private static DbHelper2 dbHelper;private Context ctx;public DbHelper2(Context ctx) {super(ctx, DB_PATH, null, DB_VERSION);this.ctx = ctx;}public static DbHelper2 getInstance(Context ctx) {if (dbHelper == null) {dbHelper = new DbHelper2(ctx);}return dbHelper;}@Overridepublic void onCreate(SQLiteDatabase db) {try {// 开始事务db.beginTransaction();// create db ZZapplySQLs(db, R.raw.db_create);// 设置事务成功的标志db.setTransactionSuccessful();} catch (Exception e) {Log.e(this.getClass().getName(), "", e);throw new RuntimeException("Database create error! Please contact the support or developer.",e);} finally {// 关闭事务db.endTransaction();}}/** * 批量执行sql语句 *  * @param db * @param sqlResourceId * @throws IOException */private void applySQLs(SQLiteDatabase db, int sqlResourceId)throws IOException {InputStream tmpIS = ctx.getResources().openRawResource(sqlResourceId);InputStreamReader tmpReader = new InputStreamReader(tmpIS);BufferedReader tmpBuf = new BufferedReader(tmpReader);StringBuffer sql = new StringBuffer();String tmpStr = null;while ((tmpStr = tmpBuf.readLine()) != null) {sql.append(tmpStr);sql.append('\n');if (tmpStr.trim().endsWith(";")) {db.execSQL(sql.toString());sql = new StringBuffer();}}tmpBuf.close();tmpReader.close();tmpIS.close();}@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {try {db.beginTransaction();applySQLs(db, R.raw.db_clean);onCreate(db);db.setTransactionSuccessful();} catch (Exception e) {Log.e(this.getClass().getName(), "", e);throw new RuntimeException("Database upgrade error! Please contact the support or developer.",e);} finally {db.endTransaction();}}}

导出的.sql SQL语句集文件

db_create.sql文件:

create table th_cate_x(PK_ID integer primary key autoincrement,VALUE text not null,_ORDER integer not null);create table th_category(PK_ID integer primary key autoincrement,VALUE text not null,PARENT_ID integer not null);create table th_com_users(PK_ID integer primary key autoincrement,USERNAME text not null,PASSWORD text not null,EMAIL text not null);create table th_content(PK_ID integer primary key autoincrement,VALUE1 text not null,VALUE2 text not null,VALUE3 text not null,VALUE4 text not null,VALUE5 text not null);create table th_user(PK_ID integer primary key autoincrement,USERNAME text not null,PASSWORD text not null,REALNAME text not null,EMAIL text not null,PARENT_ID integer not null);

db_clean.sql 文件:

DROP TABLE IF EXISTS `th_admin_users`;DROP TABLE IF EXISTS `th_cate_x`;DROP TABLE IF EXISTS `th_category`;DROP TABLE IF EXISTS `th_com_users`;DROP TABLE IF EXISTS `th_content`;DROP TABLE IF EXISTS `th_user`;

更多相关文章

  1. Android(安卓)ScrollView用法
  2. Android(安卓)运行崩溃找不到so包解决方案 UnsatisfiedLinkError
  3. Android静态安全检测 -> 密钥硬编码
  4. Android程序调试时生成main.out.xml文件,报错: Resource entry mai
  5. Android(安卓)Library Project
  6. android 数据库升级(保留原先数据)
  7. Android通过相机拍照后无法获取URI问题解决
  8. 开启adb su权限
  9. HTTP Server 'Bad Gateway' ( Android(安卓)adt 或者 sdk 更想不

随机推荐

  1. android EditText中inputType的属性列表
  2. Android(安卓)Studio试用总结
  3. [置顶] Android入门系列一(Android学习方
  4. Android(安卓)Adapter详解(1)
  5. Android(安卓)事件处理
  6. Android(安卓)AsyncTask解析
  7. android日志分析与记录.
  8. 理解Android的菜单
  9. Android高手进阶教程(四)之----Android(
  10. .net程序员转战android第二篇---牛刀小试