After the database has been accessed, the cursor must also remember to close
import Com.huangzhong.love_power_model. Userinfodto;import Java.util.arraylist;import Java.util.list;import Android.app.alertdialog;import Android.content.context;import Android.database.cursor;import Android.database.sqlite.sqlitedatabase;import Android.util.Log; Public classuserinfodao{Private StaticFinalintMode_private =0x0; Private StaticFinal String DATABASE ="Lovepower"; Private StaticSqlitedatabase DB; Public Static voidInsert (Context context,userinfodto model) {//id,username,birthday,phonenumber,qq,address,createdatecursor cursor; intCount =0; Try{db= Context.openorcreatedatabase (DATABASE, Mode_private,NULL); //db.execsql ("drop table UserInfo;");String sqlcheckexsit ="Select COUNT (*) as C from sqlite_master where type = ' table ' and name = ' UserInfo '"; Cursor= Db.rawquery (Sqlcheckexsit,NULL); if(Cursor.movetonext ()) {count= Cursor.getint (0); if(Count <=0) {Db.execsql ("Create Table UserInfo (Id text not null,username text,birthday text,phonenumber text,qq text,address text,createdate text);"); LOG.E ("Insertsql","2222"); }} String Insertsql="INSERT INTO UserInfo (id,username,birthday,phonenumber,qq,address,createdate)"+"values ('"+ Model.getid () +"', '"+ model.getusername () +"', '"+ model.getbirthday () +"', '"+ model.getphonenumber () +"', '"+ MODEL.GETQQ () +"', '"+ model.getaddress () +"', '"+ model.getcreatedate () +"');"; Db.execsql (Insertsql); Cursor.close (); LOG.E ("Insertsql","OK"); } finally{db.close (); } } /** Query * @param context context * @param SQL SQL query statement * @param fields Field Name Collection * @return Query result set L Ist<string> type*/ Public StaticList<userinfodto>Query (context context, String sql) {LOG.E ("Userinfodto","Fetch Data"); List<UserInfoDto> dataList =NewArraylist<userinfodto>(); cursor cursor;//id,username,birthday,phonenumber,qq,address,createdate Try{db= Context.openorcreatedatabase (DATABASE, Mode_private,NULL); Cursor= Db.rawquery (SQL,NULL); if(Cursor! =NULL) { while(Cursor.movetonext ()) {Userinfodto model=Newuserinfodto (); Model.setid (cursor.getstring (Cursor.getcolumnindex ("Id"))); Model.setusername (cursor.getstring (Cursor.getcolumnindex ("UserName"))); Model.setbirthday (cursor.getstring (Cursor.getcolumnindex ("Birthday"))); Model.setphonenumber (cursor.getstring (Cursor.getcolumnindex ("PhoneNumber"))); MODEL.SETQQ (cursor.getstring (Cursor.getcolumnindex ("QQ"))); Model.setaddress (cursor.getstring (Cursor.getcolumnindex ("Address"))); Model.setcreatedate (cursor.getstring (Cursor.getcolumnindex ("CreateDate"))); Datalist.add (model); } cursor.close ();//cursors must be closed } }Catch(Exception e) {NewAlertdialog.builder (context). SetIcon (Android. R.drawable.ic_dialog_alert). Settitle ("Database connection error:"). Setmessage ("data Access exception. "). Show (); } finally{db.close (); } returndataList; } /** Total number of data records * @param context context * @param SQL SQL query statement * @return number of records*/ Public Static intGetCount (context context, String sql) {intTotalcounty =0; Try{db= Context.openorcreatedatabase (DATABASE, Mode_private,NULL); Cursor Cursor= Db.rawquery (SQL,NULL); Cursor.movetofirst (); Totalcounty= Cursor.getint (0); Cursor.close ();//cursors must be closed}Catch(Exception e) {NewAlertdialog.builder (context). SetIcon (Android. R.drawable.ic_dialog_alert). Settitle ("Database connection error:"). Setmessage ("data Access exception. "). Show (); } finally{db.close (); } returnTotalcounty; }}Data Access Layer
Public classUserinfodto {//id,username,birthday,phonenumber,qq,address,createdate PrivateString Id; PrivateString UserName; PrivateString Birthday; PrivateString PhoneNumber; PrivateString QQ; PrivateString Address; PrivateString CreateDate; PublicString getId () {returnId; } Public voidsetId (String ID) {ID=ID; } PublicString GetUserName () {returnUserName; } Public voidsetusername (String userName) {userName=UserName; } PublicString Getbirthday () {returnBirthday; } Public voidsetbirthday (String birthday) {Birthday=birthday; } PublicString Getphonenumber () {returnPhoneNumber; } Public voidSetphonenumber (String phonenumber) {PhoneNumber=PhoneNumber; } PublicString getqq () {returnQQ; } Public voidsetqq (String QQ) {QQ=QQ; } PublicString getaddress () {returnAddress; } Public voidsetaddress (String address) {address=address; } PublicString getcreatedate () {returnCreateDate; } Public voidsetcreatedate (String createdate) {createdate=CreateDate; }}Physical Layer
The SQLite database for Android