#import
#import "Team9SaveModel.h"
#import
//#import "DesignerModel.h"
//#import "FindModel.h"
typedef NS_ENUM(NSUInteger, SelectInTable) {
InTable,
NotInTable,
SelectError,
};
@interface DataBaseTool : NSObject
{
sqlite3 *dbPoint;
}
// 查询 model 的类型 因为可能有没有类型的model 如果存了 查一下 比申请快
- (NSString *)searchType:(Team9SaveModel *)model;
+(DataBaseTool *)shareDataBaseTool;
//打开
-(void)openDB;
-(void)creatTable;
-(void)insertDesigner:(Team9SaveModel *)model;
-(void)deleteDesigner:(Team9SaveModel *)model;
-(SelectInTable)isHave:(Team9SaveModel *)model;
-(NSMutableArray *)selectAllDesigner;
@end
#import "DataBaseTool.h"
@implementation DataBaseTool
+(DataBaseTool *)shareDataBaseTool
{
static DataBaseTool *tool;
static dispatch_once_t oneToken;
dispatch_once(&oneToken,^{
tool=[[DataBaseTool alloc] init];
[tool openDB];
[tool creatTable];
});
return tool;
}
//打开
-(void)openDB
{
NSArray *sandBox=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *sandBoxPath=sandBox[0];
NSString *documentPath=[sandBoxPath stringByAppendingPathComponent:@"Name.sqlite"];
int result=sqlite3_open([documentPath UTF8String], &dbPoint);
if (result == SQLITE_OK) {
NSLog(@"YES");
// NSLog(@"%@",documentPath);
}else
{
NSLog(@"NO");
}
}
//创建表格
-(void)creatTable
{
NSString *sqlStr=@"create table if not exists designer(number integer primary key autoincrement,author_nick text ,author_face text,author_id text, author_type text)";
int result=sqlite3_exec(dbPoint, [sqlStr UTF8String], nil,nil,nil);
if (result == SQLITE_OK) {
NSLog(@"表创建成功");
}else{
NSLog(@"表创建失败");
}
}
//添加
-(void)insertDesigner:(Team9SaveModel *)model
{
NSString *sqlStr=[NSString stringWithFormat:@"insert into designer (author_face,author_nick,author_id, author_type)values ('%@','%@','%@','%@')", model.author_face, model.author_nick,model.author_id, model.author_type];
int result=sqlite3_exec(dbPoint, [sqlStr UTF8String], nil, nil, nil);
if (result == SQLITE_OK) {
NSLog(@"添加成功");
}else
{
NSLog(@"添加失败");
NSLog(@"%d",result);
}
}
//删除
-(void)deleteDesigner:(Team9SaveModel *)model
{
NSString *sqlStr=[NSString stringWithFormat:@"delete from designer where author_id='%@'",model.author_id];
int result=sqlite3_exec(dbPoint, [sqlStr UTF8String], nil, nil, nil);
if (result == SQLITE_OK) {
NSLog(@"删除成功");
}else
{
NSLog(@"删除失败");
}
}
//判断是否已经含有
-(SelectInTable)isHave:(Team9SaveModel *)model
{
NSString *sqlStr = [NSString stringWithFormat:@"select * from designer where author_id = '%@'",model.author_id];
sqlite3_stmt *stmt;
int result = sqlite3_prepare_v2(dbPoint, [sqlStr UTF8String], -1, &stmt, nil);
if (result == SQLITE_OK) {
if (sqlite3_step(stmt) == SQLITE_ROW) {
return InTable;
}else
{
return NotInTable;
}
}
else
{
NSLog(@"查询失败");
}
return SelectError;
}
- (NSString *)searchType:(Team9SaveModel *)model{
NSString *sqlStr = [NSString stringWithFormat:@"select * from designer where author_id = '%@'",model.author_id];
sqlite3_stmt *stmt;
int result = sqlite3_prepare_v2(dbPoint, [sqlStr UTF8String], -1, &stmt, nil);
if (result == SQLITE_OK) {
NSLog(@"查询成功");
while (sqlite3_step(stmt) == SQLITE_ROW) {
const unsigned char *type = sqlite3_column_text(stmt, 4);
NSString *designerType = [NSString stringWithUTF8String:(const char *)type];
return designerType;
}
}
return nil;
}
-(NSMutableArray *)selectAllDesigner
{
NSString *sqlStr=@"select * from designer";
sqlite3_stmt *stmt = nil;
int result = sqlite3_prepare_v2(dbPoint, [sqlStr UTF8String], -1, &stmt, nil);
NSMutableArray *designerArr=[NSMutableArray array];
if (result == SQLITE_OK) {
NSLog(@"查询成功");
while (sqlite3_step(stmt) == SQLITE_ROW) {
const unsigned char *nick=sqlite3_column_text(stmt, 1);
const unsigned char *face = sqlite3_column_text(stmt, 2);
const unsigned char *userId = sqlite3_column_text(stmt, 3);
const unsigned char *type = sqlite3_column_text(stmt, 4);
NSString *designerNick = [NSString stringWithUTF8String:(const char *)nick];
NSString *designerFace = [NSString stringWithUTF8String:(const char *)face];
NSString *designerId = [NSString stringWithUTF8String:(const char *)userId];
NSString *designerType = [NSString stringWithUTF8String:(const char *)type];
Team9SaveModel *model = [Team9SaveModel new];
model.author_nick =designerNick;
model.author_face = designerFace;
model.author_id = designerId;
model.author_type = designerType;
[designerArr addObject:model];
}
}else
{
NSLog(@"查询失败");
NSLog(@"%d",result);
}
//释放掉(出现5的错误,就加这句话)
sqlite3_finalize(stmt);
return designerArr;
}
@end