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;
+(ZQWCarFMDB *)sharedCarFmdb{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
if (carFmdb == nil) {
carFmdb = [[ZQWCarFMDB alloc] init];
//創建數據庫
[carFmdb createDataBase];
//創建表
[carFmdb createTable];
}
});
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];