Fmdb is a third-party database management framework that parses SQLite under the iOS platform, is easy to use, and provides multi-threaded secure database operations that are more flexible and lightweight than coredata.
There are three main categories of Fmdb:
Fmdatabase: Used to execute SQL statements;
Fmresultset: The result collection used to execute the query with Fmdatabase
Fmdatabasequeue: Used to query and update data in multiple threads, it is thread-safe.
Use of Fmdb
1. In the newly created project, import the Libsqlite3 dynamic database:
Note: (added in link Binary withlibraries in building phases)
2. Join these main documents in the third-party framework Fmdb:
3. Create a database in your code and specify the file path:
1 //Specify the sandbox path2NSString *path =[Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) lastobject];3 //add sqlite files under the specified sandbox path4NSString *filepath = [path stringbyappendingpathcomponent:@"Moxue.sqlite"];5 //to create a database under the specified path path6Fmdatabase *db =[Fmdatabase Databasewithpath:filepath];7 //See if success is turned on8 if(![DB Open]) {9 return;Ten}Else{ OneNSLog (@"Open Success"); A}
4. Add list and List properties to your code:
1 //Create a list in the database and add list properties2BOOL result = [db executeupdate:@"CREATE TABLE IF not EXISTS data (ID INTEGER PRIMARY KEY autoincrement,title text,date text,context text,url TEXT)"];3 if(Result) {4NSLog (@"Create data table successfully");5}Else{6NSLog (@"failed to create data table");7}
5. Insert the data in the list:
1 //Inserting Data2BOOL res = [db executeupdate:@"INSERT into Data (Title,date,context,url) VALUES (?,?,?,?)",@"mo Xue",@"2015-10-11",@"my form",@"my URL"];3BOOL res1 = [db executeupdate:@"INSERT into Data (Title,date,context,url) VALUES (?,?,?,?)",@"Liu Qinghe",@"2015-10-10",@"Liu Qinghe's form",@"URL of Liu Qinghe"];4 if(res1) {5NSLog (@"Insert Data successfully");6}Else{7NSLog (@"failed to insert data table");8}
6. Delete the data or list in the class table:
1BOOL dele = [db executeupdate:@"DROP TABLE IF EXISTS data"];2BOOL dele1 = [db executeupdate:@"Delete from data where Title = ' ink Snow '"];3 if(dele) {4NSLog (@"Delete Data successfully");5}Else{6NSLog (@"failed to delete data");7}
7. Modify the data in the list:
1 //Modifying Data2BOOL UPDATE = [db executeupdate:@"Update data SET Title = ' haha ' WHERE id = 1" ];3 if(UPDATE) {4NSLog (@"Modification succeeded");5}Else{6NSLog (@"Modification Failed");7}
8. Query the data in the list:
1 //Querying Data2Fmresultset *rs = [db executeQuery:@"SELECT Title from Data"];3 while([RS next]) {4NSString *name = [RS stringforcolumn:@"Title"];5NSLog (@"%@", name);6}
9. If you use a multi-threaded database in your application, you need to use Fmdatabasequeue to ensure thread safety. It is not common in the application to use a Fmdatabase object in multiple threads to manipulate the database, which can cause confusion in database data. For multi-threaded operation of database security, Fmdb uses Fmdatabasequeue, using Fmdatabasequeue is very simple, first with a database file address to the initial fmdatabasequeue, and then you can be a closure (block) Passed into the Indatabase method. Operate the database in a closure without directly participating in the management of the fmdatabase.
1Fmdatabasequeue *queue =[Fmdatabasequeue Databasequeuewithpath:filepath];2dispatch_queue_t myq1 = Dispatch_queue_create ("myqueue1", nil);3Dispatch_async (myq1, ^{4[Queue indatabase:^ (Fmdatabase *db) {5Fmresultset *rs = [db executeQuery:@"SELECT Title from Data"];6 while([RS next]) {7NSString *name = [RS stringforcolumn:@"Title"];8NSLog (@"%@", name);9 }Ten One }]; A});
10. Close the database
1 [db close];
11. Fmresultset also provides a number of ways to obtain the desired format values:
IntForColumn:longForColumn:longLongIntForColumn:boolForColumn:doubleForColumn:stringForColumn:data ForColumn:dataNoCopyForColumn:UTF8StringForColumnIndex:objectForColumn:
Use of SQLite third-party class library Fmdb