The following is no nonsense directly on the code??
/* First drag the Fmdb into the project, we need to sqlite3 this library into the project, (if the project is non-arc, we can directly import the good) database--"operation of a large number of regular data fmdb is someone else in the system database of the cumbersome basis to give Encapsulated a bit, for us to use,--"compared to the system database is more simple, good fmdatabase is mainly to provide the operation of the SQL statement FMDatabaseAdditions.h as long as it is responsible for multi-threaded or query update when the operation F Mdbresultset result set, operation on database, return result *///Create an object of Fmdatabase class, this object is used to manipulate database db = [[Fmdatabase alloc] Initwithpath:[nshom EDirectory () stringbyappendingpathcomponent:@ "Documents/pop.sqlite"]; Open the database--"refers to the open when there may be an exception, if an exception is turned off, if there is no exception, execute the following code directly if (![ DB Open]) {//close database [db close]; Return }//To the operation of the table in the database, add, delete, modify//See if the table is not in the database---"Perform the table operation, import FMDatabaseAdditions.h if (![ DB tableexists:@ "People"]) {[DB executeupdate:@ "CREATE TABLE people (Peopleid INTEGER PRIMARY key,peoplename TE Xt,peopleage INTEGER) "]; }//Close database [db close]; Insert//Open database if (![ DB Open]) {[db close]; Return }//Set cache---to increase efficiency [db setshouldcachestatements:YES]; Perform insert operation [NSNumber numberwithint:22]; convert integer type to NSNumber object type [db executeupdate:@ "insert into people (Peoplename, Peopleage) VALUES (?,?) ", @" Bai Tongdong ", [NSNumber numberwithint:22]]; Close the database [db close]; Delete which line you want to know,--> go if (![ DB Open]) {[db close]; Return } BOOL issuccess = [db executeupdate:@ "DELETE from people WHERE Peopleid =?", [NSNumber Numberwithint:_peopleid]]; Detects if the SQL statement is written correctly-1 o'clock correct 0 opposite NSLog (@ "9------%d", issuccess); [DB close]; Update//Open database if (![ DB Open]) {[db close]; Return }//Set cache [db Setshouldcachestatements:yes]; Perform update [DB executeupdate:@] Update people SET Peoplename =? WHERE peopleid=? ", @" Stone ", [NSNumber Numberwithint:_peopleid]]; [DB close]; Update ui//Open Database if (![ DB Open]) {[db close]; Return }//Set cache [db Setshouldcachestatements:yes]; Execute the query//Add, delete, modify, all is the database in the operation of the table, the query is to read the data in the database (result set) Fmresultset *rs = [Db executequery:@ "Select *from people"]; To create an array to hold the data that has been added to the read//Shift group, and to appear once [Peoplearray removeallobjects]; Iterate through the result set, reading only one data at a time while ([Rs next]) {//From the result set to fetch the data just read,--"to encapsulate a people class--" To create a people class Zypeople *p = [[Zypeople alloc] init]; According to the type of data in the Database (field-field type)--"to give people description of several corresponding properties P.pid = [rs intforcolumn:@" Peopleid "]; P.pname = [rs stringforcolumn:@ "Peoplename"]; P.page = [rs intforcolumn:@ "peopleage"]; [Peoplearray Addobject:p]; }//Close result set [Rs close]; Close the database [db close]; [。。。 Reloaddata];
Use of the Ios-fmdb database