Android SQLite使用

创建表
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);");

使用Cursor实现增删改查功能

使用Cursor增加和修改:

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删除:

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();


你可能感兴趣的:(android,sqlite)