IOS Study Notes (16)-Database Operations (using FMDB)

Source: Internet
Author: User

FMDB (https://github.com/ccgus/fmdb

FMResultSet: Execute to query an FMDatabase result set, which is similar to the android Cursor.
FMDatabaseQueue: this class is used when multiple threads are used to execute queries and updates.

 

db = [FMDatabase databaseWithPath:database_path];


 

.

 

[db open]


Return BOOL type.

 

 

[db close]



 

 

 

if ([db open]) {        NSString *sqlCreateTable =  [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS '%@' ('%@' INTEGER PRIMARY KEY AUTOINCREMENT, '%@' TEXT, '%@' INTEGER, '%@' TEXT)",TABLENAME,ID,NAME,AGE,ADDRESS];        BOOL res = [db executeUpdate:sqlCreateTable];        if (!res) {            NSLog(@"error when creating db table");        } else {            NSLog(@"success to creating db table");        }        [db close];    }



 

 

If ([db open]) {NSString * insertSql1 = [NSString stringWithFormat: @ "insert into '% @' ('% @', '% @', '% @') VALUES ('% @', '% @', '% @') ", TABLENAME, NAME, AGE, ADDRESS, @" Zhang San ", @" 13 ", @ "Jinan"]; BOOL res = [db executeUpdate: insertSql1]; NSString * insertSql2 = [NSString stringWithFormat: @ "insert into '% @' ('% @', '% @', '% @') VALUES ('% @', '% @', '% @') ", TABLENAME, NAME, AGE, ADDRESS, @ "Li Si", @ "12", @ "Jinan"]; BOO L res2 = [db executeUpdate: insertSql2]; if (! Res) {NSLog (@ "error when insert db table");} else {NSLog (@ "success to insert db table");} [db close];}



 

 

if ([db open]) {        NSString *updateSql = [NSString stringWithFormat:                               @"UPDATE '%@' SET '%@' = '%@' WHERE '%@' = '%@'",                               TABLENAME,   AGE,  @"15" ,AGE,  @"13"];        BOOL res = [db executeUpdate:updateSql];        if (!res) {            NSLog(@"error when update db table");        } else {            NSLog(@"success to update db table");        }        [db close];    }



 

 

If ([db open]) {NSString * deleteSql = [NSString stringWithFormat: @ "delete from % @ where % @ = '% @'", TABLENAME, NAME, @ "James"]; BOOL res = [db executeUpdate: deleteSql]; if (! Res) {NSLog (@ "error when delete db table");} else {NSLog (@ "success to delete db table");} [db close];}



 

 

if ([db open]) {        NSString * sql = [NSString stringWithFormat:                          @"SELECT * FROM %@",TABLENAME];        FMResultSet * rs = [db executeQuery:sql];        while ([rs next]) {            int Id = [rs intForColumn:ID];            NSString * name = [rs stringForColumn:NAME];            NSString * age = [rs stringForColumn:AGE];            NSString * address = [rs stringForColumn:ADDRESS];            NSLog(@"id = %d, name = %@, age = %@  address = %@", Id, name, age, address);        }        [db close];    }

 



 

FMDatabaseQueue * queue = [FMDatabaseQueue queue: database_path]; q1 = dispatch_queue_create ("queue1", NULL); q2 = partition ("queue2", NULL); dispatch_async (q1, ^ {for (int I = 0; I <50; ++ I) {[queue inDatabase: ^ (FMDatabase * db2) {NSString * insertSql1 = [NSString stringWithFormat: @ "insert into '% @' ('% @', '% @', '% @') VALUES (?, ?, ?) ", TABLENAME, NAME, AGE, ADDRESS]; NSString * name = [NSString stringWithFormat: @" jack % d ", I]; NSString * age = [NSString stringWithFormat: @ "% d", 10 + I]; BOOL res = [db2 executeUpdate: insertSql1, name, age, @ "Jinan"]; if (! Res) {NSLog (@ "error to inster data: % @", name);} else {NSLog (@ "succ to inster data: % @", name) ;}}] ;}}); dispatch_async (q2, ^ {for (int I = 0; I <50; ++ I) {[queue inDatabase: ^ (FMDatabase * db2) {NSString * insertSql2 = [NSString stringWithFormat: @ "insert into '% @' ('% @', '% @', '% @') VALUES (?, ?, ?) ", TABLENAME, NAME, AGE, ADDRESS]; NSString * name = [NSString stringWithFormat: @" lilei % d ", I]; NSString * age = [NSString stringWithFormat: @ "% d", 10 + I]; BOOL res = [db2 executeUpdate: insertSql2, name, age, @ "Beijing"]; if (! Res) {NSLog (@ "error to inster data: % @", name);} else {NSLog (@ "succ to inster data: % @", name) ;}}] ;}});



 


@ Zhang xingye TBOW(#-> @)

 

 

 

 

Https://github.com/ccgus/fmdb

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.