First introduce
Create a database
NSString *path = [Nssearchpathfordirectoriesindomains (nsdocumentdirectory, Nsuserdomainmask, YES) lastObject];
NSLog (@ "%@", Path);
NSString *databasepath = [path stringbyappendingstring:@ "/dsn.sqlite"];
/**
* Create a Fmdb database
**/
Fmdatabase *db = [[Fmdatabase Alloc]initwithpath:databasepath];
[DB Open];
// /**
* Create a table
// **/
NSString *createtable = @ "CREATE table Student (ID integer primary key autoincrement,name Text,password text)";
[DB executeupdate:createtable];
/**
* Insert Data
**/
NSString *insertsql = @ "INSERT into Student (Name,password) VALUES (' DSN ', ' 123456 ')";
BOOL B = [db executeupdate:insertsql];
if (b) {
NSLog (@ "add success");
}else{
NSLog (@ "Add failed");
}
/**
* parameter, return value, find
**/
Fmresultset *set = [db executequery:@ "select * from Student where name;?", @ "DSN"];
if (!set) {
NSLog (@ "Query failed");
}else{
while ([Set next]) {
int id = [set intforcolumnindex:0];
NSString *name = [set stringforcolumnindex:1];
NSString *password = [set stringforcolumnindex:2];
NSLog (@ "%d,%@,%@", Id,name,password);
}
}
// /**
* with parameters, add
// **/
//
for (int i = 0; i<5; i++) {
NSString *name = [NSString stringwithformat:@ "dsn%d", i+1];
NSString *password = [NSString stringwithformat:@ "%06d", Arc4random ()%100000];
//
[DB executeupdate:@ insert INTO Student (Name,password) VALUES (?,?) ", Name,password];
// }
//
/**
* Enquiry
// **/
//
NSString *selectsql = @ "SELECT * from Student";
Fmresultset *set = [db Executequery:selectsql];
NSLog (@ "%@", set);
while ([Set next]) {
int stuid = [set intforcolumnindex:0];
NSString *stuname = [set stringforcolumnindex:1];
NSString *stupass = [set stringforcolumnindex:2];
NSLog (@ "stuid =%d,name =%@,pass =%@", Stuid,stuname,stupass);
// }
IOS Learning Note 17-fmdb app