Android編程心得-在Assets檔案夾中放入.sql檔案實現建立SQlite表的操作

來源:互聯網
上載者:User

標籤:

 

  當我們在使用SQLiteOpenHelper時,經常使用db.execSQL(String sql)方法寫入對應語句實現建立表的操作,這樣的確可以實現商務邏輯。與此同時還有一種更靈活的方法,從assets檔案夾下讀取對應的.sql檔案,然後建立表。

  1.首先在工程的assets檔案夾下,添加對應的.sql檔案
   

 

 2.配置一個Configuration類,用於儲存固定路徑變數

 

[java] view plaincopy 
  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.       
  7. }  

 

3.邏輯實作類別,executeAssetsSQL方法用於向Assets檔案夾對應的路徑讀取SQL語句然後執行建立操作

[java] view plaincopy 
  1. public class DBHelper extends SQLiteOpenHelper {  
  2.   
  3.     private Context mContext;  
  4.   
  5.     public DBHelper(Context context, String databaseName,  
  6.             CursorFactory factory, int version) {  
  7.         super(context, databaseName, factory, version);  
  8.         mContext = context;  
  9.     }  
  10.   
  11.     /** 
  12.      * 資料庫第一次建立時調用 
  13.      * */  
  14.     @Override  
  15.     public void onCreate(SQLiteDatabase db) {  
  16.         executeAssetsSQL(db, "schema.sql");  
  17.         System.out.println("建立表");  
  18.     }  
  19.   
  20.     /** 
  21.      * 資料庫升級時調用 
  22.      * */  
  23.     @Override  
  24.     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {  
  25.         //資料庫不升級  
  26.         if (newVersion <= oldVersion) {  
  27.             return;  
  28.         }  
  29.         Configuration.oldVersion = oldVersion;  
  30.   
  31.         int changeCnt = newVersion - oldVersion;  
  32.         for (int i = 0; i < changeCnt; i++) {  
  33.             // 依次執行updatei_i+1檔案      由1更新到2 [1-2],2更新到3 [2-3]  
  34.             String schemaName = "update" + (oldVersion + i) + "_"  
  35.                     + (oldVersion + i + 1) + ".sql";  
  36.             executeAssetsSQL(db, schemaName);  
  37.         }  
  38.     }  
  39.   
  40.     /** 
  41.      * 讀取資料庫檔案(.sql),並執行sql語句 
  42.      * */  
  43.     private void executeAssetsSQL(SQLiteDatabase db, String schemaName) {  
  44.         BufferedReader in = null;  
  45.         try {  
  46.             in = new BufferedReader(new InputStreamReader(mContext.getAssets()  
  47.                     .open(Configuration.DB_PATH + "/" + schemaName)));  
  48.               
  49.             System.out.println("路徑:"+Configuration.DB_PATH + "/" + schemaName);  
  50.             String line;  
  51.             String buffer = "";  
  52.             while ((line = in.readLine()) != null) {  
  53.                 buffer += line;  
  54.                 if (line.trim().endsWith(";")) {  
  55.                     db.execSQL(buffer.replace(";", ""));  
  56.                     buffer = "";  
  57.                 }  
  58.             }  
  59.         } catch (IOException e) {  
  60.             Log.e("db-error", e.toString());  
  61.         } finally {  
  62.             try {  
  63.                 if (in != null)  
  64.                     in.close();  
  65.             } catch (IOException e) {  
  66.                 Log.e("db-error", e.toString());  
  67.             }  
  68.         }  
  69.     }  
  70.   
  71. }  


 

 

Android編程心得-在Assets檔案夾中放入.sql檔案實現建立SQlite表的操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.