DBAction.m
8.03 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
//
// DBAction.m
// SCIOLive
//
// Created by wei on 14-9-29.
// Copyright (c) 2014年 Miao Long. All rights reserved.
//
#import "DBAction.h"
#import "FMDatabase.h"
FMDatabase * db = nil;
static DBAction *instance = nil;
@implementation DBAction
+ (DBAction *)sharedDBAction{
if (instance == nil){
instance = [[super allocWithZone:nil]init];
}
return instance;
}
+(id)allocWithZone:(NSZone *)zone{
return [DBAction sharedDBAction];
}
//可写可不写
- (id)init{
self = [super init];
if (self) {
db = [[FMDatabase alloc] initWithPath:SQLITE_PATH];
}
return self;
}
-(id)copy{
return self;
}
- (id)copyWithZone:(NSZone *)zone{
return self;
}
-(void)createNoticeTable{
if ([db open]) {
NSString * sql = @"CREATE TABLE NOTICE (time varchar, title varchar, content varchar)";
BOOL res = [db executeUpdate:sql];
if (!res) {
NSLog(@"建表失败");
} else {
NSLog(@"建表成功");
}
[db close];
} else {
NSLog(@"打开失败");
}
}
- (BOOL)isInNoticeTable:(NoticeData *)notice{
if ([db open]) {
NSString * check = [NSString stringWithFormat:@"SELECT * FROM NOTICE where time = %@",notice.time];
FMResultSet * re = [db executeQuery:check];
if ([re next]) {
return YES;
}
[db close];
}
return NO;
}
- (void)insertNoticeTable:(NoticeData*)notice{
if ([db open]) {
NSString * check = [NSString stringWithFormat:@"SELECT * FROM NOTICE where time = %@",notice.time];
FMResultSet * re = [db executeQuery:check];
if ([re next]) {
NSLog(@"已存在,进行更新操作");
NSString * sql = [NSString stringWithFormat:@"UPDATE NOTICE SET title = '%@', content = '%@' WHERE time = %@'",notice.time,notice.title,notice.content];
BOOL res = [db executeUpdate:sql];
if (!res) {
NSLog(@"error to insert data");
} else {
NSLog(@"succ to insert data");
}
}else{
NSString * sql = [NSString stringWithFormat:@"INSERT INTO NOTICE (time, title, content) values('%@', '%@', '%@')",notice.time,notice.title,notice.content];
BOOL res = [db executeUpdate:sql];
if (!res) {
NSLog(@"error to insert data");
} else {
NSLog(@"succ to insert data");
}
}
[db close];
}
}
- (void)deleteNotice:(NoticeData*)notice{
if ([db open]) {
NSString * sql = [NSString stringWithFormat:@"DELETE FROM NOTICE where time = %@",notice.time];
BOOL res = [db executeUpdate:sql];
if (!res) {
NSLog(@"error to delete db data");
} else {
NSLog(@"succ to deleta db data");
}
[db close];
}
}
- (NSMutableArray *)queryNoticeTable{
NSMutableArray *list = [[NSMutableArray alloc] init];
if ([db open]) {
NSString * sql = @"SELECT * FROM NOTICE";
FMResultSet * rs = [db executeQuery:sql];
while ([rs next]) {
NoticeData * notice = [[NoticeData alloc] init];
notice.time = [rs stringForColumn:@"time"];
notice.title = [rs stringForColumn:@"title"];
notice.content = [rs stringForColumn:@"content"];
[list addObject:notice];
}
[db close];
}
return list;
}
- (void)createTable{
if ([db open]) {
NSString * sql = @"CREATE TABLE DATATABLE (cmsColumnName varchar, cmsColumnId varchar, cmsContentId int, createDate varchar, desc varchar, gxbArticleId int, gxbVideoId int, img varchar, pageUrl varchar, picType varchar, playType varchar, publishDate varchar, subTitle varchar, title varchar, type varchar, uploadVideoUrl varchar, videoUrl varchar, vipFlag int, uuid varchar)";
BOOL res = [db executeUpdate:sql];
if (!res) {
NSLog(@"建表失败");
NSString *sql1 = [NSString stringWithFormat:@"ALTER TABLE %@ ADD %@ VARCHAR",@"DATATABLE",@"playType"];
BOOL result1 = [db executeUpdate:sql1];
NSString *sql2 = [NSString stringWithFormat:@"ALTER TABLE %@ ADD %@ VARCHAR",@"DATATABLE",@"uuid"];
BOOL result2 = [db executeUpdate:sql2];
} else {
NSLog(@"建表成功");
}
[db close];
} else {
NSLog(@"打开失败");
}
}
- (BOOL)isInTable:(TableData*)notice{
if ([db open]) {
NSString * check = [NSString stringWithFormat:@"SELECT * FROM DATATABLE where cmsContentId = %d",notice.cmsContentId];
FMResultSet * re = [db executeQuery:check];
while ([re next]) {
return YES;
}
[db close];
}
return NO;
}
- (BOOL)insertTable:(TableData*)notice{
if ([db open]) {
NSString * check = [NSString stringWithFormat:@"SELECT * FROM DATATABLE where cmsContentId = %d",notice.cmsContentId];
FMResultSet * re = [db executeQuery:check];
if (![re next]) {
NSString *desc = [notice.desc stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
NSString * sql = [NSString stringWithFormat:@"INSERT INTO DATATABLE (cmsColumnName, cmsColumnId, cmsContentId, createDate, desc, gxbArticleId, gxbVideoId, img, pageUrl, picType, playType, publishDate, subTitle, title, type, uploadVideoUrl, videoUrl, vipFlag, uuid) values('%@','%@','%d', '%@','%@','%d','%d', '%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%d', '%@')", notice.cmsColumnName, notice.cmsColumnId, notice.cmsContentId, notice.createDate, desc, notice.gxbArticleId, notice.gxbVideoId, notice.img, notice.pageUrl, notice.picType, notice.playType, notice.publishDate, notice.subTitle, notice.title, notice.type, notice.uploadVideoUrl, notice.videoUrl, notice.vipFlag, notice.uuid?notice.uuid:@""];
BOOL res = [db executeUpdate:sql];
if (!res) {
return NO;
} else {
return YES;
}
}
[db close];
}
return NO;
}
- (BOOL)deleteTable:(TableData*)notice{
if ([db open]) {
NSString * sql = [NSString stringWithFormat:@"DELETE FROM DATATABLE where cmsContentId = %d",notice.cmsContentId];
BOOL res = [db executeUpdate:sql];
if (!res) {
return NO;
} else {
return YES;
}
[db close];
}
return NO;
}
- (NSMutableArray *)queryTable{
NSMutableArray *list = [[NSMutableArray alloc] init];
if ([db open]) {
NSString * sql = @"SELECT * FROM DATATABLE";
FMResultSet * rs = [db executeQuery:sql];
while ([rs next]) {
TableData * notice = [[TableData alloc] init];
notice.cmsColumnName = [rs stringForColumn:@"cmsColumnName"];
notice.cmsColumnId = [rs stringForColumn:@"cmsColumnId"];
notice.cmsContentId = [rs intForColumn:@"cmsContentId"];
notice.createDate = [rs stringForColumn:@"createDate"];
notice.desc = [rs stringForColumn:@"desc"];
notice.gxbArticleId = [rs intForColumn:@"gxbArticleId"];
notice.gxbVideoId = [rs intForColumn:@"gxbVideoId"];
notice.img = [rs stringForColumn:@"img"];
notice.pageUrl = [rs stringForColumn:@"pageUrl"];
notice.picType = [rs stringForColumn:@"picType"];
notice.playType = [rs stringForColumn:@"playType"];
notice.uuid = [rs stringForColumn:@"uuid"];
notice.publishDate = [rs stringForColumn:@"publishDate"];
notice.subTitle = [rs stringForColumn:@"subTitle"];
notice.title = [rs stringForColumn:@"title"];
notice.type = [rs stringForColumn:@"type"];
notice.uploadVideoUrl= [rs stringForColumn:@"uploadVideoUrl"];
notice.videoUrl = [rs stringForColumn:@"videoUrl"];
notice.vipFlag = [rs intForColumn:@"vipFlag"];
[list addObject:notice];
}
[db close];
}
return list;
}
@end