#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
// 数据库管理器
#define CHDbMgr [CHFmdb shareInstance].fmdb
// 数据库管理队列,该对象支持多线程操作
#define CHDbQueue [CHFmdb shareInstance].queue
@interface CHFmdb : NSObject
/** 数据库管理器 */
@property(nonatomic, strong) FMDatabase *fmdb;
/** 数据库管理队列,该对象支持多线程操作 */
@property(nonatomic, strong) FMDatabaseQueue *queue;
+ (instancetype)shareInstance;
@end
NS_ASSUME_NONNULL_END
#import "CHFmdb.h"
@interface CHFmdb ()
@end
#define DataBaseName @"OutLibrary"
@implementation CHFmdb
+ (instancetype)shareInstance {
static CHFmdb *fmdbMgr = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
fmdbMgr = [[self alloc] init];
// 判断是否存在数据库,如果有,就继续,如果没有就将工程里面的复制到Document
// 获取所有文档路径
NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// 获取第一个路径
NSString *docPath = [searchPaths objectAtIndex:0];
// 拼接完整路径
NSString *dbPath = [docPath stringByAppendingPathComponent:@"BookLibrary.db"];
// 创建文件管理器
NSFileManager *fileManager = [[NSFileManager alloc] init];
// 判断该路径上是否存在数据库
BOOL isExit = [fileManager fileExistsAtPath:dbPath];
// 如果不存在,讲工程里面数据库的复制到Document里面
if (!isExit) {
// 获取工程里面的数据库路径
NSString *bundleDBPath = [[NSBundle mainBundle] pathForResource:DataBaseName ofType:@"db"];
// 拷贝工程里面的数据库到沙盒
BOOL success = [fileManager copyItemAtPath:bundleDBPath toPath:dbPath error:nil];
if (success) {
//CHLog(@"数据库复制成功");
}
}
// 根据数据库路径创建数据库管理器
fmdbMgr.fmdb = [[FMDatabase alloc] initWithPath:dbPath];
// 为数据库设置缓存,提高查询效率
[fmdbMgr.fmdb setShouldCacheStatements:YES];
// 根据数据库路径创建数据库管理队列,该对象支持多线程操作
fmdbMgr.queue = [FMDatabaseQueue databaseQueueWithPath:dbPath];
});
return fmdbMgr;
}
@end
#import "CHFmdb.h"
@interface CHFmdb ()
@end
@implementation CHFmdb
+ (instancetype)shareInstance {
static CHFmdb *fmdbMgr = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
fmdbMgr = [[self alloc] init];
// 判断是否存在数据库,如果有,就继续,如果没有就将工程里面的复制到Document
// 获取所有文档路径
NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// 获取第一个路径
NSString *docPath = [searchPaths objectAtIndex:0];
// 拼接完整路径
NSString *dbPath = [docPath stringByAppendingPathComponent:@"BookLibrary.db"];
// 根据数据库路径创建数据库管理器
fmdbMgr.fmdb = [[FMDatabase alloc] initWithPath:dbPath];
// 为数据库设置缓存,提高查询效率
[fmdbMgr.fmdb setShouldCacheStatements:YES];
// 根据数据库路径创建数据库管理队列,该对象支持多线程操作
fmdbMgr.queue = [FMDatabaseQueue databaseQueueWithPath:dbPath];
});
return fmdbMgr;
}
@end
if ([CHDbMgr open]) {
NSString *sqlStr = NSStringFormat(@"CREATE TABLE IF NOT EXISTS t_ReadHistory(id INTEGER PRIMARY KEY AUTOINCREMENT, bookId INTEGER, bookName TEXT, readTime TEXT);");
BOOL success = [CHDbMgr executeUpdate:sqlStr];
}
if ([CHDbMgr open]) {
[CHDbMgr executeUpdate:@"insert into t_ReadHistory(bookId, bookName, readTime) values(?,?,?);" , @(1001), @"西游记", @"2020年06月23日"];
}
if ([CHDbMgr open]) {
[CHDbMgr executeUpdateWithForamat:@"insert into t_ReadHistory(bookId, bookName, readTime) values(%ld, %@, %ld);", @(1001), @"西游记", @"2020年06月23日"];
}
if ([CHDbMgr open]) {
[CHDbMgr executeUpdate:@"insert into t_ReadHistory (bookId, bookName, readTime) values(?, ?, ?);" withArgumentsInArray:@[@(1001), @"西游记", @"2020年06月23日"]];
}
if ([CHDbMgr open]) {
[CHDbMgr executeUpdate:@"delete from t_ReadHistory where bookId = ?;", @(1001)];
}
if ([CHDbMgr open]) {
[CHDbMgr executeUpdateWithFormat:@"delete from t_ReadHistory where bookName = %@;", @"西游记"];
}
if ([CHDbMgr open]) {
// FMResultSet结果集
FMResultSet *set = [CHDbMgr executeQuery:@"select bookId, bookName, readTime from t_ReadHistory;"];
// next 返回yes说明有数据
if ([set next]) {
NSInteger bookId = [set intForColumn:@"bookId"];
NSString *bookName = [set stringForColumn:@"bookName"];
NSString *readTime = [set stringForColumn:@"readTime"];
}
else {
CHLog(@"查询出错");
}
}
intForColumn:
longForColumn:
longLongIntForColumn:
boolForColumn:
doubleForColumn:
stringForColumn:
dataForColumn:
dataNoCopyForColumn:
UTF8StringForColumnIndex:
objectForColumn:
if ([CHDbMgr open]) {
// 查符合条件的书的总数
sqlStr = @"select COUNT(*) from t_ReadHistory where bookId = ?;";
NSUInteger bookCount = [CHDbMgr intForQuery:sqlStr, @(1001)];
}
if ([CHDbMgr open]) {
[CHDbMgr executeUpdate:@"update t_ReadHistory set bookName = ? where bookId = ?", @"红楼梦", @(1001)];
}
if ([CHDbMgr open]) {
// 如果表格存在 则销毁
[CHDbMgr executeUpadate:@"drop BookLibrary if existst t_ReadHistory;"];
}
if ([CHDbMgr open]) {
BOOL success = [CHDbMgr executeUpdate:@"insert into t_ReadHistory(bookId, bookName, readTime) values(?,?,?);" , @(1001), @"西游记", @"2020年06月23日"];
if (success) {
// 此处为刚才添加的自增id号
NSInteger logid = CHDbMgr.lastInsertRowId;
CHLog(@"NSInteger == %ld", logid);
}
}
// 会通过block传递队列中创建好的数据库给我们
[CHDbQueue inDatabase:^(FMDatabase *db) {
// 编写需要执行的代码
NSString *sqlStr = NSStringFormat(@"CREATE TABLE IF NOT EXISTS t_ReadHistory(id INTEGER PRIMARY KEY AUTOINCREMENT, bookId INTEGER, bookName TEXT, readTime TEXT);");
BOOL success = [db executeUpdate:sqlStr];
if (success) {
CHLog(@"创建表成功");
}
else {
CHLog(@"创建表失败");
}
}];
// 会通过block传递队列中创建好的数据库给我们
[CHDbQueue inDatabase:^(FMDatabase *db) {
// 编写需要执行的代码
[db executeUpdate:@"insert into t_ReadHistory(bookId, bookName, readTime) values(?,?,?);" , @(1001), @"西游记", @"2020年06月23日"];
[db executeUpdate:@"insert into t_ReadHistory(bookId, bookName, readTime) values(?,?,?);" , @(1002), @"三国演义", @"2020年06月23日"];
}];
原文:https://www.cnblogs.com/CH520/p/13179204.html