FMDB是个第三方库。用起来很方便。简单的介绍一些用法:
首先下载 FMDB 第三方库。下载地址从网上搜吧,很多。
然后就是要添加 libsqlite3.0.dylib 库。
再引入 import "FMDatabase.h" FMDatabase *db;
准备工作做完了。接下来就是创建库,创建表,插入数据,删除数据等对数据库的操作了。
首先创建库跟sqlite一样。
NSArray *documentPath =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *path = [documentPathobjectAtIndex:0];
NSString *myPath = [pathstringByAppendingPathComponent:@"myTest"];
打开数据库
db = [FMDatabasedatabaseWithPath:myPath];
if (![dbopen]) {
NSLog(@"open cancel");
return;
}else{
NSLog(@"open OK");
}
创建表
[dbexecuteUpdate:@"create table User (id int, name text,age imteger)"];
删除表
[dbexecuteUpdate:@"drop table User"];
插入数据
[dbexecuteUpdate:@"insert into User(id,name,age) values(?,?,?)",[NSNumbernumberWithInt:1],@"张三",[NSNumbernumberWithInt:20]];
更新数据
[dbexecuteUpdate:@"update User set name = ? where name = ?",@"王五",@"张三"];
查询数据
FMResultSet *rt = [dbexecuteQuery:@"select id, name,age from User"];
NSLog(@"%@",rt);
while ([rtnext]) {
int _id = [rtintForColumn:@"id"];
NSString *name = [rtstringForColumn:@"name"];
int age = [rtintForColumn:@"age"];
NSLog(@"%d,%@,%d",_id,name,age);
}
关闭数据库[dbclose];
常用sql语句: