Use of fmdb

Source: Internet
Author: User

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];

5

If(! [DB open]) {

6

Nslog (@ "cocould not open dB .");

7

Return;

8

}

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 (?,?,?,?,?,?) ",

2

 

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"];

02

 

03

While([RS next]) {

04

 

05

Nsstring * name = [RS stringforcolumn: @ "name"];

06

 

07

Int age = [RS intforcolumn: @ "Age"];

08

 

09

}

10

 

11

[RS close];

[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

1

// Find the address

2

 

3

Nsstring * address = [dB stringforquery: @ "select address from personlist where name =? ", @" John "];

4

 

5

// Year round

6

 

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];

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.