-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCTYFMDBSqliteProvider.m
76 lines (67 loc) · 2.02 KB
/
CTYFMDBSqliteProvider.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//
// CTYFMDBSqliteProvider.m
// ChuanTaiYi-Client
//
// Created by yang on 16/1/21.
// Copyright © 2016年 ChuanTaiYi. All rights reserved.
//
#import <FMDB/FMDB.h>
#import "CTYFMDBSqliteProvider.h"
@interface CTYFMDBSqliteProvider()
{
FMDatabase *_dataBase;
}
@end
@implementation CTYFMDBSqliteProvider
+ (instancetype)defaultProvider
{
static CTYFMDBSqliteProvider *_sqliteProvider = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sqliteProvider = [[CTYFMDBSqliteProvider alloc] initWithDatabase];
});
return _sqliteProvider;
}
- (instancetype)initWithDatabase
{
if (self = [super init]) {
NSString * path = [NSString stringWithFormat:@"%@/Documents/data.db",NSHomeDirectory()];
_dataBase = [[FMDatabase alloc] initWithPath:path];
if ([_dataBase open]) {
NSLog(@"[ACTION]: open database success");
[_dataBase executeUpdate:@"create table IF NOT EXISTS history(historyname)"];
}else{
NSLog(@"[ERROR]: failed to open database");
}
}
return self;
}
- (BOOL)insertValueWithString:(NSString *)string
{
FMResultSet *result = [_dataBase executeQuery:@"select * from history where historyname = ?", string];
if ([result next]) {
[_dataBase executeUpdate:@"delete from history where historyname = ?", string];
}
BOOL isInsertDataBase = [_dataBase executeUpdate:@"insert into history values(?)", string];
if (isInsertDataBase) {
NSLog(@"[ACTION]:insert a value to database");
}else{
NSLog(@"[ERROR]: failed to insert a value to database");
}
return isInsertDataBase;
}
- (void)deleteSQLite
{
BOOL isDelete = [_dataBase executeUpdate:@"delete from history"];
if (isDelete) {
NSLog(@"[VERBOSE]: delete the history table");
}else{
NSLog(@"[ERROR]: failed delete the history table");
}
}
- (FMResultSet *)queryObjectFromDatabase
{
FMResultSet * result = [_dataBase executeQuery:@"select * from history"];
return result;
}
@end