1.添加fmdb
- 点击这里下载fmdb
- 在项目中TARGETS->Build Phases ->Link Binary With Libraries 添加libsqlite3.tdb
- 把fmdb拖到自己的项目中 添加头文件
#import "DBManager.h"
- TARGETS->Build Phases ->Compile Sources 中找到
FMDatabaseQueue.m
和FMDatabasePool.m
,改为非arc (-fno-objc-arc)
这下运行fmdb就不会报错了
2.SQL语句
我项目中使用的代码
DBManager.h
#import <Foundation/Foundation.h>
#import "BookDetailModel.h"
@interface DBManager : NSObject
+ (DBManager *)shareManager;
//包含这条数据的话 我们显示取消收藏 没有这条数据的话 我们应该收藏
- (BOOL)isExistsDataWithModel:(BookDetailModel *)model;
//收藏 插入一条数据
- (BOOL)insertDataWithModel:(BookDetailModel *)model;
- (BOOL)deleteDataWithModel:(BookDetailModel *)model;
//更改一条数据
- (BOOL)changeDataWithModel:(BookDetailModel *)model newStr:(NSString *)str;
//返回所有数据
- (NSArray *)allData;
@end
DBManager.m
#import "DBManager.h"
#import "FMDatabase.h"
@implementation DBManager{
//操作数据库的对象
FMDatabase *_fmdb;
}
+ (DBManager *)shareManager{
static DBManager *_dB = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
if (_dB == nil) {
_dB = [[DBManager alloc] init];
}
});
return _dB;
}
- (id)init{
self = [super init];
if (self) {
//fmdb对象 他的实例化 需要一个路径
//[NSBundle mainBundle]
//user/Documents/photoshops/....
//当使用 NSHomeDirectory() 你得到了3个文件夹(路径 你所在的地方有三个文件夹)
//NSHomeDirectory() -> NSString
NSString *path = [NSHomeDirectory() stringByAppendingString:@"/Documents/appa.db"];
NSLog(@"打印数据库路径:%@",path);
_fmdb = [[FMDatabase alloc] initWithPath:path];
if ([_fmdb open]) {
NSLog(@"打开数据库成功");
//我们打开数据库之后 需要创建表格
/*
create table if not exists app(id varchar(32),name varchar(128),pic varchar(1024))
*/
NSString *sql = @"create table if not exists app(_id varchar(32),updated varchar(128),cover varchar(1024),title varchar(128))";
BOOL isSuccess = [_fmdb executeUpdate:sql];
if (isSuccess) {
NSLog(@"create Tabel Success");
}else{
NSLog(@"create Tabel Fail:%@",_fmdb.lastErrorMessage);
}
}else{
NSLog(@"open Sqlite File");
}
}
return self;
}
#pragma mark - 数据库操作
//这个表格里面是否包含这个数据
//查询
- (BOOL)isExistsDataWithModel:(BookDetailModel *)model{
/*
select applicationid from app where applicationid = ?
*/
NSString *sql = @"select _id from app where _id = ?";
//_fmdb
//这个方法是查询
FMResultSet *set = [_fmdb executeQuery:sql,model._id];
//[set next] 如果你查询到了 这个方法会返回一个真值
return [set next];
}
//插入
- (BOOL)insertDataWithModel:(BookDetailModel *)model{
/*
insert into app (applicationId,name,iconUrl) values (?,?,?)
*/
NSString *sql = @"insert into app (_id,updated,cover,title) values (?,?,?,?)";
BOOL isSuccess = [_fmdb executeUpdate:sql,model._id,model.updated,model.cover,model.title];
if (isSuccess) {
NSLog(@"插入成功");
}else{
NSLog(@"插入失败:%@",_fmdb.lastErrorMessage);
}
return isSuccess;
}
- (BOOL)deleteDataWithModel:(BookDetailModel *)model{
NSString *sql = @"delete from app where _id = ?";
BOOL isSuccess = [_fmdb executeUpdate:sql,model._id];
if (isSuccess) {
NSLog(@"删除成功");
}else{
NSLog(@"删除失败%@",_fmdb.lastErrorMessage);
}
return isSuccess;
}
//修改
//- (BOOL)changeDataWithModel:(BookDetailModel *)model newStr:(NSString *)str{
//
// NSString *sql = @"update app set name = ? where name = ?";
// BOOL isSuccess = [_fmdb executeUpdate:sql,str,model.name];
// if (isSuccess) {
// NSLog(@"修改成功");
// }else{
// NSLog(@"修改失败:%@",_fmdb.lastErrorMessage);
// }
// return isSuccess;
//}
- (NSArray *)allData{
NSString *sql = @"select * from app";
FMResultSet *set = [_fmdb executeQuery:sql];
//装数据模型
NSMutableArray *array = [NSMutableArray array];
while ([set next]) {
BookDetailModel *model = [[BookDetailModel alloc] init];
model.title = [set stringForColumn:@"title"];
model._id = [set stringForColumn:@"_id"];
model.cover = [set stringForColumn:@"cover"];
model.updated = [set stringForColumn:@"updated"];
[array addObject:model];
}
return array;
}
/*
删除
delete from app where applicationId = ?
更新一条数据
update app set name = ? where name = ?
查找整张表
select * from app
*/
@end
这些简单的增删改,足够我们日常使用了
再补充一点
- 一个数据库可以创建多个表格
下面在数据库/Documents/appa.db
中再创建一个
网友评论