AndroidSQLite數(shù)據(jù)庫操作實(shí)例_第1頁
AndroidSQLite數(shù)據(jù)庫操作實(shí)例_第2頁
AndroidSQLite數(shù)據(jù)庫操作實(shí)例_第3頁
AndroidSQLite數(shù)據(jù)庫操作實(shí)例_第4頁
全文預(yù)覽已結(jié)束

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

AndroidSQLite數(shù)據(jù)庫操作實(shí)例AndroidSQLiteSQL語句去操作數(shù)據(jù),SQLiteSQLMysql?樣,以下是利?SQL操作SQLite:Java代碼importjava.util.ArrayList;importjava.util.List;3.importandroid.content.Context;importandroid.database.Cursor;importandroid.database.sqlite.SQLiteDatabase;7.publicclassPersonDaoClassic{privateDBOpenHelperhelper;10.publicPersonDaoClassic(Contextcontext){helper=newDBOpenHelper(context);13. }14.publicvoidinsert(Personp){//打開可寫數(shù)據(jù)庫SQLiteDatabasedb=helper.getWritableDatabase();18.//SQL替換占位符INTOperson(name,balance)VALUES(?,?)",newObject[]{p.getName(),p.getBalance()});//釋放資源db.close();24. }25.publicvoiddelete(intid){SQLiteDatabasedb=helper.getWritableDatabase();FROMpersonWHEREid=?",newObject[]{id});db.close();30. }31.publicvoidupdate(Personp){SQLiteDatabasedb=helper.getWritableDatabase(); personSETname=?,balance=?WHEREid=?",newObject[]{p.getName(),p.getBalance(),p.getId()});db.close();36. }37.publicPersonquery(intid){SQLiteDatabasedb=helper.getReadableDatabase();40.//Cursor(ResultSet)Cursorc=db.rawQuery("SELECTname,balanceFROMpersonWHEREid=?",newString[]{String.valueOf(id)});Personp=44.//Cursor是否有下?條記錄if(c.moveToNext())//從CursorPerson對(duì)象pPerson(id,c.getString(0),c.getInt(1));49.//釋放資源c.close();db.close();returnp;54. }55.publicList<Person>queryAll(){SQLiteDatabasedb=helper.getReadableDatabase();Cursorc=db.rawQuery("SELECTid,name,balanceFROMperson",List<Person>persons=newArrayList<Person>();while(c.moveToNext())Person(c.getInt(0),c.getString(1),c.getInt(2)));c.close();db.close();returnpersons;65. }66.publicList<Person>queryPage(intpageNum,intcapacity){//開始索引Stringstart=String.valueOf((pageNum-1)*capacity);69.Stringstart=String.valueOf((pageNum-1)*capacity);70.//查詢的個(gè)數(shù)71.Stringlength=String.valueOf(capacity);72.73.SQLiteDatabasedb=helper.getReadableDatabase();74.75.//翻頁查詢語句,和MySQL中相同76.Cursorc=db.rawQuery("SELECTid,name,balanceFROMpersonLIMIT?,?",newString[]{start,length});77.List<Person>persons=newArrayList<Person>();78.while(c.moveToNext())79.persons.add(newPerson(c.getInt(0),c.getString(1),c.getInt(2)));80.c.close();81.db.close();82.returnpersons;83.}84.85.publicintqueryCount(){86.SQLiteDatabasedb=helper.getReadableDatabase();87.88.//查詢記錄條數(shù)89.Cursorc=db.rawQuery("SELECTCOUNT(*)FROMperson",null);90.c.moveToNext();91.intcount=c.getInt(0);92.c.close();93.db.close();94.returncount;95.}96.97.}除上述?法以外,android還給我們帶來了另外?種更加簡單,也是android推薦使?的?種?式,ft種?式把數(shù)據(jù)封裝在ContentValues中,因?yàn)閍ndroid編程過程中經(jīng)常會(huì)使?到已經(jīng)封裝好了數(shù)據(jù)的ContentValues,所以使?第?種?式在有些時(shí)候更加便捷,以下是代碼:...3.44.45.packagecn.itcast.sqlite;importjava.util.ArrayList;importjava.util.List;importandroid.content.ContentValues;importandroid.content.Context;importandroid.database.Cursor;importandroid.database.sqlite.SQLiteDatabase;publicclassPersonDao{privateDBOpenHelperhelper;publicPersonDao(Contextcontext){helper=newDBOpenHelper(context);}publicvoidremit(intfrom,intto,intamount){SQLiteDatabasedb=helper.getWritableDatabase();//開啟事務(wù)db.beginTransaction();try{personSETbalance=balance-?WHEREid=?",newObject[]{amount,from});personSETbalance=balance+?WHEREid=?",newObject[]{amount,to});db.setTransactionSuccessful();}catch(Exceptione){e.printStackTrace();}//結(jié)束事務(wù),將事務(wù)成功點(diǎn)前?的代碼提交db.endTransaction();db.close();}publicvoidinsert(Personp){SQLiteDatabasedb=helper.getWritableDatabase();//準(zhǔn)備數(shù)據(jù)ContentValuesvalues=newContentValues();values.put("name",p.getName());values.put("balance",p.getBalance());//ContentValuesSQLid為表中的?個(gè)列名db.insert("person","id"values);45.db.insert("person","id",values);46.47.db.close();48.}4.75.76.publicvoiddelete(intid){SQLiteDatabasedb=helper.getWritableDatabase();//執(zhí)?刪除操作,在person表中刪除id為指定值的記錄db.delete("person","id=?",newString[]{String.valueOf(id)});db.close();}publicvoidupdate(Personp){SQLiteDatabasedb=helper.getWritableDatabase();//要更新的數(shù)據(jù)ContentValuesvalues=newContentValues();values.put("name",p.getName());values.put("balance",p.getBalance());//更新person表中id為指定值的記錄db.update("person",values,"id=?",newString[]{String.valueOf(p.getId())});db.close();}publicPersonquery(intid){SQLiteDatabasedb=helper.getReadableDatabase();77.//personname和balanceWhere"id=?"idhaving,不排序,沒有分頁Cursorc=db.query(false,"person",newString[]{"name","balance"},"id=?",newString[]{String.valueOf(id)},null,null,null,null);78.Personp=//Cursor是否有下?條記錄if(c.moveToNext())//從CursorPerson對(duì)象pPerson(id,c.getString(0),c.getInt(1));//釋放資源c.close();db.close();returnp;89. }90.publicList<Person>queryAll(){SQLiteDatabasedb=helper.getReadableDatabase();93.//倒序Cursorc=db.query(false,"person",newString[]{"id","name","balance"},"idDESC",96.List<Person>persons=newArrayList<Person>();while(c.moveToNext())Person(c.getInt(0),c.getString(1),c.getInt(2)));c.close();db.close();returnpersons;103. }104.publicList<Person>queryPage(intpageNum,intcapacity){//開始索引Stringstart=String.valueOf((pageNum-1)*capacity);//查詢的個(gè)數(shù)Stringlength=String.valueOf(capacity);110.111. SQLiteDatabasedb=helper.getReadableDatabase();112.//翻頁查詢 Cursorc=db.query(false,"person",newString[]{"id","name","balance"},start+","+length);List<Person>persons=newArrayList<Person>();while(c.moveToNext())Person(c.getInt(0),c.getString(1),c.getInt(2)));c.close();db.close();returnpersons

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論