-
Notifications
You must be signed in to change notification settings - Fork 1
/
WeiboDataBase.m
executable file
·142 lines (95 loc) · 4.5 KB
/
WeiboDataBase.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
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
//
// WeiboDataBase.m
// Weibo_Test-0001
//
// Created by 1007 on 13-11-29.
// Copyright (c) 2013年 Ibokan. All rights reserved.
//
#import "WeiboDataBase.h"
#import "WeiBoContext.h"
@implementation WeiboDataBase
#pragma mark -- 通过数据库来存取数据
+ (NSString *)getPathDB
{
NSLog(@"%s", __func__);
NSArray *pathArr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[pathArr lastObject] stringByAppendingPathComponent:@"WeiBoDB.sqlite"];
return path;
}
//???: 创建表
+ (BOOL)createWeiboTable
{
NSLog(@"%s", __func__);
#pragma mark -- 使用FMDB第三方类库来实现数据的打开
FMDatabase *db = [FMDatabase databaseWithPath:[WeiboDataBase getPathDB]];
if (![db open]) {
NSLog(@"打开数据库失败!");
return NO;
}
#pragma mark -- 使用FMDB第三方类库来实现数据的创建
BOOL isSucess = [db executeUpdate:@"create table if not exists WeiboDataBases(id integer primary key autoincrement, mid integer, idstr text, text text, source text, favorited bool, truncated bool, thumbnail_pic blob, bmiddle_pic blob, original_pic blob, retweeted_status_id text, reposts_count integer, comments_count integer, attitudes_count integer)"];
if (isSucess) {
NSLog(@"表创建成功!");
}
[db close];
return isSucess;
}
+ (BOOL)addWithWeibo:(WeiBoContext *)weibo
{
NSLog(@"%s", __func__);
FMDatabase *db = [FMDatabase databaseWithPath:[WeiboDataBase getPathDB]];
if (![db open]) {
NSLog(@"数据库打开失败!");
return NO;
}
BOOL isSucess = [db executeUpdate:@"insert into WeiboDataBases(id, mid, idstr, text, source, favorited, truncated, retweeted_status_id, reposts_count, comments_count, attitudes_count) values(?,?,?,?,?,?,?,?,?,?,?)",[NSNumber numberWithInteger:weibo.ID], [NSNumber numberWithInteger:weibo.MID], weibo.idstr, weibo.text, weibo.source, [NSNumber numberWithBool:weibo.favorited], [NSNumber numberWithBool:weibo.truncated], weibo.retweeted_status, [NSNumber numberWithInteger:weibo.reposts_count], [NSNumber numberWithInteger:weibo.comments_count], [NSNumber numberWithInteger:weibo.attitudes_count]];
[db close];
return isSucess;
}
+ (BOOL)updateWithWeibo:(WeiBoContext *)weibo andWeiboDBID:(NSInteger)ID
{
NSLog(@"%s", __func__);
FMDatabase *db = [FMDatabase databaseWithPath:[WeiboDataBase getPathDB]];
if (![db open]) {
NSLog(@"数据库打开失败!");
return NO;
}
BOOL isSucess = [db executeUpdate:@"update WeiboDataBases set id = ?, mid = ?, idstr = ?, text = ?, source = ?, favorited = ?, truncated = ?, retweeted_status_id = ?, reposts_count = ?, comments_count = ?, attitudes_count = ? where id = ?", [NSNumber numberWithInteger:weibo.ID], [NSNumber numberWithInteger:weibo.MID], weibo.idstr, weibo.text, weibo.source, [NSNumber numberWithBool:weibo.favorited], [NSNumber numberWithBool:weibo.truncated], weibo.retweeted_status, [NSNumber numberWithInteger:weibo.reposts_count], [NSNumber numberWithInteger:weibo.comments_count], [NSNumber numberWithInteger:weibo.attitudes_count], [NSNumber numberWithInteger:ID]];
[db close];
return isSucess;
}
+ (NSMutableArray *)findAll
{
NSLog(@"%s", __func__);
__autoreleasing NSMutableArray *array = [NSMutableArray new];
FMDatabase *db = [FMDatabase databaseWithPath:[WeiboDataBase getPathDB]];
if (![db open]) {
NSLog(@"数据库打开失败!");
return NO;
}
FMResultSet *rs = [db executeQuery:@"select * from WeiboDataBases"];
while ([rs next]) {
NSDictionary *dic = [rs resultDictionary];
NSLog(@"----------------------- DB Dic-----------------------%@", dic);
WeiBoContext *weibo = [[WeiBoContext alloc] initWithWeibo:dic];
[array addObject:weibo];
}
NSLog(@"----------------------- DB -----------------------%@", array);
[db close];
return array;
}
+ (BOOL)deleteDataByID:(int)aID
{
NSLog(@"%s", __func__);
FMDatabase *db = [FMDatabase databaseWithPath:[WeiboDataBase getPathDB]];
if (![db open]) {
NSLog(@"数据库打开失败!");
}
BOOL isSucess = [db executeUpdate:@"delete from WeiboDataBases where id = ?", [NSNumber numberWithInt:aID]];
if (isSucess) {
NSLog(@"删除成功!");
}
[db close];
return YES;
}
@end