ios中原生的sqlite api在使用上相当不友好,在使用时,非常不便。于是,就出现了一系列将sqlite api进行封装的库,例如fmdb、plausibledatabase、sqlitepersistentobjects等,fmdb (https://github.com/ccgus/fmdb) 是一款简洁、易用的封装库,这一篇文章简单介绍下fmdb的使用。
在fmdb下载文件后,工程中必须导入如下文件,并使用 libsqlite3.dylib 依赖包。

fmdb同时兼容arc和非arc工程,会自动根据工程配置来调整相关的内存管理代码。
fmdb常用类:
- fmdatabase : 一个单一的sqlite数据库,用于执行sql语句。
- fmresultset :执行查询一个fmdatabase结果集,这个和android的cursor类似。
- fmdatabasequeue :在多个线程来执行查询和更新时会使用这个类。
创建数据库:
?| 1 |
db = [fmdatabase databasewithpath:database_path];
|
1、当数据库文件不存在时,fmdb会自己创建一个。
2、 如果你传入的参数是空串:@"" ,则fmdb会在临时文件目录下创建这个数据库,数据库断开连接时,数据库文件被删除。
3、如果你传入的参数是 null,则它会建立一个在内存中的数据库,数据库断开连接时,数据库文件被删除。
打开数据库:
?| 1 |
[db open]
|
返回bool型。
关闭数据库:
?| 1 |
[db close]
|
数据库增删改等操作:
除了查询操作,fmdb数据库操作都执行executeupdate方法,这个方法返回bool型。

看一下例子:
创建表:
?| 1 2 3 4 5 6 7 8 9 10 11 |
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];
}
|
添加数据:
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
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];
}
|
修改数据:
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 |
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];
}
|
删除数据:
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
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。
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 |
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的管理。
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
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);
}
}];
}
});
|
源码下载:demo
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://blog.csdn.net/xyz_lmn/article/details/9312837








发表评论
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。