Fork me on GitHub

CCSQLite Objective-C 封装的SQLite. 结合YapDatabase(key/value store) + FMDB

CCSQLite Objective-C 封装的SQLite. 结合YapDatabase(key/value store) + FMDB.


目的

  • 转变 FMDB 到 ARC 模式
  • YapDatabase 键/值 store
  • CCSQLite 结合 FMDB 和 YapDatabase 键/值

目的

  • 转变 FMDB 到 ARC 模式
  • YapDatabase 键/值 store
  • CCSQLite 结合 FMDB 和 YapDatabase 键/值
  • 支持 OBJECT, JSON 到 键/值


缘由 && 为啥


当我在写”HSCache [缓存处理方面]”, 想选择FMDB或者YapDatabase, 但是他们看起来不一样. 因此想结合YapDatabase(键/值 store) 与 FMDB, 于是 CCSQLite 在路上.

大于版本 1.1.1调整变化


CCSQLite CCSQLiteMac Screenshot

Podfile

1
pod 'CCSQLite'

演示

for Objective-C

see CCSQLiteDemo
iOS CCSQLiteiOSDemo
OSX CCSQLiteOSXDemo

for Swift call Objective-C

iOS CCSQLiteDataiOS
OSX CCSQLiteDatamacOS

演示代码

1
#import <CCSQLite.h>

or

1
#import "CCSQLite.h"

or

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
#import "CCSQLite/CCSQLite.h"
+ (void) SQLiteTest {
NSLog(@"SQLiteTest");

NSString *path = nil;
#if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) lastObject];
#else
path = NSTemporaryDirectory() ;
#endif

path = [path stringByAppendingPathComponent:CCSQLiteTestDB];

if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
[[NSFileManager defaultManager] removeItemAtPath:path error: nil];
}

CCSQLite *SQLite = [CCSQLite databaseWithPath: path];

if ([SQLite open]) {
BOOL result = [SQLite executeUpdate: @"create table if not exists t_student (id integer primary key autoincrement, name text not NULL, age integer not NULL);"];
if (result) {
NSLog(@"create table t_student ok");
NSLog(@"path : %@", path);
}
}

[SQLite executeUpdate:@"insert into t_student (name, age) values (?, ?);", @"cc test 0", @0];
[SQLite executeUpdate:@"insert into t_student (name, age) values (?, ?);", @"cc test 1", @1];
[SQLite executeUpdateWithFormat:@"insert into t_student (name, age) values (%@, %i);", @"cc test 2", 2000];

// [SQLite executeUpdate:@"delete from t_student where id = ?", @1];

CCResultSet *resultSet = [SQLite executeQuery:@"select * from t_student;"];
while ([resultSet next]) {
int idNum = [resultSet intForColumn:@"id"];
NSString *name = [resultSet objectForColumnName:@"name"];
int age = [resultSet intForColumn:@"age"];

NSLog(@"id = %d name = %@ age = %d", idNum, name, age);
}

// [SQLite executeUpdate:@"drop table if exists t_student;"];

NSString *sql = @"create table bulktest1 (id integer primary key autoincrement, x text);"
"create table bulktest2 (id integer primary key autoincrement, y text);"
"create table bulktest3 (id integer primary key autoincrement, z text);"
"insert into bulktest1 (x) values ('XXX');"
"insert into bulktest2 (y) values ('YYY');"
"insert into bulktest3 (z) values ('ZZZ');";

BOOL success = [SQLite executeStatements:sql];

if (success) {
NSLog(@"success");
}

sql = @"select count(*) as count from bulktest1;"
"select count(*) as count from bulktest2;"
"select count(*) as count from bulktest3;";

[SQLite executeStatements:sql withResultBlock:^int(NSDictionary *resultsDictionary) {
NSInteger count = [resultsDictionary[@"count"] integerValue];
NSLog(@"count = %ld", count);
return 0;
}];

[SQLite close];

CCSQLiteQueue *queue = [CCSQLiteQueue databaseQueueWithPath:path];

__block NSInteger index = 3000;
[queue inDatabase:^(CCSQLite *db) {
while (index < 3100) {
index++;
[db executeUpdate:@"insert into t_student (name, age) values (?, ?);", [NSString stringWithFormat:@"cc test inDatabase %ld", index], @(index)];
}

}];

[queue inTransaction:^(CCSQLite *db, BOOL *rollback) {
NSLog(@"rollback NO");
while (index < 3150) {
index++;
[db executeUpdate:@"insert into t_student (name, age) values (?, ?);", [NSString stringWithFormat:@"cc test inTransaction %ld", index], @(index)];
}
}];

[queue inTransaction:^(CCSQLite *db, BOOL *rollback) {
NSLog(@"rollback YES");
while (index < 3200) {
index++;
[db executeUpdate:@"insert into t_student (name, age) values (?, ?);", [NSString stringWithFormat:@"cc test inTransaction %ld", index], @(index)];

if (index == 3188) {
*rollback = YES;
return ;
}
}
}];
}

CCKeyValue 演示代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
CCKeyValue *kv = [CCKeyValue defaultKeyValueWithPath:path];
kv.valueType = CCKeyValueTypeJson;

NSData * data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"CCJSON" ofType:@"json"]];

[kv setObject:data key:@"jsonkey"];

id CCJSON = [kv objectForKey:@"jsonkey"];

if ([CCJSON isKindOfClass:[NSArray class]]) {
NSArray *list = CCJSON;

[list enumerateObjectsUsingBlock:^(NSDictionary *d, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"%@\n", d);
}];
}

结果 CCKeyValue Screenshot

Swift 调用 Objective-C

1
2
3
4
5
6
7
8
9
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

CCSQLiteData.writeList()
if let list = CCSQLiteData.readList() {
print(list)
}
}

###