This article reprinted to http://blog.csdn.net/chen505358119/article/details/9289489Category: iOS2013-07-10 14:05 2471 People read comments (0) favorite reports
Download the Fmdb's original code from the Web, drag it into the project, and add Libsqlite3.dylib in link Binary with libraries to use the
The implementation is as follows:
[Plain]View Plaincopyprint?
- #import "ViewController.h"
- #import "FMDatabase.h"
- #import "FMDatabaseAdditions.h"
- #import "FMDatabaseQueue.h"
- @interface Viewcontroller ()
- @end
- @implementation Viewcontroller
- -(void) viewdidload
- {
- [Super Viewdidload];
- Additional setup after loading the view, typically from a nib.
- }
- -(void) didreceivememorywarning
- {
- [Super didreceivememorywarning];
- Dispose of any resources the can be recreated.
- }
- -(void) Dealloc {
- [_nametext release];
- [_agetext release];
- [_sextext release];
- [_showlabel release];
- [Super Dealloc];
- }
- -(void) Viewdidunload {
- [Self setnametext:nil];
- [Self setagetext:nil];
- [Self setsextext:nil];
- [Self setshowlabel:nil];
- [Super Viewdidunload];
- }
- Save
- -(Ibaction) SAVEBTN: (ID) Sender {
- fmdatabase* database=[fmdatabase databasewithpath:[self DatabasePath];
- if (![ Database Open]) {
- NSLog (@ "Open database Failed");
- Return
- }
- if (![ Database tableexists:@ "User"]) {
- [Database executeupdate:@ "CREATE TABLE User (ID integer primary key autoincrement not null,name text,age integer,sex text) "];
- }
- BOOL insert=[database executeupdate:@ "insert into User (Name,age,sex) VALUES (?,?,?)", _nametext.text,[nsnumber Numberwithinteger:[_agetext.text Integervalue]],_sextext.text];
- The following comments also enable the insertion of data, except that it is passed into the dictionary
- nsmutabledictionary* Argsdict=[nsmutabledictionary Dictionary];
- [Argsdict setobject:_nametext.text forkey:@ "name"];
- [Argsdict setobject:_agetext.text forkey:@ "age"];
- [Argsdict setobject:_sextext.text forkey:@ "Sex"];
- BOOL insert=[database executeupdate:@ "insert into User (Name,age,sex) VALUES (: Name,:age,:sex)" Withparameterdiction ARY:ARGSDICT];
- if (insert) {
- uialertview* Alert=[[uialertview alloc] initwithtitle:@ "Prompt" message:@ "saved successfully" Delegate:nil Cancelbuttontitle:nil otherbuttontitles:@ "OK", nil];
- [Alert show];
- [Alert release];
- }else{
- NSLog (@ "Insert failed");
- }
- [Database Close];
- This section of the note below is also the creation of a table except that it is multithreaded secure, and the above is unsafe in multiple threads
- fmdatabasequeue* queue=[fmdatabasequeue databasequeuewithpath:[self DatabasePath];
- [Queue indatabase:^ (fmdatabase* database) {
- if (![ Database tableexists:@ "User"]) {
- [Database executeupdate:@] CREATE TABLE user (ID integer primary key autoincrement not null,name Text,age int Eger,sex text) "];
- // }
- BOOL insert=[database executeupdate:@ "insert into User (Name,age,sex) VALUES (?,?,?)", _nametext.text,[nsnumber N Umberwithinteger:[_agetext.text Integervalue]],_sextext.text];
- if (insert) {
- uialertview* Alert=[[uialertview alloc] initwithtitle:@ "Prompt" message:@ "saved successfully" Delegate:nil cancelbuttontitle:n Il otherbuttontitles:@ "OK", nil];
- [Alert show];
- [Alert release];
- }else{
- NSLog (@ "Insert failed");
- // }
- //
- // }];
- [Queue close];
- }
- To display data from all databases
- -(Ibaction) SHOWBTN: (ID) Sender {
- fmdatabase* database=[fmdatabase databasewithpath:[self DatabasePath];
- if (![ Database Open]) {
- Return
- }
- Note that the fmresultset here can not be closed, because Fmresultset automatically shuts down when the database is closed
- fmresultset* resultset=[database executequery:@ "select* from user"];
- nsstring* [email protected] "";
- while ([ResultSet next]) {
- nsstring* name=[resultset stringforcolumn:@ "name"];
- Nsinteger age=[resultset intforcolumn:@ "age"];
- nsstring* sex=[resultset stringforcolumn:@ "Sex"];
- Str=[str stringbyappendingformat:@ "name:%@,age:%d,sex:%@\n", Name,age,sex];
- NSLog (@ "name:%@,age:%d,sex:%@\n", name,age,sex);
- }
- _SHOWLABEL.TEXT=STR;
- [Database Close];
- }
- Inquire
- -(Ibaction) CHECKBTN: (ID) Sender {
- fmdatabase* database=[fmdatabase databasewithpath:[self DatabasePath];
- if (![ Database Open]) {
- Return
- }
- fmresultset* resultset=[database executequery:@ "select* from user where name =?", @ "Chen"];
- nsstring* [email protected] "";
- while ([ResultSet next]) {
- nsstring* name=[resultset stringforcolumn:@ "name"];
- Nsinteger age=[resultset intforcolumn:@ "age"];
- nsstring* sex=[resultset stringforcolumn:@ "Sex"];
- Str=[str stringbyappendingformat:@ "name:%@,age:%d,sex:%@\n", Name,age,sex];
- }
- _SHOWLABEL.TEXT=STR;
- [Database Close];
- }
- Delete
- -(Ibaction) DELETEBTN: (ID) Sender {
- fmdatabase* database=[fmdatabase databasewithpath:[self DatabasePath];
- if (![ Database Open]) {
- Return
- }
- BOOL delete=[database executeupdate:@ "Delete from user where name =?", @ "Chen"];
- if (delete) {
- uialertview* Alert=[[uialertview alloc] initwithtitle:@ "Prompt" message:@ "Delete succeeded" Delegate:nil Cancelbuttontitle:nil otherbuttontitles:@ "OK", nil];
- [Alert show];
- [Alert release];
- }
- [Database Close];
- }
- Update
- -(Ibaction) UPDATEBTN: (ID) Sender {
- fmdatabase* database=[fmdatabase databasewithpath:[self DatabasePath];
- if (![ Database Open]) {
- Return
- }
- BOOL update=[database executeupdate:@ "Update user set age = where name=?", [NSNumber numberwithint:20],@ "Chen"];
- if (update) {
- uialertview* Alert=[[uialertview alloc] initwithtitle:@ "Prompt" message:@ "update succeeded" Delegate:nil Cancelbuttontitle:nil otherbuttontitles:@ "OK", nil];
- [Alert show];
- [Alert release];
- }
- [Database Close];
- }
- -(nsstring*) DatabasePath
- {
- nsstring* path=[nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) objectAtIndex:0];
- nsstring* dbpath=[path stringbyappendingpathcomponent:@ "user.db"];
- return dbPath;
- }
- @end
iOS data storage mode Fmdb