您的位置:首页 > 数据库

ios sqlite框架FMDB 的简单应用

2016-01-05 18:10 239 查看
// 导入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);
}
}];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: