1.把FMOD的文件拖入工程,也可以用pod直接导入,这里就不做赘述了
F682E4A0-95B8-4233-B481-F5AD92EFD420.png
2.创建一个单例类
.h文件
#import <Foundation/Foundation.h>
@class ZQWCarModel;
@interface ZQWCarFMDB : NSObject
+(ZQWCarFMDB *)sharedCarFmdb;
@property(strong,nonatomic)FMDatabase *db;
//插入数据
-(void)insertCar:(ZQWCarModel *)model;
//删除数据
-(void)deleteCar:(ZQWCarModel *)model;
//修改数据
-(void)updateCar:(ZQWCarModel *)model;
//查询
-(NSArray *)queryCar;
//清空数据库
-(void)removeAllCar;
@end
.m文件
#import "ZQWCarFMDB.h"
#import "ZQWCarModel.h"
@implementation ZQWCarFMDB
static ZQWCarFMDB *carFmdb = nil;
+(instancetype)allocWithZone:(struct _NSZone *)zone{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
if (carFmdb == nil) {
carFmdb = [[ZQWCarFMDB alloc] init];
//创建数据库
[carFmdb createDataBase];
//创建表
[carFmdb createTable];
}
});
return carFmdb;
}
+(ZQWCarFMDB *)sharedCarFmdb{
// 最好用self 用Tools他的子类调用时会出现错误
return [[self alloc]init];
}
// 为了严谨,也要重写copyWithZone 和 mutableCopyWithZone
-(id)copyWithZone:(NSZone *)zone
{
return carFmdb;
}
-(id)mutableCopyWithZone:(NSZone *)zone
{
return carFmdb;
}
//创建数据库
-(void)createDataBase{
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *filePath = [documentPath stringByAppendingPathComponent:@"car.sqlite"];
self.db = [FMDatabase databaseWithPath:filePath];
}
//创建表
- (void)createTable{
if ([self.db open]) {
//AUTOINCREMENT 自增
BOOL result = [self.db executeUpdate:@"CREATE TABLE IF NOT EXISTS car (carId text PRIMARY KEY NOT NULL, quantity text NOT NULL, productPrice text NOT NULL, productId text NOT NULL, productName text NOT NULL,productImg text NOT NULL,memberId text NOT NULL,productAttribute text NOT NULL,productDescription text NOT NULL,productPrice2Set text NOT NULL,productSn text NOT NULL,site text NOT NULL);"];
if (result) {
//[ProgressHUD showSuccess:@"创建表成功"];
}else{
[ProgressHUD showError:@"创建表失败"];
}
[self.db close];
}else{
[ProgressHUD showError:@"创建表,数据库打开失败"];
}
}
//插入数据
-(void)insertCar:(ZQWCarModel *)model{
if ([self.db open]) {
BOOL result = [self.db executeUpdate:@"INSERT INTO car (carId, quantity,productPrice,productId,productName,productImg,memberId,productAttribute,productDescription,productPrice2Set,productSn,site) VALUES (?,?,?,?,?,?,?,?,?,?,?,?);", model.carId, model.quantity,model.productPrice,model.productId,model.productName,model.productImg,model.memberId,model.productAttribute,model.productDescription,model.productPrice2Set,model.productSn,model.site];
if (result) {
[ProgressHUD showSuccess:[NSString stringWithFormat:@"插入数据成功%@",model.productPrice]];
}else{
[ProgressHUD showError:@"插入数据失败"];
}
[self.db close];
}else{
[ProgressHUD showError:@"插入数据,数据库打开失败"];
}
}
//删除数据
-(void)deleteCar:(ZQWCarModel *)model{
if ([self.db open]) {
BOOL result = [self.db executeUpdate:@"delete from car where carId = ?",model.carId];
if (result) {
[ProgressHUD showSuccess:[NSString stringWithFormat:@"删除数据成功%@",model.productPrice]];
}else{
[ProgressHUD showError:@"删除数据失败"];
}
[self.db close];
}else{
[ProgressHUD showError:@"删除数据,数据库打开失败"];
}
}
//修改数据
-(void)updateCar:(ZQWCarModel *)model{
if ([self.db open]) {
BOOL result = [self.db executeUpdate:@"UPDATE car SET quantity = ?,productPrice = ?,productId = ?,productName = ?,productImg = ?,memberId = ?,productAttribute = ?,productDescription = ?,productPrice2Set = ?,productSn = ?,site = ? WHERE carId = ?",model.quantity,model.productPrice,model.productId,model.productName,model.productImg,model.memberId,model.productAttribute,model.productDescription,model.productPrice2Set,model.productSn,model.site,model.carId];
if (result) {
[ProgressHUD showSuccess:[NSString stringWithFormat:@"修改数据成功%@",model.productPrice]];
}else{
[ProgressHUD showError:@"修改数据库,失败"];
}
[self.db close];
}else{
[ProgressHUD showError:@"修改数据库,打开数据库失败"];
}
}
//查询
-(NSArray *)queryCar{
NSMutableArray *array = [[NSMutableArray alloc]init];
if ([self.db open]) {
FMResultSet *set = [self.db executeQuery:@"SELECT * FROM car"];
while ([set next]) {
ZQWCarModel *model = [[ZQWCarModel alloc] init];
model.carId = [set stringForColumn:@"carId"];
model.quantity = [set stringForColumn:@"quantity"];
model.productPrice = [set stringForColumn:@"productPrice"];
model.productId = [set stringForColumn:@"productId"];
model.productName = [set stringForColumn:@"productName"];
model.productImg = [set stringForColumn:@"productImg"];
model.productAttribute = [set stringForColumn:@"productAttribute"];
model.productDescription = [set stringForColumn:@"productDescription"];
model.productPrice2Set = [set stringForColumn:@"productPrice2Set"];
model.memberId = [set stringForColumn:@"memberId"];
model.productSn = [set stringForColumn:@"productSn"];
model.site = [set stringForColumn:@"site"];
[array addObject:model];
}
[self.db close];
}else{
[ProgressHUD showError:@"查询数据库,打开数据库失败"];
}
return array;
}
//清空数据
-(void)removeAllCar{
if ([self.db open]) {
BOOL result = [self.db executeUpdate:@"DELETE FROM car"];
if (result) {
[ProgressHUD showSuccess:@"数据清空成功"];
}else{
[ProgressHUD showError:@"数据清空失败"];
}
[self.db close];
}else{
[ProgressHUD showError:@"清空数据库,打开数据库失败"];
}
}
//使用方式
1.获取数据
self.dataArr = [[ZQWCarFMDB sharedCarFmdb] queryCar].mutableCopy;
2.插入数据
[[ZQWCarFMDB sharedCarFmdb] insertCar:self.model];
3.修改数据
[[ZQWCarFMDB sharedCarFmdb] updateCar:model];
4.删除单个数据
[[ZQWCarFMDB sharedCarFmdb] deleteCar:model];
5.删除全部数据
[[ZQWCarFMDB sharedCarFmdb] removeAllCar];
网友评论