android SQLite 鎬荤粨
銆愯浆銆?android SQLite 鎬荤粨
鎬荤粨SQLite鑺变簡濂藉鏃堕棿锛屽弬鑰冧簡涓嶅皯璧勬枡銆備互涓嬬煡璇嗗鏋滆寰楃溂鐔燂紝涓嶈鎬€鐤戯紝杩欎簺鎬荤粨涓殑浠g爜锛岃櫧鐒跺緢澶氫笉鏄垜鑷繁鍐欑殑锛屼絾鏄繖浜涗唬鐮侀兘鏄€氱敤鐨勩€係QLite瀛樺偍骞朵笉闅撅紝灏辨槸楹荤儲鑰屽凡锛屽笇鏈涗笅闈㈢殑鎬荤粨鑳藉瀵逛綘浠湁甯姪銆?/p>
Android涓瘡涓€涓暟鎹簱瀵瑰垱寤哄畠鐨勫簲鐢ㄧ▼搴忓寘濂椾欢鏉ヨ閮芥槸绉佹湁鐨勶紝榛樿鎯呭喌涓嬪叾浠栧簲鐢ㄧ▼搴忔棤娉曠洿鎺ヨ闂绉佹湁鏁版嵁搴撱€傛墍鏈夌殑鏁版嵁搴撴枃浠跺瓨鏀惧湪鎵嬫満涓殑/data/data/package_name/databases璺緞涓嬶紝浠ヤ笅鏄父鐢ㄧ殑涓庢暟鎹簱鐩稿叧鐨勫嚱鏁帮細
杩斿洖鍊?/strong> | 鍑芥暟 | 澶囨敞 |
static SQLiteDatabase | create(SQLiteDatabase.CursorFactory factory) | 鍒涘缓涓€涓暟鎹簱 factory锛氬彲閫夌殑鏁版嵁搴撴父鏍囧伐鍘傜被锛屽綋鏌ヨ(query)琚彁浜ゆ椂锛岃瀵硅薄浼氳璋冪敤鏉ュ疄渚嬪寲涓€涓父鏍囥€?/p> |
static SQLiteDatabase | openDatabase(String path,SQLiteDatabase.CursorFactory factory,int flags) | 鏍规嵁鎻愪緵鐨勬ā寮忔墦寮€涓€涓暟鎹簱 path锛氭墦寮€鎴栧垱寤虹殑鏁版嵁搴撴枃浠?/p> factory锛氬彲閫夌殑鏁版嵁搴撴父鏍囧伐鍘傜被锛屽綋鏌ヨ(query)琚彁浜ゆ椂锛岃瀵硅薄浼氳璋冪敤鏉ュ疄渚嬪寲涓€涓父鏍囥€傞粯璁や负null銆?/p> flags锛氭帶鍒舵暟鎹簱鐨勮闂ā寮忋€備富瑕佹湁浠ヤ笅鍑犵妯″紡锛?/p> l OPEN_READWRITE l OPEN_READONLY l CREATE_IF_NECESSARY l NO_LOCALIZED_COLLATORS |
static SQLiteDatabase | openOrCreateDatabase(File file, SQLiteDatabase.CursorFactory factory) | 绛夊悓浜巓penDatabase(file.getPath(), factory, CREATE_IF_NECESSARY) |
static SQLiteDatabase | openOrCreateDatabase(String path, SQLiteDatabase.CursorFactory factory) | 绛夊悓浜巓penDatabase(path,factory, CREATE_IF_NECESSARY) |
void | close() | 鍏抽棴鏁版嵁搴?/p> |
boolean | deleteDatabase(String name) | 鍒犻櫎鎸囧畾鐨勬暟鎹簱 name锛氳鍏抽棴鐨勬暟鎹簱鐨勫悕瀛?/p> |
绀轰緥浠g爜濡備笅锛?/p>
//鍒涘缓鏁版嵁搴?/p> SQLiteDatabase mydataBase=SQLiteDatabase.create(new CursorFactory(){ //宸ュ巶绫伙紝涓€涓彲閫夊伐鍘傜被锛屽綋鏌ヨ鏃惰皟鐢ㄦ潵瀹炰緥鍖栦竴涓父鏍?/p> @Override public Cursor newCursor(SQLiteDatabase db, SQLiteCursorDriver masterQuery, String editTable, SQLiteQuery query) { // TODO Auto-generated method stub return null; } }); //鍒涘缓鎴栨墦寮€鏁版嵁搴?/p> SQLiteDatabase myDataBase=this.openOrCreateDatabase("myDataBase.db", MODE_PRIVATE, new CursorFactory(){ //鍒涘缓鏂扮殑鏁版嵁搴擄紝鍚嶇ОmyDatabase锛屾ā寮廙ODE_PRIVATE锛屽彲閫夌殑娓告爣宸ュ巶绫?/p> @Override public Cursor newCursor(SQLiteDatabase db, SQLiteCursorDriver masterQuery, String editTable, SQLiteQuery query) { // TODO Auto-generated method stub return null; } }); SQLiteDatabase myDataBase=this.openOrCreateDatabase("myDataBase.db",MODE_PRIVATE,null); //鍏抽棴鏁版嵁搴?/p> myDataBase.close(); //鍒犻櫎鎸囧畾鍚嶇О鐨勬暟鎹簱 this.deleteDatabase(鈥渕yDatabase.db鈥?; |
鍦ㄥ鏁版嵁搴撲腑鐨勮〃杩涜鐩稿叧鎿嶄綔鏃讹紝鍙互浣跨敤闈炴煡璇?/strong>鐨別xecSQL(String sql)鏉ユ墽琛屻€傜ず渚嬩唬鐮佸涓嬶細
String CREATE_TABLE = 鈥渃reate table table1 (_id integer primary key autoincrement,number integer,data text)鈥? //鍒涘缓琛?/p> myDataBase.execSQL(CREATE_TABLE); |
Android鎻愪緵浜嗛潪SQL鎸囦护鐨勫鏁版嵁琛ㄨ繘琛屾搷浣滅殑鏂规硶锛屼互涓嬭〃鏍兼槸涓€浜涘父鐢ㄦ柟娉曪細
杩斿洖鍊?/strong> | 鍑芥暟 | 澶囨敞 |
long | insert(String table, String nullColumnHack, ContentValues values) | 鍚戣〃涓彃鍏ヤ竴鏉℃暟鎹?/p> table锛氭兂瑕佹彃鍏ユ暟鎹殑琛ㄥ悕 nullColumnHack锛歋QL涓嶅厑璁告彃鍏ョ┖琛岋紝鍒濆鍖栧€间负绌烘椂锛岃繖涓€鍒楀皢浼氳鏄剧ず鍦拌祴涓€涓猲ull鍊?/p> values锛氳鎻掑叆鐨勫€硷紝鍏蜂綋瑙i噴瑙佽〃鏍煎悗鐨勮В閲?/p> |
int | delete(String table, String whereClause, String[] whereArgs) | 浠庤〃涓垹闄や竴琛屾暟鎹?/p> table锛氭兂瑕佸垹闄ゆ暟鎹殑琛ㄥ悕 whereClause锛氬彲閫夌殑where瀛愬彞锛屽鏋滃叾鍊间负null锛屽皢浼氬垹闄ゆ墍鏈夌殑琛屻€?/p> whereArgs锛氬綋鍦╳hereClause涓寘鍚€?鈥濇椂锛屽鏋渨hereArgs鐨勫€间笉涓簄ull锛屽垯杩欎釜鏁扮粍涓殑鍊煎皢渚濇鏇挎崲whereClause涓嚭鐜扮殑鈥?鈥?/p> |
Cursor | query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit) | 鏌ヨ鏁版嵁琛?/p> table锛氳鏌ヨ鏁版嵁鐨勮〃鍚?/p> columns锛氳杩斿洖鐨勫垪鐨勫垪鍚嶆暟缁?/p> selection锛氬彲閫夌殑where瀛愬彞 锛屽鏋滃叾鍊间负null锛屽皢浼氳繑鍥炴墍鏈夌殑琛?/p> selectionArgs锛氬綋鍦╯election涓寘鍚€?鈥濇椂锛屽鏋渟electionArgs鐨勫€间笉涓簄ull锛屽垯杩欎釜鏁扮粍涓殑鍊煎皢渚濇鏇挎崲selection涓嚭鐜扮殑鈥?鈥?/p> groupBy锛氬彲閫夌殑group by瀛愬彞锛屽鏋滃叾鍊间负null锛屽皢涓嶄細瀵硅杩涜鍒嗙粍 having锛氬彲閫夌殑having瀛愬彞锛屽鏋滃叾鍊间负null锛屽皢浼氬寘鍚墍鏈夌殑鍒嗙粍 orderBy锛氬彲閫夌殑order by瀛愬彞锛屽鏋滃叾鍊间负null锛屽皢浼氫娇鐢ㄩ粯璁ょ殑鎺掑簭瑙勫垯 limit锛氬彲閫夌殑limit瀛愬彞锛屽鏋滃叾鍊间负null锛屽皢涓嶄細鍖呭惈limit瀛愬彞 鍏充簬杩斿洖鍊肩被鍨婥ursor灏嗗湪琛ㄦ牸鍚庨潰杩涜瑙i噴 |
int | update(String table, ContentValues values, String whereClause, String[] whereArgs) | 淇敼琛ㄤ腑鐨勬暟鎹?/p> table锛氭兂瑕佷慨鏀规暟鎹殑琛ㄥ悕 values锛氳鏇存柊鐨勫€硷紝鍏蜂綋瑙i噴瑙佽〃鏍煎悗鐨勮В閲?/p> whereClause锛氬彲閫夌殑where瀛愬彞锛屽鏋滃叾鍊间负null锛屽皢浼氫慨鏀规墍鏈夌殑琛屻€?/p> whereArgs锛氬綋鍦╳hereClause涓寘鍚€?鈥濇椂锛屽鏋渨hereArgs鐨勫€间笉涓簄ull锛屽垯杩欎釜鏁扮粍涓殑鍊煎皢渚濇鏇挎崲whereClause涓嚭鐜扮殑鈥?鈥?/p> |
ContentValues涓昏鏄瓨鏀捐〃涓殑鏁版嵁娈碉紝浠ュ強鍏跺搴旂殑鍊硷紝涓嶩ashtable涓€鏍烽噰鐢ㄥ悕鍊煎鐨勫舰寮忓瓨鍌紝浣嗘槸瀹冨瓨鍌ㄧ殑鍚嶅€煎涓紝鍚嶆槸涓€涓猄tring绫诲瀷锛屽€兼槸鍩烘湰鏁版嵁绫诲瀷銆傚叾浣跨敤绀轰緥濡備笅锛?/p>
ContentValues args = new ContentValues(); args.put(KEY_TITLE, title); args.put(KEY_BODY, body); myDataBase.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null); |
Android閲囩敤娓告爣瀵逛粠鏁版嵁搴撲腑鏌ヨ鍑烘潵鐨勭粨鏋滆繘琛岄殢鏈虹殑璇诲啓璁块棶锛屽湪鏌ヨ鏁版嵁搴撳悗锛屽皢缁撴灉杩斿洖缁欐父鏍?鍗砤ndroid.database.Cursor)锛岃繖鏄煡璇㈢粨鏋滅殑璁板綍闆嗭紝绀烘剰鍥惧涓嬶細
_id | someNumber | |
1 | 8 | |
2 | 10 | |
3 | 2 |
Cursor绫诲父瑙佺殑鏂规硶濡備笅锛?/p>
杩斿洖鍊?/strong> | 鍑芥暟 | 澶囨敞 |
boolean | move(int offset) | 浠ュ綋鍓嶄綅缃负鍙傝€冿紝灏咰ursor绉诲姩鎸囧畾鏁扮洰鐨勪綅缃?鐩稿浣嶇疆) |
boolean | moveToPosition(int position) | 灏咰ursor绉诲姩鍒版寚瀹氫綅缃?缁濆浣嶇疆) |
boolean | moveToNext() | 灏咰ursor鍚戝墠绉诲姩涓€涓綅缃?/p> |
boolean | moveToLast() | 灏咰ursor鍚戝悗绉诲姩涓€涓綅缃?/p> |
boolean | moveToFirst() | 灏咰ursor绉诲姩鍒扮涓€琛?/p> |
boolean | isBeforeFirst() | 杩斿洖Cursor鏄惁鎸囧悜绗竴椤规暟鎹箣鍓?/p> |
boolean | isAfterLast() | 杩斿洖Cursor鏄惁鎸囧悜鏈€鍚庝竴椤规暟鎹箣鍚?/p> |
boolean | isClosed() | 杩斿洖Cursor鏄惁鍏抽棴 |
boolean | isFirst() | 杩斿洖Cursor鏄惁鎸囧悜绗竴椤规暟鎹?/p> |
boolean | isLast() | 杩斿洖Cursor鏄惁鎸囧悜鏈€鍚庝竴椤规暟鎹?/p> |
boolean | isNull(int columnIndex) | 杩斿洖鎸囧畾浣嶇疆鐨勫€兼槸鍚︿负null |
int | getCount() | 杩斿洖鎬荤殑椤圭洰鎬绘暟 |
int | getInt(int columnIndex) | 杩斿洖鎸囧畾鍒椾腑鐨勬暟鎹殑int鍨嬭〃绀?/p> |
int | getColumnIndex(String columnName) | 鎸夌粰瀹氱殑鍒楃殑鍚嶅瓧杩斿洖鍒楃殑绱㈠紩鍊硷紝濡傛灉涓嶅瓨鍦ㄥ垯杩斿洖 -1 |
瑕佸垱寤轰竴涓狢ursor(娓告爣)锛屽繀椤绘墽琛屼竴涓煡璇紝閫氳繃SQL浣跨敤rawQuery()鏂规硶鎴栨槸鏇寸簿蹇冪殑query()鏂规硶锛岃€屼笉鑳戒娇鐢╡xecSQL(String sql)鏂规硶銆備互涓嬩负Cursor鐨勪娇鐢ㄧず渚嬶細
Cursor cur=myDataBase.rawQuery("select * from test", null); if(cur!=null){//娓告爣涓嶄负绌?/p> //杩斿洖缁欏畾鍚嶇О鐨勫垪鐨勫熀浜?寮€濮嬬殑index锛屽鏋滆灞炴€у垪涓嶅瓨鍦ㄥ垯杩斿洖-1 //閫氳繃瀹冧滑鐨刬ndex鏉ユ绱㈠睘鎬у€?/p> int numColumn=cur.getColumnIndex("someNumber"); if(cur.moveToFirst()){ //cur.moveToFirst()璁╂父鏍囨寚鍚戠涓€琛岋紝濡傛灉娓告爣鎸囧悜绗竴琛岋紝鍒欒繑鍥瀟rue do { int num=cur.getInt(numColumn);//鑾峰緱褰撳墠琛岃灞炴€х殑鍊?/p> /*Cursor鎻愪緵浜嗕笉鍚岀殑鏂规硶鏉ュ洖绱笉鍚岀殑鏁版嵁绫诲瀷锛屼緥濡俫etInt(int index)/getString(int index)绛夌瓑*/ /*鍋氫竴浜涗簨鎯?/ } while (cur.moveToNext()); /*娓告爣绉诲姩鍒颁笅涓€琛岋紝濡傛灉娓告爣宸茬粡鍒拌揪缁撴灉闆嗕腑鐨勬渶鍚庯紝鍗虫病鏈夎鍙互绉诲姩鏃讹紝鍒欒繑鍥瀎alse*/ //鍏朵粬鍙兘绉诲姩鐨勬槸 moveToPrevious() 鍜宮oveToFirst()鏂规硶 } } |
鍦ㄥ疄闄呬娇鐢ㄧ殑鏃跺€欙紝閫氬父閮借灏佽涓€涓户鎵縎QLiteOpenHelper绫荤殑鏁版嵁搴撴搷浣滅被銆係QLiteOpenHelper绫绘槸涓€涓娊璞$殑杈呭姪绫伙紝涓昏鐢ㄦ潵鐢熸垚涓€涓暟鎹簱骞跺鏁版嵁搴撶殑鐗堟湰杩涜绠$悊锛屽湪SQLiteOpenHelper绫荤殑鏋勯€犳柟娉曚腑鍒嗗埆浼犲叆Context銆佹暟鎹簱鍚嶇О锛孋ursorFactory(涓€鑸紶鍏ull锛屽惁鍒欎负榛樿鏁版嵁搴?锛屾暟鎹簱鐗堟湰鍙?涓嶈兘涓鸿礋鏁?銆傚湪SQLiteOpenHelper涓鍏堟墽琛岀殑鏄痮nCreate鏂规硶(褰撴暟鎹簱绗竴娆″垱寤烘椂)锛屼竴鑸湪杩欎釜鏂规硶閲岀敓鎴愭暟鎹〃銆傝娉ㄦ剰鐨勬槸锛屽湪鏋勯€犲嚱鏁版椂骞舵病鏈夌湡姝e垱寤烘暟鎹簱锛岃€屾槸鍦ㄨ皟鐢╣etWriteableDatabase()鎴栬€単etReadableDatabase()鏂规硶鐨勬椂鍊欑郴缁熸墠浼氱湡姝e垱寤烘暟鎹簱锛屽鏋滃綋鏃剁郴缁熶腑涓嶅瓨鍦ㄨ繖涓暟鎹簱锛岀郴缁熶細鑷姩鐢熸垚涓€涓暟鎹簱锛岀劧鍚庤繑鍥濻QLiteDatabase瀵硅薄銆傚湪缁ф壙杩欎釜绫荤殑鏃跺€欙紝瑕佸疄鐜伴噷闈㈢殑3涓柟娉曪紝鍏朵腑鍓嶄袱涓柟娉曟槸蹇呴』閲嶅啓鐨勩€傝濡備笅琛ㄦ牸锛?/p>
杩斿洖鍊?/strong> | 鍑芥暟 | 澶囨敞 |
public void | onCreate(SQLiteDatabase db) | 鍦ㄦ暟鎹簱绗竴娆$敓鎴愭椂浼氳皟鐢ㄨ繖涓柟娉曪紝涓€鑸垜浠湪杩欎釜鏂规硶閲岀敓鎴愭暟鎹〃 |
public void | onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion) | 褰撴暟鎹簱闇€瑕佸崌绾ф椂锛岀郴缁熶細鑷姩璋冪敤杩欎釜鏂规硶锛屼竴鑸垜浠湪杩欎釜鏂规硶閲屽垹闄ゆ暟鎹〃锛屽苟寤虹珛鏂扮殑鏁版嵁琛紝骞舵牴鎹疄闄呴渶姹傚仛鍏朵粬鐨勬搷浣?/p> |
public void | onOpen(SQLiteDatabase db) | 杩欐槸鎵撳紑鏁版嵁搴撴椂鐨勫洖璋冨嚱鏁帮紝涓€鑸笉浼氱敤鍒?/p> |
浠ヤ笅绀轰緥浠g爜璇︾粏缁欏嚭涓€灏佽濂界殑鏁版嵁搴撴搷浣滅被锛?/p> public class MyDbHelper {
public static final String KEY_TITLE = "title"; //鏁版嵁琛ㄥ瓧娈?/span>
public static final String KEY_BODY = "body"; //鏁版嵁琛ㄥ瓧娈?/span>
public static final String KEY_ROWID = "_id"; //鏁版嵁琛ㄥ瓧娈?/span>
private DatabaseHelper mDbHelper; //SQLiteOpenHelper瀹炰緥瀵硅薄
private SQLiteDatabase mDb; //鏁版嵁搴撳疄渚嬪璞?/span>
//鏁版嵁琛ㄥ垱寤鸿鍙?/span>
private static final String DATABASE_CREATE = "create table notes (_id integer primary key autoincrement, "
+ "title text not null, body text not null);";
private static final String DATABASE_NAME = "data"; //鏁版嵁搴撳悕
private static final String DATABASE_TABLE = "notes"; //鏁版嵁搴撹〃鍚?/span>
private static final int DATABASE_VERSION = 2; //鏁版嵁搴撶増鏈彿
private final Context mCtx; //涓婁笅鏂囧疄渚?/span>
private static class DatabaseHelper extends SQLiteOpenHelper { //鏁版嵁搴撹緟鍔╃被
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS notes");
onCreate(db);
}
}
/**
* Constructor - takes the context to allow the database to be opened/created
*
* @param ctx the Context within which to work
*/
public MyDbHelper(Context ctx) {
this.mCtx = ctx;
}
/**
* Open the notes database. If it cannot be opened, try to create a new instance of the database.
* If it cannot be created, throw an exception to signal the failure
*
* @return this (self reference, allowing this to be chained in an initialization call)
* @throws SQLException if the database could be neither opened or created
*/
public MyDbHelper open() throws SQLException {
mDbHelper = new DatabaseHelper(mCtx);
mDb = mDbHelper.getWritableDatabase();
return this;
}
public void close() {
mDbHelper.close();
}
/**
* Create a new note using the title and body provided. If the note is successfully created
* return the new rowId for that note, otherwise return a -1 to indicate failure.
*
* @param title the title of the note
* @param body the body of the note
* @return rowId or -1 if failed
*/
public long createNote(String title, String body) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_TITLE, title);
initialValues.put(KEY_BODY, body);
return mDb.insert(DATABASE_TABLE, null, initialValues);
}
/**
* Delete the note with the given rowId
*
* @param rowId id of note to delete
* @return true if deleted, false otherwise
*/
public boolean deleteNote(long rowId) {
return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
}
/**
* Return a Cursor over the list of all notes in the database
*
* @return Cursor over all notes
*/
public Cursor fetchAllNotes() {
return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE,KEY_BODY}, null, null, null, null, null);
}
/**
* Return a Cursor positioned at the note that matches the given rowId
*
* @param rowId id of note to retrieve
* @return Cursor positioned to matching note, if found
* @throws SQLException if note could not be found/retrieved
*/
public Cursor fetchNote(long rowId) throws SQLException {
Cursor mCursor = mDb.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,KEY_TITLE,
KEY_BODY}, KEY_ROWID + "=" + rowId, null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
/**
* Update the note using the details provided. The note to be updated is specified using the rowId,
*and it is altered to use the title and body values passed in
*
* @param rowId id of note to update
* @param title value to set note title to
* @param body value to set note body to
* @return true if the note was successfully updated, false otherwise
*/
public boolean updateNote(long rowId, String title, String body) {
ContentValues args = new ContentValues();
args.put(KEY_TITLE, title);
args.put(KEY_BODY, body);
return mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
}
}
杞嚜锛?a href="http://www.cnblogs.com/jerrychoi/archive/2010/04/15/1712704.html">http://www.cnblogs.com/jerrychoi/archive/2010/04/15/1712704.html
更多相关文章
- 代码中设置drawableleft
- android 3.0 隐藏 系统标题栏
- Android开发中activity切换动画的实现
- Android(安卓)学习 笔记_05. 文件下载
- Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
- 技术博客汇总
- android 2.3 wifi (一)
- AndRoid Notification的清空和修改
- Android中的Chronometer