Need to use the offline cache to store the data in the database, when there is no network load, and iOS is the Sqlite3 database, with the native SQL we can also achieve, but it is more difficult to write, especially in other languages to the programmer will feel the difficulty, we generally use Third party Fmdb
2 Fmdb of Https://github.com/ccgus/fmdb
3 after downloading the file in Fmdb, the project must import the following files and use the Libsqlite3.dylib dependency package
4 Fmdb Common class
Fmdatabase: A single SQLite database for executing SQL statements.
Fmresultset: Executes a query for a fmdatabase result set.
Fmdatabasequeue: This class is used when multiple threads are executing queries and updates
5 Operational databases
1 Creating and opening a database
1 Get database Objects NSString *path=nssearchpathfordirectoriesindomains (nsdocumentdirectory, Nsuserdomainmask, YES) [0]; Path=[path stringbyappendingpathcomponent:@ "Test.sqlite"]; Database=[fmdatabase Databasewithpath:path];2 Open the database, create and open the BOOL open=[database open] if it does not exist;if (open) {NSLog (@ "Database open successfully");}3 Creating a table NSString * create1=@ "CREATE table if not exists t_user (ID integer autoincrement primary key,name varchar)"; BOOL c1= [DataBase executeupdate:create1];if (C1) {NSLog (@ "CREATE table succeeded"); }4 Inserting Data NSString * insertsql=@ "INSERT into T_user (id,name) VALUES (?,?)";Insert statement 1BOOL Inflag1=[database executeupdate:insertsql,@ (2),@ "admin"];Insert Statement 2BOOL Inflag2=[database Executeupdate:insertsql withargumentsinarray:@[@ "Admin" @ (5)];Insert Statement 3BOOL Inflag3=[database Executeupdatewithformat:@ "INSERT into T_user (id,name) VALUES (%@,%d)",@ "Admin",6];Delete statement NSString * delete=@ "Delete from T_user"; BOOL dflag= [DataBase executeupdate:delete];if (dflag) {NSLog (@ "Delete succeeded");} //Modify statement nsstring *update=@ "Update t_user set name=?" "; BOOL flag= [dataBase executeupdate:update,@ "Zhangsan"]; if (flag) {NSLog (@ "modified successfully");} //5 query data Fmdb Fmresultset provides several methods to get different types of data NSString * Sql=@ "select * FROM t _user "; Fmresultset *result=[database Executequery:sql]; while (result.next) {int ids=[result intforcolumn: @ "id"]; NSString * Name=[result stringforcolumn:@ "name"]; int Ids=[result Intforcolumnindex:0]; NSString * Name=[result stringforcolumnindex:1]; NSLog (@ "%@,%d", Name,ids);}
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. Operation of databases in closures, not directly involved in Fmdatabase management
2 Multi-threaded operation NSString *path=nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) [0]; Path=[path stringbyappendingpathcomponent:@ "Test.sqlite"]; Fmdatabasequeue * Queue=[fmdatabasequeue Databasequeuewithpath:path]; [Queue indatabase:^ (Fmdatabase *db) {NSString * create=@ "CREATE table if not exists T_book (ID integer,name varchar)"; BOOL c1= [db executeupdate:create];if (C1) {NSLog (@ "Success");}}]; [Queue indatabase:^ (Fmdatabase *db) {NSString * insertsql=@ "INSERT into T_book (id,name) VALUES (?,?)"; //INSERT statement 1 bool inflag=[db executeupdate:insertsql,@ (2),@ "admin"]; if (inflag) {NSLog (@ "Insert succeeded");}]; [Queue indatabase:^ (Fmdatabase *db) {Fmresultset * data=[db executeQuery:@ "SELECT * from T_book"]; while (data.next) { int ids=[data intforcolumn:@ "id"]; NSString *name=[data stringforcolumn:@ "name"]; NSLog (@ "%@", name); NSLog (@ "%i", IDs);}];
Use of Fmdb in iOS