使用Cursor實現增刪改查功能java
使用Cursor增長和修改:android
ContentValues values = new ContentValues(); values.put(RadioDbConst.RadioStation.BAND, station.getBand()); values.put(RadioDbConst.RadioStation.FREQUENCY, station.getFrequencyStr()); values.put(RadioDbConst.RadioStation.IS_STERO, station.isStereoInt()); values.put(RadioDbConst.RadioStation.TYPE, station.getType()); ContentResolver resolver = mContext.getContentResolver(); StationInfo mInfo = getCollectStation(station.getBand(), station.getFrequencyStr()); if (null != mInfo) { Log.i(TAG, "collect -- has"); if (mInfo.getType() != 2 || mInfo.isStereoInt() != station.isStereoInt()) { Log.i(TAG, "collect -- update"); String selection = RadioDbConst.RadioStation.TYPE + " = ?" + " and " + RadioDbConst.RadioStation.IS_STERO + " ?"; String[] selectionArgs = { String.valueOf(2), String.valueOf(station.isStereoInt()) }; resolver.update(RadioProvider.CONTENT_URI_COLLECT, values, selection, selectionArgs); } } else { Log.i(TAG, "collect -- insert value:" + values.getAsString(RadioDbConst.SavedStation.FREQUENCY)); resolver.insert(RadioProvider.CONTENT_URI_COLLECT, values); }
使用Cursor刪除:sql
int id = mContext.getContentResolver().delete(RadioProvider.CONTENT_URI_RADIO_STATION, selection, selectionArgs);
使用Cursor查詢:數據庫
String selection = RadioDbConst.RadioStation.BAND + " = ? " + "and" + RadioDbConst.RadioStation.FREQUENCY + " = ?"; String[] selectionArgs = { fre }; StationInfo si = null; Cursor cursor = mContext.getContentResolver().query(RadioProvider.CONTENT_URI_COLLECT, null, selection, selectionArgs, null); for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) { int b = cursor.getInt(cursor.getColumnIndex(RadioDbConst.RadioStation.IS_STERO)); int type = cursor.getInt(cursor.getColumnIndex(RadioDbConst.RadioStation.TYPE)); si = new StationInfo(band, fre, b, type); Log.e("si", si.toString()); } cursor.close();
1.構造函數ide
private Context mContext; public DatabaseHelper(Context context, String name, CursorFactory factory, int version) { // 必須經過super調用父類當中的構造函數 super(context, name, null, version); mContext = context; try { createDataBase(); } catch (IOException e) { e.printStackTrace(); } } public DatabaseHelper(Context context, String name, int version) { this(context, name, null, version); } public DatabaseHelper(Context context, String name) { this(context, name, StaticCode.DATABASE_VERSION); } public DatabaseHelper(Context context) { this(context, StaticCode.DATABASE_FILEPATH + StaticCode.DATABASE_NAME); }
2.檢查數據庫是否有效函數
/** * 檢查數據庫是否有效 * * @return */ private boolean checkDataBase() { SQLiteDatabase checkDB = null; boolean isCecked = false; String myPath = StaticCode.DATABASE_FILEPATH + StaticCode.DATABASE_NAME; try { checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); if (checkDB != null) { checkDB.close(); isCecked = true; } } catch (Exception e) { } return isCecked; }
3.建立數據庫大數據
public void createDataBase() throws IOException { boolean dbExist = checkDataBase(); if (dbExist) { // 數據庫已存在,do nothing. } else { // 建立數據庫 File dir = new File(StaticCode.DATABASE_FILEPATH); if (!dir.exists()) { dir.mkdirs(); } File dbf = new File(StaticCode.DATABASE_FILEPATH + StaticCode.DATABASE_NAME); if (dbf.exists()) { dbf.delete(); } SQLiteDatabase.openOrCreateDatabase(dbf, null); } }
4.將本地資源文件夾中的數據庫複製到剛剛建立的空數據庫this
/** * Copies your database from your local assets-folder to the just created empty database in the * system folder, from where it can be accessed and handled. * This is done by transfering bytestream. * */ private void copyDataBase() throws IOException{ //Open your local db as the input stream InputStream myInput = myContext.getAssets().open(ASSETS_NAME); // Path to the just created empty db String outFileName = DB_PATH + DB_NAME; //Open the empty db as the output stream OutputStream myOutput = new FileOutputStream(outFileName); //transfer bytes from the inputfile to the outputfile byte[] buffer = new byte[1024]; int length; while ((length = myInput.read(buffer))>0){ myOutput.write(buffer, 0, length); } //Close the streams myOutput.flush(); myOutput.close(); myInput.close(); } //複製assets下的大數據庫文件時用這個 private void copyBigDataBase() throws IOException{ InputStream myInput; String outFileName = DB_PATH + DB_NAME; OutputStream myOutput = new FileOutputStream(outFileName); for (int i = ASSETS_SUFFIX_BEGIN; i < ASSETS_SUFFIX_END+1; i++) { myInput = myContext.getAssets().open(ASSETS_NAME + "." + i); byte[] buffer = new byte[1024]; int length; while ((length = myInput.read(buffer))>0){ myOutput.write(buffer, 0, length); } myOutput.flush(); myInput.close(); } myOutput.close(); }
注意:spa
execSQL不支持帶;的多條SQL語句,只能一條一條的執行,暈了好久才明白,見execSQL的源碼註釋 (Multiple statements separated by ;s are not supported.).net
將把assets下的數據庫文件直接複製到DB_PATH,但數據庫文件大小限制在1M如下, 若是有超過1M的大文件,則須要先分割爲N個小文件,而後使用copyBigDatabase()替換copyDatabase()
5.建立表
db.execSQL("CREATE TABLE IF NOT EXISTS " + RadioDbConst.RadioStation.TABLE_NAME + " (" + RadioDbConst.RadioStation._ID + " integer primary key autoincrement," + RadioDbConst.RadioStation.FREQUENCY + " text," + RadioDbConst.RadioStation.TITLE + " text," + RadioDbConst.RadioStation.BAND + " integer," + RadioDbConst.RadioStation.IS_STERO + " integer," + RadioDbConst.RadioStation.TYPE + " integer);");
package cn.arthur.common; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteException; import android.database.sqlite.SQLiteOpenHelper; /** * @author Joshua * 用法: * DBHelper dbHelper = new DBHelper(this); * dbHelper.createDataBase(); * SQLiteDatabase db = dbHelper.getWritableDatabase(); * Cursor cursor = db.query() * db.execSQL(sqlString); * 注意:execSQL不支持帶;的多條SQL語句,只能一條一條的執行,暈了好久才明白 * 見execSQL的源碼註釋 (Multiple statements separated by ;s are not supported.) * 將把assets下的數據庫文件直接複製到DB_PATH,但數據庫文件大小限制在1M如下 * 若是有超過1M的大文件,則須要先分割爲N個小文件,而後使用copyBigDatabase()替換copyDatabase() */ public class DBHelper extends SQLiteOpenHelper { //用戶數據庫文件的版本 private static final int DB_VERSION = 1; //數據庫文件目標存放路徑爲系統默認位置,cn.arthur.examples 是你的包名 private static String DB_PATH = "/data/data/cn.arthur.examples/databases/"; /* //若是你想把數據庫文件存放在SD卡的話 private static String DB_PATH = android.os.Environment.getExternalStorageDirectory().getAbsolutePath() + "/arthurcn/drivertest/packfiles/"; */ private static String DB_NAME = "hello.db"; private static String ASSETS_NAME = "hello.db"; private SQLiteDatabase myDataBase = null; private final Context myContext; /** * 若是數據庫文件較大,使用FileSplit分割爲小於1M的小文件 * 此例中分割爲 hello.db.101 hello.db.102 hello.db.103 */ //第一個文件名後綴 private static final int ASSETS_SUFFIX_BEGIN = 101; //最後一個文件名後綴 private static final int ASSETS_SUFFIX_END = 103; /** * 在SQLiteOpenHelper的子類當中,必須有該構造函數 * @param context 上下文對象 * @param name 數據庫名稱 * @param factory 通常都是null * @param version 當前數據庫的版本,值必須是整數而且是遞增的狀態 */ public DBHelper(Context context, String name, CursorFactory factory, int version) { //必須經過super調用父類當中的構造函數 super(context, name, null, version); this.myContext = context; } public DBHelper(Context context, String name, int version){ this(context,name,null,version); } public DBHelper(Context context, String name){ this(context,name,DB_VERSION); } public DBHelper (Context context) { this(context, DB_PATH + DB_NAME); } public void createDataBase() throws IOException{ boolean dbExist = checkDataBase(); if(dbExist){ //數據庫已存在,do nothing. }else{ //建立數據庫 try { File dir = new File(DB_PATH); if(!dir.exists()){ dir.mkdirs(); } File dbf = new File(DB_PATH + DB_NAME); if(dbf.exists()){ dbf.delete(); } SQLiteDatabase.openOrCreateDatabase(dbf, null); // 複製asseets中的db文件到DB_PATH下 copyDataBase(); } catch (IOException e) { throw new Error("數據庫建立失敗"); } } } //檢查數據庫是否有效 private boolean checkDataBase(){ SQLiteDatabase checkDB = null; String myPath = DB_PATH + DB_NAME; try{ checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); }catch(SQLiteException e){ //database does't exist yet. } if(checkDB != null){ checkDB.close(); } return checkDB != null ? true : false; } /** * Copies your database from your local assets-folder to the just created empty database in the * system folder, from where it can be accessed and handled. * This is done by transfering bytestream. * */ private void copyDataBase() throws IOException{ //Open your local db as the input stream InputStream myInput = myContext.getAssets().open(ASSETS_NAME); // Path to the just created empty db String outFileName = DB_PATH + DB_NAME; //Open the empty db as the output stream OutputStream myOutput = new FileOutputStream(outFileName); //transfer bytes from the inputfile to the outputfile byte[] buffer = new byte[1024]; int length; while ((length = myInput.read(buffer))>0){ myOutput.write(buffer, 0, length); } //Close the streams myOutput.flush(); myOutput.close(); myInput.close(); } //複製assets下的大數據庫文件時用這個 private void copyBigDataBase() throws IOException{ InputStream myInput; String outFileName = DB_PATH + DB_NAME; OutputStream myOutput = new FileOutputStream(outFileName); for (int i = ASSETS_SUFFIX_BEGIN; i < ASSETS_SUFFIX_END+1; i++) { myInput = myContext.getAssets().open(ASSETS_NAME + "." + i); byte[] buffer = new byte[1024]; int length; while ((length = myInput.read(buffer))>0){ myOutput.write(buffer, 0, length); } myOutput.flush(); myInput.close(); } myOutput.close(); } @Override public synchronized void close() { if(myDataBase != null){ myDataBase.close(); } super.close(); } /** * 該函數是在第一次建立的時候執行, * 其實是第一次獲得SQLiteDatabase對象的時候纔會調用這個方法 */ @Override public void onCreate(SQLiteDatabase db) { } /** * 數據庫表結構有變化時採用 */ @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }