Use learning notes for iOS Fmdb class libraries

Source: Internet
Author: User

Article excerpt from: Text Top-top iOS Development database Chapter-fmdb Simple Introduction

What is Fmdb

Fmdb is the SQLite database framework for the iOS platform

FMBD encapsulates the C language API of SQLite in OC mode

Advantages of Fmdb

use more object-oriented, save a lot of cumbersome, redundant C language code

More lightweight and flexible than Apple's core data framework

Provides a multi-threaded secure database operation method to effectively prevent data confusion

Fmdb's GitHub Address

Https://github.com/ccgus/fmdb

Core class

Fmdb has three main classes

1, Fmdatabase

a Fmdatabase object represents a separate SQLite database to execute SQL statements

2, Fmresultset

result set after query execution with Fmdatabase

3, Fmdatabasequeue

used to execute multiple queries or updates in multiple threads, it is thread-safe.

Open Database

Create a Fmdatabase object by developing a SQLite database file path

Fmdatabase *db = [Fmdatabase databasewithpath:path];if (![ DB Open] {    NSLog (@ "Database open failed!) ");}

There are three types of file paths

1, the specific file path (file does not exist automatically created)

2, empty string @ ""(an empty database is created in the temp directory, and the database file is deleted when the Fmdatabase connection is closed)

3, Nil(Create an in-memory staging database that will be destroyed when the Fmdatabase connection is closed)

Perform the update

in Fmdb, in addition to querying for all unexpected operations, it is called an update

Create, DROP, insert, UPDATE, delete, and more

To perform an update using the Executeupdate method

-(BOOL) Executeupdate: (nsstring*) SQL, ...-(BOOL) Executeupdatewithformat: (nsstring*) format, ...-(BOOL) Executeupdate: (nsstring*) SQL Withargumentsinarray: (Nsarray *) arguments

Example

[DB executeupdate:@ "UPDATE t_student SET age =? WHERE name =?; ", @20, @" Jack "]

Execute Query Method

-(Fmresultset *) ExecuteQuery: (nsstring*) SQL, ...-(Fmresultset *) Executequerywithformat: (nsstring*) format, ...-( Fmresultset *) ExecuteQuery: (NSString *) SQL Withargumentsinarray: (Nsarray *) arguments

Example

Query data Fmresultset *rs = [db executequery:@ "select * from T_student"];

while ([Rs next]) {    NSString *name = [rs stringforcolumn:@ "name"];    int age = [rs intforcolumn:@ ' age '];    Double score = [Rs doubleforcolumn:@ "score"];}

use of class libraries:

Open Database + (Fmdatabase *) opendb{nsstring *filepath = [Nssearchpathfordirectoriesindomains (Nsdocumentationdirectory,    Nsuserdomainmask, YES) lastobject];        NSString *filename = [FilePath stringbyappendingstring:@ "Baidumap.sqlite"];        Fmdatabase *db = [Fmdatabase databasewithpath:filename];    if ([db Open]) {return db; } return nil;}    #pragma mark-Database update//Other operations except queries are update + (BOOL) Update: (NSString *) sql{fmdatabase *db = [self opendb];    if (db! = nil) {return [db executeupdate:sql]; } return NO;    #pragma mark-Database creation table + (BOOL) createtable{fmdatabase *db = [self opendb];  if (db! = nil) {NSString *sql = @ "CREATE TABLE location_record (ID INTEGER PRIMARY KEY autoincrement, Latitude REAL        Not null,longitude REAL isn't null,time TEXT not NULL ";    return [DB Executeupdate:sql]; } return NO; #pragma mark-Insert Data + (BOOL) Insert: (float) latitude:(float) Longitude:(NSString *) time{nsstring *sql = [NSString Strin Gwithformat: @ "InserT into Location_record (latitude,longitude,time) VALUES (%f,%f,%@) ", Latitude,longitude,time]; return [self update:sql];} #pragma mark-query + (Nsarray *) Query:(NSString *) Startime:(NSString *) endTime:(int) startnumber{fmdatabase *db = [SE    LF OPENDB]; if (db! = nil) {NSString * sql = [NSString stringwithformat:@ "SELECT (latitude,longitude) from Location_record WHE        RE time>%@ and time <%@ limit%d,60 ", startime,endtime,startnumber];        Fmresultset *resultset = [db Executequery:sql];        Nsmutablearray *array = [Nsmutablearray array];            while ([ResultSet next]) {double latitude = [ResultSet doubleforcolumnindex:1];            Double longitude= [ResultSet doubleforcolumnindex:2];            [Array Addobject:[nsnumber numberwithdouble:latitude];        [Array Addobject:[nsnumber numberwithdouble:longitude];    } return array; } return nil;}

Walnuts posterity--the draft does not forget to dig the well Man

·

Use learning notes for iOS Fmdb class libraries

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.