原本沒有打算整理有關Sqlite數據庫文章的,最近一直在研究ContentProvider的使用,全部以爲仍是先對Sqlite進行一個簡單的回顧,也方便研究學習ContentProvider。html
Sqlite數據庫是一種輕量級數據庫,它具有跨平臺,多語言操做等優勢,它普遍用於包括瀏覽器、IOS,Android以及一些便攜需求的小型web應用系統。它具有佔用資源低,處理速度快等優勢。web
public class DBHelper extends SQLiteOpenHelper { private static final String TAG = "DatabaseHelper"; private static final String DB_NAME = "test_db";//數據庫名字 public static String TABLE_NAME = "person";// 表名 public static String FIELD_ID = "id";// 列名 public static String FIELD_NAME= "name";// 列名 private static final int DB_VERSION = 1; // 數據庫版本 public DBHelper(Context context) { super(context, DB_NAME, null, DB_VERSION); } /** * 建立數據庫 * @param db */ @Override public void onCreate(SQLiteDatabase db) { //建立表 String sql = "CREATE TABLE " + TABLE_NAME + "(" + FIELD_ID + " integer primary key autoincrement , " + FIELD_NAME + " text not null);"; try { db.execSQL(sql); } catch (SQLException e) { Log.e(TAG, "onCreate " + TABLE_NAME + "Error" + e.toString()); return; } } /** * 數據庫升級 * @param db * @param oldVersion * @param newVersion */ @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }
SQLiteOpenHelper是SQLiteDatabase的一個幫助類,主要用於操做數據庫和數據庫升級,通常須要自定義個DBHelper類來繼承SQLiteOpenHelper,具體實現參考上面的代碼。
如何建立和打開一個能夠操做的數據庫?
//獲取可寫數據庫 SQLiteDatabase db = dbHelper.getWritableDatabase(); //獲取可讀數據庫 SQLiteDatabase db = dbHelper.getReadableDatabase();
如何添加一個數據?sql
//獲取寫數據庫 SQLiteDatabase db = dbHelper.getWritableDatabase(); //生成要修改或者插入的鍵值 ContentValues cv = new ContentValues(); cv.put(DBHelper.FIELD_NAME, name); // insert 操做 db.insert(DBHelper.TABLE_NAME, null, cv); //關閉數據庫 db.close();
//生成條件語句 StringBuffer whereBuffer = new StringBuffer(); whereBuffer.append(DBHelper.FIELD_NAME).append(" = ").append("'").append(name).append("'"); //獲取寫數據庫 SQLiteDatabase db = dbHelper.getWritableDatabase(); // delete 操做 db.delete(DBHelper.TABLE_NAME, whereBuffer.toString(), null); //關閉數據庫 db.close();
//生成條件語句 StringBuffer whereBuffer = new StringBuffer(); whereBuffer.append(DBHelper.FIELD_NAME).append(" = ").append("'").append(name).append("'"); //生成要修改或者插入的鍵值 ContentValues cv = new ContentValues(); cv.put(DBHelper.FIELD_NAME, name+name); //獲取寫數據庫 SQLiteDatabase db = dbHelper.getWritableDatabase(); // update 操做 db.update(DBHelper.TABLE_NAME, cv, whereBuffer.toString(), null); //關閉數據庫 db.close();
//獲取寫數據庫 SQLiteDatabase db = dbHelper.getWritableDatabase(); //直接執行sql語句 db.execSQL(sql);//或者 //關閉數據庫 db.close();
//生成條件語句 StringBuffer whereBuffer = new StringBuffer(); whereBuffer.append(DBHelper.FIELD_NAME).append(" = ").append("'").append(name).append("'"); //指定要查詢的是哪幾列數據 String[] columns = {DBHelper.FIELD_NAME}; //獲取可讀數據庫 SQLiteDatabase db = dbHelper.getReadableDatabase(); //查詢數據庫 Cursor cursor = null; try { cursor = db.query(DBHelper.TABLE_NAME, columns, whereBuffer.toString(), null, null, null, null); if (cursor != null && cursor.getCount() > 0) { int count = cursor.getColumnCount(); String columName = cursor.getColumnName(0); String tname = cursor.getString(0); Log.e(TAG, "count = " + count + " columName = " + columName + " name = " +tname); cursor.moveToFirst(); } if (cursor != null) { cursor.close(); } } catch (SQLException e) { Log.e(TAG, "queryDatas" + e.toString()); } //關閉數據庫 db.close();
參數解說:數據庫
//獲取寫數據庫 SQLiteDatabase db = dbHelper.getWritableDatabase(); db.beginTransaction(); //手動設置開始事務 try{ //在此處理批量操做 for(int i =0;i<testCount;i++ ){ //生成要修改或者插入的鍵值 ContentValues cv = new ContentValues(); cv.put(DBHelper.FIELD_NAME, String.valueOf(i)); // insert 操做 db.insert(DBHelper.TABLE_NAME, null, cv); } db.setTransactionSuccessful(); //設置事務處理成功,不設置會自動回滾不提交 }catch(Exception e){ }finally{ db.endTransaction(); //處理完成 //關閉數據庫 db.close(); }
知識擴展:數組
1.) 數據庫數據安全問題探討?數據庫默認存放位置data/data/pakage/database目錄下,以明文存儲,獲取root權限以後能夠任意訪問。因此後續要研究一下數據庫加密。瀏覽器
總結的文章連接:http://www.cnblogs.com/whoislcj/p/5511522.html安全
2.)數據操做以後忘記關閉數據庫或者查詢數據時Cursor忘記關閉引發的內存溢出。app
文章最後附上本身寫的一個數據庫管理類:ide
/** * 數據庫管理者 - 提供數據庫封裝 * */ public class DBManager { private static final String TAG = "DatabaseManager"; // 靜態引用 private volatile static DBManager mInstance; // DatabaseHelper private DBHelper dbHelper; private DBManager(Context context) { dbHelper = new DBHelper(context.getApplicationContext()); } /** * 獲取單例引用 * * @param context * @return */ public static DBManager getInstance(Context context) { DBManager inst = mInstance; if (inst == null) { synchronized (DBManager.class) { inst = mInstance; if (inst == null) { inst = new DBManager(context); mInstance = inst; } } } return inst; } /** * 插入數據 */ public void insertData(String name) { //獲取寫數據庫 SQLiteDatabase db = dbHelper.getWritableDatabase(); //生成要修改或者插入的鍵值 ContentValues cv = new ContentValues(); cv.put(DBHelper.FIELD_NAME, name); // insert 操做 db.insert(DBHelper.TABLE_NAME, null, cv); //關閉數據庫 db.close(); } /** * 未開啓事務批量插入 * @param testCount */ public void insertDatasByNomarl(int testCount){ //獲取寫數據庫 SQLiteDatabase db = dbHelper.getWritableDatabase(); for(int i =0;i<testCount;i++ ){ //生成要修改或者插入的鍵值 ContentValues cv = new ContentValues(); cv.put(DBHelper.FIELD_NAME, String.valueOf(i)); // insert 操做 db.insert(DBHelper.TABLE_NAME, null, cv); Log.e(TAG, "insertDatasByNomarl"); } //關閉數據庫 db.close(); } /** * 測試開啓事務批量插入 * @param testCount */ public void insertDatasByTransaction(int testCount){ //獲取寫數據庫 SQLiteDatabase db = dbHelper.getWritableDatabase(); db.beginTransaction(); //手動設置開始事務 try{ //批量處理操做 for(int i =0;i<testCount;i++ ){ //生成要修改或者插入的鍵值 ContentValues cv = new ContentValues(); cv.put(DBHelper.FIELD_NAME, String.valueOf(i)); // insert 操做 db.insert(DBHelper.TABLE_NAME, null, cv); Log.e(TAG, "insertDatasByTransaction"); } db.setTransactionSuccessful(); //設置事務處理成功,不設置會自動回滾不提交 }catch(Exception e){ }finally{ db.endTransaction(); //處理完成 //關閉數據庫 db.close(); } } /** * 刪除數據 */ public void deleteData(String name) { //生成條件語句 StringBuffer whereBuffer = new StringBuffer(); whereBuffer.append(DBHelper.FIELD_NAME).append(" = ").append("'").append(name).append("'"); //獲取寫數據庫 SQLiteDatabase db = dbHelper.getWritableDatabase(); // delete 操做 db.delete(DBHelper.TABLE_NAME, whereBuffer.toString(), null); //關閉數據庫 db.close(); } /** * 刪除全部數據 */ public void deleteDatas() { String sql="delete from "+ DBHelper.TABLE_NAME; execSQL(sql); } /** * 更新數據 */ public void updateData(String name) { //生成條件語句 StringBuffer whereBuffer = new StringBuffer(); whereBuffer.append(DBHelper.FIELD_NAME).append(" = ").append("'").append(name).append("'"); //生成要修改或者插入的鍵值 ContentValues cv = new ContentValues(); cv.put(DBHelper.FIELD_NAME, name+name); //獲取寫數據庫 SQLiteDatabase db = dbHelper.getWritableDatabase(); // update 操做 db.update(DBHelper.TABLE_NAME, cv, whereBuffer.toString(), null); //關閉數據庫 db.close(); } /** * 指定條件查詢數據 */ public void queryDatas(String name){ //生成條件語句 StringBuffer whereBuffer = new StringBuffer(); whereBuffer.append(DBHelper.FIELD_NAME).append(" = ").append("'").append(name).append("'"); //指定要查詢的是哪幾列數據 String[] columns = {DBHelper.FIELD_NAME}; //獲取可讀數據庫 SQLiteDatabase db = dbHelper.getReadableDatabase(); //查詢數據庫 Cursor cursor = null; try { cursor = db.query(DBHelper.TABLE_NAME, columns, whereBuffer.toString(), null, null, null, null); while (cursor.moveToNext()) { int count = cursor.getColumnCount(); String columName = cursor.getColumnName(0); String tname = cursor.getString(0); Log.e(TAG, "count = " + count + " columName = " + columName + " name = " +tname); } if (cursor != null) { cursor.close(); } } catch (SQLException e) { Log.e(TAG, "queryDatas" + e.toString()); } //關閉數據庫 db.close(); } /** * 查詢所有數據 */ public void queryDatas(){ //指定要查詢的是哪幾列數據 String[] columns = {DBHelper.FIELD_NAME}; //獲取可讀數據庫 SQLiteDatabase db = dbHelper.getReadableDatabase(); //查詢數據庫 Cursor cursor = null; try { cursor = db.query(DBHelper.TABLE_NAME, columns, null, null, null, null, null);//獲取數據遊標 while (cursor.moveToNext()) { int count = cursor.getColumnCount(); String columeName = cursor.getColumnName(0);//獲取表結構列名 String name = cursor.getString(0);//獲取表結構列數據 Log.e(TAG, "count = " + count + " columName = " + columeName + " name = " +name); } //關閉遊標防止內存泄漏 if (cursor != null) { cursor.close(); } } catch (SQLException e) { Log.e(TAG, "queryDatas" + e.toString()); } //關閉數據庫 db.close(); } /** * 執行sql語句 */ private void execSQL(String sql){ //獲取寫數據庫 SQLiteDatabase db = dbHelper.getWritableDatabase(); //直接執行sql語句 db.execSQL(sql);//或者 //關閉數據庫 db.close(); } }