#import <Foundation/Foundation.h>
@class GoodsDetailsVO;
extern NSString * DataStoreType(int level);
extern NSString * tableName;
@interface DatabaseCenter : NSObject
/**
* 獲取單例對象
*/
+ (DatabaseCenter *)shareInstance;
/**
* 添加一條數據
*/
- (BOOL)addRecordWithGoodsDetailsVO:(GoodsDetailsVO *)model;
/**
* 刪除一條數據
*/
- (BOOL)removeRecordWithGoodsDetailsVO:(GoodsDetailsVO *)model;
/**
* 檢查某件商品是否被記錄過
*/
- (BOOL)isExistRecordWithGoodsDetailsVO:(GoodsDetailsVO *)model;
/**
* 獲取記錄的列表
*/
- (NSArray *)recordList;
/**
* 刪除所有數據
*/
- (void)removeAllRecord;
@end
#import "DatabaseCenter.h"
#import "GoodsDetailsVO.h"
#import "FMDatabase.h"
@interface DatabaseCenter ()
{
FMDatabase *_database;
}
@end
@implementation DatabaseCenter
NSString* DataStoreType (int type)
{
NSString *str;
switch (type)
{
case 0:
str = @"GoodsRecord";
break;
case 1:
str = @"GoodsCollect";
break;
default:
break;
}
return str;
}
/**
* 獲取單例對象
*/
+ (DatabaseCenter *)shareInstance
{
staticDatabaseCenter *dc =nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
dc = [[[self class] alloc]init];
});
[dc createDatabaseTable];
return dc;
}
- (id)init
{
if(self = [superinit])
{
[selfinitDatabaseTwoDataStore];
}
return self;
}
/**
* 初始化數據庫
*/
- (void)initDatabaseTwoDataStore
{
NSString *path = [NSStringstringWithFormat:@"%@/Documents/data.sqlite",NSHomeDirectory()];
_database = [[FMDatabasealloc]initWithPath:path];
if(!_database.open)
{
return;
}
}
/**
* 創建數據表
*/
- (void)createDatabaseTable
{
FMResultSet * set = [_databaseexecuteQuery:[NSStringstringWithFormat:@"select count(*) from sqlite_master where type ='table' and name = '%@'",tableName]];
[set next];
NSInteger count = [set intForColumnIndex:0];
BOOL existTable = !!count;
if (existTable)
{
// TODO:是否更新數據庫
NSLog(@"數據表已存在");
}
else
{
// TODO: 插入新的數據庫 (此處字段約束說明,可以不寫)
//create table if not exists 這樣寫的話,即使已經存在這個表也能創建成功
//primary key 主鍵
//autoincrement not null 自增並且不能爲空
NSString *sql = [NSStringstringWithFormat:@"create table if not exists %@ ("
" id integer primary key autoincrement not null, "
" mpId integer not null, "
" name varchar(128), "
" urlImage varchar(1024), "
" price integer "
");",tableName];
BOOL res = [_databaseexecuteUpdate:sql];
if (!res)
{
NSLog(@"數據表創建失敗");
}
else
{
NSLog(@"數據表創建成功");
}
}
}
/**
* 添加一條記錄
*/
- (BOOL)addRecordWithGoodsDetailsVO:(GoodsDetailsVO *)model
{
BOOL ret = NO;
NSString *sql = [NSStringstringWithFormat:@"insert into %@ (mpId,name,urlImage,price) values(?,?,?,?)",tableName];
//executeUpdate其他參數一律時字符串格式
BOOL b = [_databaseexecuteUpdate:sql,
model.mpId,
model.name,
model.urlImage,
model.price];
if(!b)
{
NSLog(@"插入失敗!");
ret = NO;
}
else
{
NSLog(@"插入成功");
ret = YES;
}
return ret;
}
/**
* 刪除一條數據
*/
- (BOOL)removeRecordWithGoodsDetailsVO:(GoodsDetailsVO *)model
{
BOOL ret = NO;
NSString *sql = [NSStringstringWithFormat:@"delete from %@ where mpId=?",tableName];
BOOL b = [_databaseexecuteUpdate:sql,
model.mpId];
if(!b)
{
NSLog(@"刪除失敗!");
ret = NO;
}
else
{
NSLog(@"刪除成功!");
ret = YES;
}
return ret;
}
/**
* 檢查某件商品是否被瀏覽過
*/
- (BOOL)isExistRecordWithGoodsDetailsVO:(GoodsDetailsVO *)model
{
NSString *sql = [NSStringstringWithFormat:@"select * from %@ where mpId=?",tableName];
FMResultSet *resultSet = [_databaseexecuteQuery:sql,
model.mpId];
int count=0;
while ([resultSet next])
{
count++;
}
return count>0;
}
/**
* 獲取記錄的列表
*/
- (NSArray *)recordList
{
NSString *sql = [NSStringstringWithFormat:@"select * from %@",tableName];
FMResultSet *resultSet = [_databaseexecuteQuery:sql];
NSMutableArray *marr = [[NSMutableArrayalloc]init];
while([resultSet next])
{
GoodsDetailsVO *model = [[GoodsDetailsVOalloc]init];
model.mpId = [resultSet stringForColumn:@"mpId"];
model.name = [resultSet stringForColumn:@"name"];
model.urlImage = [resultSet stringForColumn:@"urlImage"];
model.price = [resultSet stringForColumn:@"price"];
[marr addObject:model];
}
return marr;
}
/**
* 刪除所有數據
*/
- (void)removeAllRecord
{
NSArray *arr = [selfrecordList];
for (int i =0; i < arr.count; i++)
{
DatabaseCenter *dc = [DatabaseCentershareInstance];
[dc removeRecordWithGoodsDetailsVO:arr[i]];
}
}
@end
NSString *tableName;
//標註使用哪個表
tableName = DataStoreType(0);
//初始化對象
DatabaseCenter *dc = [DatabaseCentershareInstance];
//判斷是否記錄過此條數據
if (![dcisExistRecordWithGoodsDetailsVO:self.detailModel])
{
//將添加過的數據,保存到數據庫
[dc addRecordWithGoodsDetailsVO:self.detailModel];
}
//獲取記錄列表
self.dataList = [DatabaseCentershareInstance].recordList;
GoodsRecordCell *cell = (GoodsRecordCell*)[tableViewcellForRowAtIndexPath:indexPath];
[dc removeRecordWithGoodsDetailsVO:cell.model];
[dc removeAllRecord];
#import <Foundation/Foundation.h>
@class AMapPOI;
@interface DataBaseCenter : NSObject
/**
* 獲取單例對象
*/
+ (DataBaseCenter *)shareInstance;
/**
* 添加一條數據
*/
- (void)addHistoryrRecordWithAMapPOI:(AMapPOI *)amapPoi;
/**
* 刪除一條數據
*/
- (void)removeHistoryrRecordWithAMapPOI:(AMapPOI *)amapPoi;
/**
* 檢查此地址是否已經存入歷史記錄
*/
- (BOOL)isExistHistoryrRecordWithAMapPOI:(AMapPOI *)amapPoi;
/**
* 獲取記錄的列表
*/
- (NSArray *)recordList;
/**
* 刪除所有數據
*/
- (void)removeAllRecord;
@end
#import "DataBaseCenter.h"
@interface DataBaseCenter ()
{
FMDatabase *_database;
}
@end
@implementation DataBaseCenter
/**
* 獲取單例對象
*/
+ (DataBaseCenter *)shareInstance
{
staticDataBaseCenter *dc =nil;
if(dc == nil)
{
dc = [[[self class] alloc]init];
}
return dc;
}
- (id)init
{
if(self = [superinit])
{
[selfinitDataBaseDataStore];
}
return self;
}
/**
* 初始化數據庫
*/
- (void)initDataBaseDataStore
{
NSString *path = [NSStringstringWithFormat:@"%@/Documents/data.sqlite",NSHomeDirectory()];
_database = [[FMDatabasealloc]initWithPath:path];
if(!_database.open)
{
return;
}
//創建歷史記錄數據表
NSString *sql =@"create table if not exists historyrRecordList(uid,name,type,address,province,pcode,city,citycode,district,adcode,gridcode,latitude,longitude)";
BOOL b = [_databaseexecuteUpdate:sql];
if(!b)
{
NSLog(@"數據表創建失敗!");
}
else
{
NSLog(@"數據表創建成功!");
}
}
/**
* 添加一條記錄
*/
- (void)addHistoryrRecordWithAMapPOI:(AMapPOI *)amapPoi;
{
NSString *sql =@"insert into historyrRecordList (uid,name,type,address,province,pcode,city,citycode,district,adcode,gridcode,latitude,longitude) values(?,?,?,?,?,?,?,?,?,?,?,?,?)";
BOOL b = [_databaseexecuteUpdate:sql,
amapPoi.uid,
amapPoi.name,
amapPoi.type,
amapPoi.address,
amapPoi.province,
amapPoi.pcode,
amapPoi.city,
amapPoi.citycode,
amapPoi.district,
amapPoi.adcode,
amapPoi.gridcode,
[NSString stringWithFormat:@"%f",amapPoi.location.latitude],
[NSString stringWithFormat:@"%f",amapPoi.location.longitude]];
if(!b)
{
NSLog(@"插入失敗!");
}
else
{
NSLog(@"插入成功");
}
}
/**
* 刪除一條數據
*/
- (void)removeHistoryrRecordWithAMapPOI:(AMapPOI *)amapPoi;
{
NSString *sql =@"delete from historyrRecordList where uid=?";
BOOL b = [_databaseexecuteUpdate:sql,
amapPoi.uid];
if(!b)
{
NSLog(@"刪除失敗!");
}
else
{
NSLog(@"刪除成功!");
}
}
/**
* 檢查此地址是否已經存入歷史記錄
*/
- (BOOL)isExistHistoryrRecordWithAMapPOI:(AMapPOI *)amapPoi;
{
NSString *sql =@"select * from historyrRecordList where uid=?";
FMResultSet *resultSet = [_databaseexecuteQuery:sql,
amapPoi.uid];
int count=0;
while ([resultSet next])
{
count++;
}
return count>0;
}
/**
* 獲取記錄的列表
*/
- (NSArray *)recordList;
{
NSString *sql =@"select * from historyrRecordList";
FMResultSet *resultSet = [_databaseexecuteQuery:sql];
NSMutableArray *marr = [[NSMutableArrayalloc]init];
while([resultSet next])
{
AMapPOI *amapPoi = [[AMapPOIalloc]init];
amapPoi.uid = [resultSet stringForColumn:@"uid"];
amapPoi.name = [resultSet stringForColumn:@"name"];
amapPoi.type = [resultSet stringForColumn:@"type"];
amapPoi.address = [resultSet stringForColumn:@"address"];
amapPoi.province = [resultSet stringForColumn:@"province"];
amapPoi.pcode = [resultSet stringForColumn:@"pcode"];
amapPoi.city = [resultSet stringForColumn:@"city"];
amapPoi.citycode = [resultSet stringForColumn:@"citycode"];
amapPoi.district = [resultSet stringForColumn:@"district"];
amapPoi.adcode = [resultSet stringForColumn:@"adcode"];
amapPoi.gridcode = [resultSet stringForColumn:@"gridcode"];
AMapGeoPoint *locationT = [[AMapGeoPointalloc]init];
locationT.latitude = [[resultSet stringForColumn:@"latitude"] floatValue];
locationT.longitude = [[resultSet stringForColumn:@"longitude"] floatValue];
amapPoi.location = locationT;
[marr addObject:amapPoi];
}
return marr;
}
/**
* 刪除所有數據
*/
- (void)removeAllRecord;
{
NSArray *arr = [selfrecordList];
for (int i =0; i < arr.count; i++)
{
DataBaseCenter *dc = [DataBaseCentershareInstance];
[dc removeHistoryrRecordWithAMapPOI:arr[i]];
}
}
@end