// ViewController.m
// 01-FMDB的简单使用
#import "ViewController.h"
#import "FMDB.h"
@interface ViewController ()
@property (nonatomic, strong) FMDatabase *db;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 打印沙盒路径
// NSLog(@"沙盒路径 = %@",NSHomeDirectory());
// 之前是都是保存到docment,最近保存到docment,苹果不允许上传。
// 游戏一般都是document
// 获取cache文件夹路径
NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
// 拼接文件名
NSString *filePath = [cachePath stringByAppendingPathComponent:@"contact.sqlite"];
// 创建一个数据库的实例,仅仅在创建一个实例,并不会打开数据库
FMDatabase *db = [FMDatabase databaseWithPath:filePath];
_db = db;
// 打开数据库
BOOL flag = [db open];
if (flag) {
NSLog(@"打开数据库成功");
}else{
NSLog(@"打开数据库失败");
}
// 创建数据库表
// 数据库操作:插入,更新,删除都属于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(@"创建表成功");
}else{
NSLog(@"创建表失败");
}
}
// 增
- (IBAction)insert:(id)sender {
// ?:表示数据库里面的占位符
BOOL flag = [_db executeUpdate:@"insert into t_contact (name,phone) values (?,?)",@"Jack",@"120"];
if (flag) {
NSLog(@"添加成功");
}else{
NSLog(@"添加失败");
}
}
// 删
- (IBAction)delete:(id)sender {
BOOL flag = [_db executeUpdate:@"delete from t_contact;"];
if (flag) {
NSLog(@"删除成功");
}else{
NSLog(@"删除失败");
}
}
// 改
- (IBAction)update:(id)sender {
// FMDB?,只能是对象,不能是基本数据类型,如果是int类型,就包装成NSNumber
BOOL flag = [_db executeUpdate:@"update t_contact set name = ?",@"abc"];
if (flag) {
NSLog(@"修改成功");
}else{
NSLog(@"修改失败");
}
}
// 查
- (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 = %@",name,phone);
}
}
@end
// ViewController.m
// 02-FMDB多线程安全和事务
#import "ViewController.h"
#import "FMDB.h"
@interface ViewController ()
@property (nonatomic, strong) FMDatabaseQueue *queue;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 打印沙盒路径
// NSLog(@"沙盒路径 = %@",NSHomeDirectory());
// 之前是都是保存到docment,最近保存到docment,苹果不允许上传。
// 游戏一般都是document
// 获取cache文件夹路径
NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
// 拼接文件名
NSString *filePath = [cachePath stringByAppendingPathComponent:@"user.sqlite"];
// 创建数据库实例,并且打开数据库
FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath:filePath];
_queue = queue;
// 创建数据库表
// 提供了一个多线程安全的数据库实例
[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(@"创建表成功");
}else{
NSLog(@"创建表失败");
}
}];
}
// 增
- (IBAction)add:(id)sender {
[_queue inDatabase:^(FMDatabase *db) {
BOOL flag = [db executeUpdate:@"insert into t_user (name,money) values (?,?)",@"a",@1000];
if (flag) {
NSLog(@"a添加成功");
}else{
NSLog(@"a添加失败");
}
BOOL flag2 = [db executeUpdate:@"insert into t_user (name,money) values (?,?)",@"b",@500];
if (flag2) {
NSLog(@"b添加成功");
}else{
NSLog(@"b添加失败");
}
}];
}
// 删
- (IBAction)delete:(id)sender {
[_queue inDatabase:^(FMDatabase *db) {
BOOL flag = [db executeUpdate:@"delete from t_user;"];
if (flag) {
NSLog(@"删除成功");
}else{
NSLog(@"删除失败");
}
}];
}
// 改
- (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(@"a修改成功");
}else{
NSLog(@"a修改失败");
// 回滚
// [db rollback];
}
BOOL flag2 = [db executeUpdate:@"update t_user set money = ? where name = ?;",@1000,@"b"];
if (flag2) {
NSLog(@"b修改成功");
}else{
NSLog(@"b修改失败");
// [db rollback];
}
// 全部操作完成时候再去提交
// [db commit];
}];
}
// 查
- (IBAction)select:(id)sender {
[_queue inDatabase:^(FMDatabase *db) {
FMResultSet *result = [db executeQuery:@"select * from t_user"];
while ([result next]) {
NSString *name = [result stringForColumn:@"name"];
int money = [result intForColumn:@"money"];
NSLog(@"%@--%d",name,money);
}
}];
}
@end