(114) using Fmdb to manipulate SQLite database

Source: Internet
Author: User
Tags sqlite sqlite database

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

Related Article

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.