1、首先要在项目中导入FMDB,具体方法可在github上查看https://github.com/ccgus/fmdb
2、注意事项:
(1)数据库的语法在Xcode里面没有提示,所以我们不能写错一个单词,要不然数据库就会报错!!!
(2)对数据库进行操作时都要先打开数据库,操作完毕后一定要关上数据库!!!
3、继承NSObject建一个类,这里我们取名为DBManager
4、在.m文件里导入头文件#import <FMDatabase.h>
5、定义一个成员变量 FMDatabase *_dataBase;
6、创建一个单例
+ (DBManager *)sharedManager {
static DBManager *manager = nil;
static dispatch_once_t onceTpken;
dispatch_once(&onceTpken, ^{
manager = [[DBManager alloc]init];
});
return manager;
}
7、创建数据库表
- (void)createSingleDataBase {
NSString *singlePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches/single.sqlite3"];
_dataBase = [[FMDatabase alloc] initWithPath:singlePath];
BOOL ret = [_dataBase open];//打开数据库
if (ret)
{
//kSingleId为唯一标识,设置成自动增加,后面的参数要跟model里的实例变量对应
//NSString类型 varchar(255)
//image 类型 blob
//int 类型 integer
NSString *singleSql = @"create table if not exists mySingle (kSingleId integer primary key autoincrement, CommentCount integer, HeadImg varchar(255), Memo varchar(255), NickName varchar(255), PublicStatus integer, SingleId integer, SingleName varchar(255), TypeId integer, TypeName varchar(255), UserId integer)";
BOOL flag1 = [_dataBase executeUpdate:singleSql];
if (!flag1)
{
NSLog(@"%@",_dataBase.lastErrorMessage);
}
}
else
NSLog(@"打开数据库失败");
[_dataBase close];//关闭数据库
}
8、在数据库里增加数据
//增加多条数据
- (void)addSingleModel:(NSMutableArray *)modelArray {
BOOL ret = [_dataBase open];//打开数据库
if (ret)
{
NSString *sql=@"";
for (SingleListModel* model in modelArray) {
//数据库插入语句,into后面的表名和我们刚才创建的表名要一样!!!
sql = @"insert into mySingle (CommentCount, HeadImg, Memo, NickName, PublicStatus, SingleId, SingleName, TypeId, TypeName, UserId) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
//插入数据,更新数据库
BOOL flag = [_dataBase executeUpdate:sql,model.CommentCount,model.HeadImg,model.Memo,model.NickName,model.PublicStatus,model.SingleId,model.SingleName,model.TypeId,model.TypeName,model.UserId ?: [NSNull null]];
if (!flag) {
NSLog(@"%@",_dataBase.lastErrorMessage);
}
}
}
[_dataBase close];//关闭数据库
}```
//插入单条数据
-
(void)addSearchModel:(NSString *)Memo {
BOOL ret = [_dataBase open];
NSString *sql = @"";
if (ret)
{
sql = @"insert into mySingle (Memo) values (?)";
BOOL flag = [_dataBase executeUpdate:sql,Memo];
if (!flag)
{
NSLog(@"%@",_dataBase.lastErrorMessage);
}
}
[_dataBase close];
}
9、查询数据库
-
(NSArray *)searchAllSingleData {
BOOL ret = [_dataBase open];
NSMutableArray *array = [NSMutableArray array];
if(ret)
{
NSString *sql = @"select * from mySingle";
FMResultSet *rs = [_dataBase executeQuery:sql];
while ([rs next])
{
SingleListModel *model = [[SingleListModel alloc] init];
model.CommentCount = [rs stringForColumn:@"CommentCount"];
model.HeadImg = [rs stringForColumn:@"HeadImg"];
model.Memo = [rs stringForColumn:@"Memo"];
model.NickName =[rs stringForColumn:@"NickName"];
model.PublicStatus = [rs stringForColumn:@"PublicStatus"];
model.SingleId = [rs stringForColumn:@"SingleId"];
model.SingleName = [rs stringForColumn:@"SingleName"];
model.TypeId = [rs stringForColumn:@"TypeId"];
model.TypeName = [rs stringForColumn:@"TypeName"];
model.UserId = [rs stringForColumn:@"UserId"];[array addObject:model]; }
}
[_dataBase close];
return array;
}```
10、删除数据
//删除所有数据
- (void)deleteAllSingle {
[_dataBase executeUpdate:@"DELETE FROM mySingle"];
[_dataBase executeUpdate:@"UPDATE sqlite_sequence set seq=0 where name='mySingle'"];
}```
//删除单条数据
-
(void)deleteOneSearch:(NSString *)Memo {
BOOL ret = [_dataBase open];
if(ret) {BOOL flag = [_dataBase executeUpdateWithFormat:@"delete from mySingle where Memo = %@",Memo]; if (!flag) { NSLog(@"==%@",_dataBase.lastErrorMessage); }
}
[_dataBase close];
}
11、修改数据库的数据
-(void)updateUserModel:(SingleListModel *)model kSingleId:(int) kSingleId
{
BOOL ret = [_dataBase open];
if(ret) {
NSString *sql = @"update mySingle set Memo = ?, NickName = ?, HeadImg = ? where kSingleId = ?";
//将图片转换成二进制数据
NSData *data = UIImagePNGRepresentation(model. HeadImg);
BOOL ret = [_dataBase executeUpdate:sql,model.Memo,model.NickName,data,@(kSingleId)];
if (!ret)
{
NSLog(@"%@",_dataBase.lastErrorMessage);
}
}
[_dataBase close];
}
以上就是数据库的增删改查的基本操作啦,记住数据库打开,一定要关闭!!!