FMDB的github地址
https://github.com/ccgus/fmdb
FMDB有三個主要的類
FMDatabase
一個FMDatabase對象就代表一個單獨的SQLite數據庫
用來執(zhí)行SQL語句
FMResultSet
使用FMDatabase執(zhí)行查詢后的結果集
FMDatabaseQueue
用于在多線程中執(zhí)行多個查詢或更新奄毡,它是線程安全的
1.打開創(chuàng)建數據庫
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
// 拼接文件名
NSString *filePath = [cachePath stringByAppendingPathComponent:@"contact.sqlite"];
// 創(chuàng)建一個數據庫的實例,僅僅在創(chuàng)建一個實例,并會打開數據庫
FMDatabase *db = [FMDatabase databaseWithPath:filePath];
_db = db;
// 打開數據庫
BOOL flag = [db open];
if (flag) {
NSLog(@"打開成功");
}else{
NSLog(@"打開失敗");
}
// 創(chuàng)建數據庫表
// 數據庫操作:插入,更新,刪除都屬于update
// 參數:sqlite語句
BOOL flag1 = [db executeUpdate:@"create table if not exists t_contact (id integer primary key autoincrement,name text,phone text);"];
if (flag1) {
NSLog(@"創(chuàng)建成功");
}else{
NSLog(@"創(chuàng)建失敗");
}
}
2.增
- (IBAction)insert:(id)sender {
// ?:表示數據庫里面的占位符
BOOL flag = [_db executeUpdate:@"insert into t_contact (name,phone) values (?,?)",@"oooo",@"21321321"];
if (flag) {
NSLog(@"success");
}else{
NSLog(@"failure");
}
}
3.刪
- (IBAction)delete:(id)sender {
BOOL flag = [_db executeUpdate:@"delete from t_contact;"];
if (flag) {
NSLog(@"success");
}else{
NSLog(@"failure");
}
}
4.改
- (IBAction)update:(id)sender {
// FMDB诗芜?重贺,只能是對象,不能是基本數據類型涣澡,如果是int類型跛锌,就包裝成NSNumber 簡單包裝@12
BOOL flag = [_db executeUpdate:@"update t_contact set name = ?",@"abc"];
if (flag) {
NSLog(@"success");
}else{
NSLog(@"failure");
}
}
5.查
- (IBAction)select:(id)sender {
FMResultSet *result = [_db executeQuery:@"select * from t_contact"];
// 從結果集里面往下找
while ([result next]) {
NSString *name = [result stringForColumn:@"name"];
NSString *phone = [result stringForColumn:@"phone"];
NSLog(@"%@--%@",name,phone);
}
}
6.線程安全
要一起操作成功的這種的一般要用事務
FMDatabase這個類是線程不安全的弃秆,如果在多個線程中同時使用一個FMDatabase實例,會造成數據混亂等問題
為了保證線程安全,FMDB提供方便快捷的FMDatabaseQueue類
FMDatabaseQueue的創(chuàng)建 不需要手動打開了
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
// 拼接文件名
NSString *filePath = [cachePath stringByAppendingPathComponent:@"user.sqlite"];
// 創(chuàng)建數據庫實例
FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath:filePath];
_queue = queue;
// 創(chuàng)建數據庫表
// 提供了一個多線程安全的數據庫實例
[queue inDatabase:^(FMDatabase *db) {
BOOL flag = [db executeUpdate:@"create table if not exists t_user (id integer primary key autoincrement,name text,money integer)"];
if (flag) {
NSLog(@"success");
}else{
NSLog(@"failure");
}
}];
}
增刪查都差不多 改的時候使用事務
- (IBAction)update:(id)sender {
// update t_user set money = 500 where name = 'a';
// update t_user set money = 1000 where name = 'b';
// a -> b 500 a 500
// b + 500 = b 1000
[_queue inDatabase:^(FMDatabase *db) {
// 開啟事務
[db beginTransaction];
BOOL flag = [db executeUpdate:@"update t_user set money = ? where name = ?;",@500,@"a"];
if (flag) {
NSLog(@"success");
}else{
NSLog(@"failure");
// 回滾
[db rollback];
}
BOOL flag1 = [db executeUpdate:@"updat t_user set money = ? where name = ?;",@1000,@"b"];
if (flag1) {
NSLog(@"success");
}else{
NSLog(@"failure");
[db rollback];
}
// 全部操作完成時候再去
[db commit];
}];
}