Advertisement
Guest User

Untitled

a guest
Jul 20th, 2011
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.37 KB | None | 0 0
  1. public class MySQLiteOpenHelper extends SQLiteOpenHelper
  2. {
  3.   public String TableNames[];
  4.   public String FieldNames[][];
  5.   public String FieldTypes[][];
  6.   public static String NO_CREATE_TABLES = "no tables";
  7.   private String message = "";
  8.  
  9.   public MySQLiteOpenHelper(Context context, String dbname, CursorFactory factory, int version, String tableNames[], String fieldNames[][], String fieldTypes[][])
  10.   {
  11.     super(context, dbname, factory, version);
  12.     TableNames = tableNames;
  13.     FieldNames = fieldNames;
  14.     FieldTypes = fieldTypes;
  15.   }
  16.  
  17.   @Override
  18.   public void onCreate(SQLiteDatabase db)
  19.   {
  20.     if (TableNames == null)
  21.     {
  22.       message = NO_CREATE_TABLES;
  23.       return;
  24.     }
  25.     /* 建立table */
  26.     for (int i = 0; i < TableNames.length; i++)
  27.     {
  28.       String sql = "CREATE TABLE " + TableNames[i] + " (";
  29.       for (int j = 0; j < FieldNames[i].length; j++)
  30.       {
  31.         sql += FieldNames[i][j] + " " + FieldTypes[i][j] + ",";
  32.       }
  33.       sql = sql.substring(0, sql.length() - 1);
  34.       sql += ")";
  35.       db.execSQL(sql);
  36.     }
  37.   }
  38.  
  39.   @Override
  40.   public void onUpgrade(SQLiteDatabase db, int arg1, int arg2)
  41.   {
  42.     for (int i = 0; i < TableNames[i].length(); i++)
  43.     {
  44.       String sql = "DROP TABLE IF EXISTS " + TableNames[i];
  45.       db.execSQL(sql);
  46.     }
  47.     onCreate(db);
  48.   }
  49.  
  50.   public void execSQL(String sql) throws java.sql.SQLException
  51.   {
  52.     SQLiteDatabase db = this.getWritableDatabase();
  53.     db.execSQL(sql);
  54.   }
  55.  
  56.   /**
  57.    * 查詢資料
  58.    *
  59.    * @param table
  60.    *          查詢的table name
  61.    * @param columns
  62.    *          查詢的資料的欄位名稱
  63.    * @param selection
  64.    *          查詢條件字串 如:field1 = ? and field2 = ?
  65.    * @param selectionArgs
  66.    *          查詢條件的值 如:["a","b"]
  67.    * @param groupBy
  68.    *          groupBy後面的字串 如:field1,field2
  69.    * @param having
  70.    *          having後面的字串
  71.    * @param orderBy
  72.    *          orderBy後面的字串
  73.    * @return Cursor 包含了取得的資料集
  74.    */
  75.   public Cursor select(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)
  76.   {
  77.     SQLiteDatabase db = this.getReadableDatabase();
  78.     Cursor cursor = db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy);
  79.     return cursor;
  80.   }
  81.  
  82.   /**
  83.    * 新增資料
  84.    *
  85.    * @param table
  86.    *          新增資料的table name
  87.    * @param fields
  88.    *          新增資料的欄位名稱
  89.    * @param values
  90.    *          新增資料的欄位值
  91.    * @return long row id
  92.    */
  93.   public long insert(String table, String fields[], String values[])
  94.   {
  95.     SQLiteDatabase db = this.getWritableDatabase();
  96.     /* 將新增的值放入ContentValues */
  97.     ContentValues cv = new ContentValues();
  98.     for (int i = 0; i < fields.length; i++)
  99.     {
  100.       cv.put(fields[i], values[i]);
  101.     }
  102.     return db.insert(table, null, cv);
  103.   }
  104.  
  105.   /**
  106.    * 刪除資料
  107.    *
  108.    * @param table
  109.    *          刪除資料的table name
  110.    * @param where
  111.    *          刪除資料的條件
  112.    * @param whereValue
  113.    *          刪除資料的條件值
  114.    * @return int 刪除的筆數
  115.    */
  116.   public int delete(String table, String where, String[] whereValue)
  117.   {
  118.     SQLiteDatabase db = this.getWritableDatabase();
  119.  
  120.     return db.delete(table, where, whereValue);
  121.   }
  122.  
  123.   /**
  124.    * 更新資料
  125.    *
  126.    * @param table
  127.    *          更新資料的table name
  128.    * @param fields
  129.    *          更新資料的欄位名稱
  130.    * @param values
  131.    *          更新資料的欄位值
  132.    * @param where
  133.    *          更新除資料的條件
  134.    * @param whereValue
  135.    *          更新資料的條件值
  136.    * @return int 更新的筆數
  137.    */
  138.   public int update(String table, String updateFields[],
  139.       String updateValues[], String where, String[] whereValue)
  140.   {
  141.     SQLiteDatabase db = this.getWritableDatabase();
  142.  
  143.     /* 將修改的值放入ContentValues */
  144.     ContentValues cv = new ContentValues();
  145.     for (int i = 0; i < updateFields.length; i++)
  146.     {
  147.       cv.put(updateFields[i], updateValues[i]);
  148.     }
  149.     return db.update(table, cv, where, whereValue);
  150.   }
  151.  
  152.   public String getMessage()
  153.   {
  154.     return message;
  155.   }
  156.  
  157.   @Override
  158.   public synchronized void close()
  159.   {
  160.     // TODO Auto-generated method stub
  161.     super.close();
  162.   }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement