The previous section introduced the system to use the C language Library operation SQLite method, more cumbersome, the use of Fmdb will be greatly simplified, and is object-oriented, the use is very convenient.
Use the following steps:
Download the Fmdb framework from GitHub and import it into the project.
① Import the Libsqlite3.0.dylib library.
② Import the primary header file FMDatabase.h.
③ creates a database object, passes in the path, opens the database, and is created if the database does not exist.
NSString *sqlitepath = [[Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) LastObject] stringbyappendingpathcomponent:@ "Test1.sqlite"]; Fmdatabase *db = [Fmdatabase Databasewithpath:sqlitepath]; [DB Open];
④ query operation usingExecuteQuery: Methods, other operations usingexecuteupdate: Method.
For example, creating a table and inserting data are all using update.
It is important to note that the OC string appears in the SQL statement without the single quotation marks included in the%@, otherwise it will make the data being stored.
Build table [DB executeupdate:@ "CREATE table IF not EXISTS t_product (ID integer PRIMARY KEY autoincrement, Name text not NULL, p Rice real); "];/ /Insert Data//Note If you use OC string%@ without single quotation marks [db executeupdatewithformat:@ "INSERT into T_product (name,price) VALUES (%@,%d)", [NSString stringwithformat:@ "Drink%d", Arc4random_uniform (10000)],arc4random_uniform (100)];
⑤ query operation through query, get the result set, the result set can be regarded as an iterator, call next property to reach the next element, the current position can take out elements, as follows:
Query data Fmresultset *set = [db executequery:@ "select * from T_product"];while (set.next) { NSString *name = [Set string forcolumn:@ "name"]; Double Price = [set doubleforcolumn:@ ' price ']; NSLog (@ "%@%f", Name,price);}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
(114) using Fmdb to manipulate SQLite database