First download the source code of fmdb here, and then remove fmdb from the SRC folder in the unlocked file. M files are added to your iOS project, and libsqlite3.dylib is added to the project. What? Why don't I add fmdb. M? To put it simply, this file is the description of fmdb. The examples in are clear and simple. If you are interested, you can directly look at fmdb. M and probably use fmdb.
The following describes several commonly used commands to share with you:
-Open/close a database
The first thing to use a database is to create a database. Note that in iOS, only document directory can be read and written. The content under the Resource folder used during program writing is read-only. Therefore, the created database should be placed under the document folder. The method is as follows:
1 |
Nsarray * paths = nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, yes ); |
2 |
Nsstring * documentdirectory = [paths objectatindex: 0]; |
3 |
Nsstring * dbpath = [documentdirectory stringbyappendingpathcomponent: @ "mydatabase. DB"]; |
4 |
Fmdatabase * DB = [fmdatabase databasewithpath: dbpath]; |
6 |
Nslog (@ "cocould not open dB ."); |
Create a table
For a newly created database file, there is no table at the beginning. Creating a table is simple:
1 |
[DB executeupdate: @ "create table personlist (Name text, age integer, sex integer, phone text, address text, photo BLOB)"]; |
This is a common instruction in fmdb. [fmdatabase_object executeupdate:] is followed by the SQLite syntax inserted with nsstring. Because this article is mainly about fmdb, The SQLite syntax is not much said. The above Code creates a table named personlist, there are names, ages, gender, phone numbers, addresses, and photos. (Well .... An example table)
-Insert data
Just as before, use executeupdate followed by the syntax. The difference is that, because the inserted data is related to the objective-C variable, how to use it in string? Number to represent these variables.
1 |
[DB executeupdate: @ "insert into personlist (name, age, sex, phone, address, photo) values (?,?,?,?,?,?) ", |
3 |
@ "Jone", [nsnumber numberwithint: 20], [nsnumber numberwithint: 0], @ "091234567", @ "Taiwan, r.o.c", [nsdata datawithcontentsoffile: filepath]; |
In SQLite, text corresponds to nsstring, integer corresponds to nsnumber, and blob corresponds to nsdata. The fmdb should be fully converted. As long as you understand the SQLite syntax, there should be no problem.
-Update Materials
It's too simple. I don't want to talk about it. Please refer to the example:
1 |
[DB executeupdate: @ "Update personlist set age =? Where name =? ", [Nsnumber numberwithint: 30], @" John "]; |
-Obtain information
To obtain specific information, use the fmresultset object to receive the returned content:
01 |
Fmresultset * rs = [dB executequery: @ "Select name, age, from personlist"]; |
05 |
Nsstring * name = [RS stringforcolumn: @ "name"]; |
07 |
Int age = [RS intforcolumn: @ "Age"]; |
[RS next] can be used to poll the data returned from the query. Each next operation can obtain the corresponding value in a row and use [RS stringforcolumn:] or [RS intforcolumn:] to convert the value to the object-C type. After the data is used up, use [RS close] to close the result.
-Quick Data Acquisition
In some cases, only a specific value in a row is queried (for example, John's age). fmdb provides several simple methods. These methods are defined in fmdatabaseadditions. H. If you want to use them, remember to import them first.
View Source
Print
3 |
Nsstring * address = [dB stringforquery: @ "select address from personlist where name =? ", @" John "]; |
7 |
Int age = [dB intforquery: @ "select age from personlist where name =? ", @" John "]; |
Create, insert, update, and delete: Use the executeupdate method, while use the executequery Method for query.
1. instantiate fmdatabase
// Paths: Specifies the document path in IOS. The document is a folder that can be read and written in IOS.
Nsarray * paths = nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, yes );
Nsstring * documentdirectory = [paths objectatindex: 0];
// Dbpath: Specifies the database path in document.
Nsstring * dbpath = [documentdirectory stringbyappendingpathcomponent: @ "test. DB"];
// Create a database instance dB. If the "test. DB" file does not exist in the path, SQLite automatically creates "test. DB"
Fmdatabase * DB = [fmdatabase databasewithpath: dbpath];
If (! [DB open]) {
Nslog (@ "cocould not open dB .");
Return;
}
2. Create a table
// Create a table named user with two fields: String-type name and integer-type age.
[DB executeupdate: @ "create table user (Name text, age integer)"];
3. insert
// Insert data using the type text in OC corresponding to nsstring integer corresponding to nsnumber integer
[DB executeupdate: @ "insert into user (name, age) values (?,?) ", @" Zhang San ", [nsnumber numberwithint: 20];
4. Update
// Update the data and change "Zhang San" to "Li Si"
[DB executeupdate: @ "update user set name =? Where name =? ", @" Li Si ", @" Zhang San "];
5. Delete
// Delete data
[DB executeupdate: @ "delete from user where name =? ", @" James "];
6. Query
// Return the first matching result in the database
Nsstring * AA = [dB stringforquery: @ "Select name from user where age =? ", @" 20 "];
// Return all query results
Fmresultset * rs = [dB executequery: @ "select * from user"];
Rs = [dB executequery: @ "select * from user where age =? ", @" 20 "];
While ([RS next]) {
Nslog (@ "% @", [RS stringforcolumn: @ "name"], [RS stringforcolumn: @ "Age"]);
}
[RS close];