Android · 2015年8月7日 0

Android编程心得-在Assets文件夹中放入.sql文件实现创建SQlite表的操作

当我们在使用SQLiteOpenHelper时,经常使用db.execSQL(String sql)方法写入对应语句实现创建表的操作,这样的确可以实现业务逻辑。与此同时还有一种更灵活的方法,从assets文件夹下读取对应的.sql文件,然后创建表。

1.首先在工程的assets文件夹下,添加对应的.sql文件

 

2.配置一个Configuration类,用于保存固定路径变量

 

  1. public class Configuration {
  2.     public static final String DB_PATH = “schema”;
  3.     public static final String DB_NAME = “test.db”;
  4.     public static final int DB_VERSION = 1;
  5.     public static int oldVersion = –1;
  6. }

 

3.逻辑实现类,executeAssetsSQL方法用于向Assets文件夹对应的路径读取SQL语句然后执行创建操作

  1. public class DBHelper extends SQLiteOpenHelper {
  2.     private Context mContext;
  3.     public DBHelper(Context context, String databaseName,
  4.             CursorFactory factory, int version) {
  5.         super(context, databaseName, factory, version);
  6.         mContext = context;
  7.     }
  8.     /**
  9.      * 数据库第一次创建时调用
  10.      * */
  11.     @Override
  12.     public void onCreate(SQLiteDatabase db) {
  13.         executeAssetsSQL(db, “schema.sql”);
  14.         System.out.println(“创建表”);
  15.     }
  16.     /**
  17.      * 数据库升级时调用
  18.      * */
  19.     @Override
  20.     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  21.         //数据库不升级
  22.         if (newVersion <= oldVersion) {
  23.             return;
  24.         }
  25.         Configuration.oldVersion = oldVersion;
  26.         int changeCnt = newVersion – oldVersion;
  27.         for (int i = 0; i < changeCnt; i++) {
  28.             // 依次执行updatei_i+1文件      由1更新到2 [1-2],2更新到3 [2-3]
  29.             String schemaName = “update” + (oldVersion + i) + “_”
  30.                     + (oldVersion + i + 1) + “.sql”;
  31.             executeAssetsSQL(db, schemaName);
  32.         }
  33.     }
  34.     /**
  35.      * 读取数据库文件(.sql),并执行sql语句
  36.      * */
  37.     private void executeAssetsSQL(SQLiteDatabase db, String schemaName) {
  38.         BufferedReader in = null;
  39.         try {
  40.             in = new BufferedReader(new InputStreamReader(mContext.getAssets()
  41.                     .open(Configuration.DB_PATH + “/” + schemaName)));
  42.             System.out.println(“路径:”+Configuration.DB_PATH + “/” + schemaName);
  43.             String line;
  44.             String buffer = “”;
  45.             while ((line = in.readLine()) != null) {
  46.                 buffer += line;
  47.                 if (line.trim().endsWith(“;”)) {
  48.                     db.execSQL(buffer.replace(“;”“”));
  49.                     buffer = “”;
  50.                 }
  51.             }
  52.         } catch (IOException e) {
  53.             Log.e(“db-error”, e.toString());
  54.         } finally {
  55.             try {
  56.                 if (in != null)
  57.                     in.close();
  58.             } catch (IOException e) {
  59.                 Log.e(“db-error”, e.toString());
  60.             }
  61.         }
  62.     }
  63. }

 

 

 

Share this: