FMDB
时间:2021-07-01 10:21:17
帮助过:7人阅读
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, @"张三", @"13", @"济南"];
- BOOL res = [db executeUpdate:insertSql1];
- NSString *insertSql2 = [NSString stringWithFormat:
- @"INSERT INTO ‘%@‘ (‘%@‘, ‘%@‘, ‘%@‘) VALUES (‘%@‘, ‘%@‘, ‘%@‘)",
- TABLENAME, NAME, AGE, ADDRESS, @"李四", @"12", @"济南"];
- BOOL res2 = [db executeUpdate:insertSql2];
-
- if (!res) {
- NSLog(@"error when insert db table");
- } else {
- NSLog(@"success to insert db table");
- }
- [db close];
-
- }
修改数据:
[cpp] view plaincopyprint?
- 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, @"张三"];
- BOOL res = [db executeUpdate:deleteSql];
-
- if (!res) {
- NSLog(@"error when delete db table");
- } else {
- NSLog(@"success to delete db table");
- }
- [db close];
-
- }
数据库查询操作:
查询操作使用了executeQuery,并涉及到FMResultSet。
- 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];
- }
FMDB的FMResultSet提供了多个方法来获取不同类型的数据:
数据库多线程操作:
如果应用中使用了多线程操作数据库,那么就需要使用FMDatabaseQueue来保证线程安全了。 应用中不可在多个线程中共同使用一个FMDatabase对象操作数据库,这样会引起数据库数据混乱。 为了多线程操作数据库安全,FMDB使用了FMDatabaseQueue,使用FMDatabaseQueue很简单,首先用一个数据库文件地址来初使化FMDatabaseQueue,然后就可以将一个闭包(block)传入inDatabase方法中。 在闭包中操作数据库,而不直接参与FMDatabase的管理。
[cpp] view plaincopyprint?
- FMDatabaseQueue * queue = [FMDatabaseQueue databaseQueueWithPath:database_path];
- dispatch_queue_t q1 = dispatch_queue_create("queue1", NULL);
- dispatch_queue_t q2 = dispatch_queue_create("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,@"济南"];
- 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,@"北京"];
- if (!res) {
- NSLog(@"error to inster data: %@", name);
- } else {
- NSLog(@"succ to inster data: %@", name);
- }
- }];
- }
- });
FMDB
标签: