// 导入FMDB头文件
import “FMDB.h”
// 创建属性数据库对象队列属性
@property (nonatomic,strong) FMDatabaseQueue *queue;
+ (void)initialie {
// 1.获取沙盒中数据库的文件名
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES)lastObject] stringByAppendingPathComponent:@”car.sqlite”];
// 创建数据库对象队列
_queue = [FMDatabaseQueuedatabaseQueueWithPath:path];
// 创建表
[_queueinDatabase:^(FMDatabase *db) {
BOOL result = [dbexecuteUpdate:@"create table if not exists t_car (id primary key autoincerment not null, name text not null)"];
if (result) {
NSLog(@"创建表成功!");
}
else
{
NSLog(@"创建表失败!");
}
}];
}
// 增加数据
- (void)add {
[_queueinDatabase:^(FMDatabase *db) {
[db executeUpdate:@”insert into t_car (name) values (?)”,@”宝马”];
}];
}
// 删除数据
- (void)del {
[_queueinDatabase:^(FMDatabase *db) {
[db executeUpdate:@”delete table t_car where name = ?”,@”宝马”];
}];
}
// 修改数据
- (void)update {
[_queueinDatabase:^(FMDatabase *db) {
[db executeUpdate:@”update t_car set name = ? where name = ?”,@”奔驰”,@”宝马”];
}];
}
// 查询数据
- (void)query {
[_queueinDatabase:^(FMDatabase *db) {
// 获得结果集
FMResultSet rs = [dbexecuteQuery:@”select from t_car where name = ?”,@”宝马”];
// 遍历结果集
while (rs.next) {
NSString *carName = [rsstringForColumn:@"name"];
int cid = [rsintForColumn:@"id"];
NSLog(@"id:%d,name:%@", cid, carName);
}
}];
}
ios sqlite框架FMDB 的简单应用
最新推荐文章于 2020-06-14 23:04:02 发布