iOS sqlite控件 Sqlight

记:上一篇文章介绍了Kache这个缓存控件,可以用来配合这次iCloud升级,这篇是关于一个sqlite的封装,Sqlight,一个很轻量级的封装,也是为了配合iCloud。模仿了PHP mysql扩展PDO的一些做法。把数据库操作封装成方法,通过NSDictionary和NSArray传递参数。源码:http://jiajun.org/g/Sqlight.zip

Sqlight真是没什么可介绍的,主要的常用方法有4个,select, update, insert和delete,还有一个create table在初始化时调用。用法示例在 Sqlite.h 文件的注释中。

简单介绍一下原理,Sqlight的数据文件是放在Documentation目录的,也就是说会被备份到iCloud。跟cache不同,我建议在数据库里存放一些比如,用户信息,登录状态等等。

以下把用法示例贴一下:

 

SqlightAdapter *sqlight = [[Sqlight alloc] initWithDatabase:@"test_database" AndTable:@"test_tb"];

if (nil == sqlight) {

sqlight = [[Sqlight alloc] initWithDatabase:@"test_database"];

 

[sqlight createTable:@"test_tb" Info:[NSArray arrayWithObjects:@"f1", @"f2", nil]];

sqlight.tableName = @"test_tb";

}

[sqlight insertData:[NSDictionary dictionaryWithObjectsAndKeys:

@"value1",@"f1",

@"value2",@"f2",

nil]];

// this is a wrong condition, there is no f0 field.

sqlight_result_t res = [sqlight selectFields:[NSArray arrayWithObjects:@"f1, f2", nil]

ByCondition:@"f0=?"

  Bind:[NSArray arrayWithObjects:@"value1", nil]]; 

NSLog(@"%d -- %@ -- %@", res.code, res.msg, res.data);// will out put 1 -- no such column: f0 -- ()

res = [sqlight selectFields:[NSArray arrayWithObjects:@"f1, f2", nil]

ByCondition:@"f1=?"

Bind:[NSArray arrayWithObjects:@"value1", nil]]; 

NSLog(@"%d -- %@", res.code, res.data);// will out put 101 -- ( value1, value2 )

[sqlight updateData:[NSDictionary dictionaryWithObjectsAndKeys:@"value_new", @"f1", nil]

ByCondition:@"f1=?"

  Bind:[NSArray arrayWithObjects:@"value1", nil]];

res = [sqlight selectFields:[NSArray arrayWithObjects:@"f1, f2", nil]

ByCondition:@"f1=?"

Bind:[NSArray arrayWithObjects:@"value1", nil]]; 

NSLog(@"%d -- %@", res.code, res.data); // will out put 101 -- ()


 

标签:ios, 缓存, kache, icloud, sqlite

添加新评论