该库提供了一种简便的方法来访问 sqlite 数据库。
此项目需要
iOS5
ARC
要使用 CocoaPods 将 STDbKit 集成到您的 Xcode 项目中,请在 Podfile 中指定它
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
target 'TargetName' do
pod 'STDbKit', '~> 2.3.0'
end
然后,运行以下命令
$ pod install
#import "STDbObject.h"
#import "STDbQueue.h"
#import "STDb.h"
@interface User : STDbObject
@property (strong, nonatomic) NSString *name;
@property (assign, nonatomic) NSInteger age;
@property (strong, nonatomic) NSNumber *sex;
@property (assign, nonatomic) NSTimeInterval time;
@property (assign, nonatomic) int _id;
@end
1:
STDbQueue *dbQueue = [STDbQueue dbWithPath:@"stdb_test/test_queue.sqlite"];
[dbQueue execute:^(STDb *db) {
User *user = [[User alloc] initWithPrimaryValue:8];
user.name = @"yls";
[db insertDbObject:user];
}];
2:
STDbQueue *dbQueue = [STDbQueue dbWithPath:@"stdb_test/test_queue.sqlite"];
[dbQueue execute:^(STDb *db) {
User *user = [[User alloc] initWithPrimaryValue:8];
user.name = @"yls";
[user insertToDb:db];
}];
3:
STDbQueue *dbQueue = [STDbQueue dbWithPath:@"stdb_test/test_queue.sqlite"];
[dbQueue execute:^(STDb *db) {
[db executeUpdate:@"insert into User(?) values(?)" dictionaryArgs:@{@"name" : @"aaa"}];
}];
// query
1:
NSArray *users = [User allDbObjects];
2:
[dbQueue execute:^(STDb *db) {
[db executeQuery:@"select * from User" resultBlock:^(NSArray *resultArray) {
NSLog(@"%@", resultArray);
}];
}];
// query objects on condition
NSArray *users = [User dbObjectsWhere:@"_id=11" orderby:nil];
// first, query the object
1:
NSArray *users = [User dbObjectsWhere:@"_id=11" orderby:nil];
if ([users count] > 0) {
User *user = users[0];
user.name = @"stlwtr";
// 更新到数据库
[user updateToDb];
}
2:
[dbQueue execute:^(STDb *db) {
[user updateToDb:db];
}];
// get the objects to remove
1:
User *user = _users[row];
// delete the object
[user removeFromDb];
2:
[dbQueue execute:^(STDb *db) {
[db executeQuery:@"delete from User where __id__=8"];
}];
}];
// delete the objects on condition
[User removeDbObjectsWhere:@"_id=%d", 4];
2.3.0:
2.2.5:
V2.2.4
V2.2.1
V2.0.2
V2.0
V1.0.5
V1.0.4
V1.0.3
支持集合类,例如 NSData、NSDate、NSArray、NSDictionary
FAQ
----------------
No question asked so far.
Licence
----------------
MIT Licence
Copyright (c) 2014 Thibault Carpentier <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
#STDbKit
## 1. 概述
对于小型数据非常方便,声明一个继承自 STDbObject 的类对象:_dbQueue = [STDbQueue dbWithPath:@"stdb_test/test_encrypt_queue.sqlite"];
写入到数据库直接执行方法 [user insertToDb]; 从数据库读取,NSArray *users = [User dbObjectsWhere:@"_id=11" orderBy:nil]; 更新到数据库,[user updateToDb]; 从数据库删除,[user removeFromDb];
## 2. 更新历史
2.2.5 更新内容(2016-08-08)
- 支持数据库加密
// 数据库 db 文件加密 [_dbQueue execute:^(STDb *db) { db.encryptDB = YES; }];
2.2.4 更新内容(2016-01-27)
- 支持事务处理
- 优化性能
2.2.1 更新内容(2015-06-15)
- 添加多线程支持
- 多db文件支持
- 增加了数据库更新功能, + (NSInteger)dbVersion;
2.0.2 更新内容(2014-02-18)
- add objc to dictionary method
- add dictionary to objc method
2.0 更新内容(2014-01-06)
- support array contain objects of STDbObject class
- support dictionary contain an STDbObject object
- remove the sub STDbObject after remove the parent object
1.0.5 更新内容(2014-01-04)
- 支持一个STDbObject对象包含另一个STDbObject对象了
1.0.4 更新内容(2014-01-02)
- 添加dbObject过期属性,当数据过期,数据会被自动删除,可用于有时间限制的历史纪录等场景添加了数据库加密功能(目前仅支持字符串加密)
1.0.3 更新内容(2013-06-01)
- 支持复杂类型NSData,NSDate,NSArray,NSDictionary
## 3. 使用方法
方法一:导入源码 方法二:项目支持 Cocoapods,在 Podfile 中添加 pod STDbKit 方法三:制作 STDbKit.framework 并引入,可从附件中下载
支持模拟器和真机 STDbKit.framework 制作方法
## 4. 示例
##### 1. 声明一个类,这里新建类User
#import "STDbObject.h" #import "STDbQueue.h" #import "STDb.h"
@interface User : STDbObject
@property (strong, nonatomic) NSString *name; @property (assign, nonatomic) NSInteger age; @property (strong, nonatomic) NSNumber *sex; @property (assign, nonatomic) NSTimeInterval time; @property (assign, nonatomic) int _id;
@end
##### 2. 插入到数据库
方法一: STDbQueue *dbQueue = [STDbQueue dbWithPath:@"stdb_test/test_queue.sqlite"]; [dbQueue execute:^(STDb *db) { User *user = [[User alloc] initWithPrimaryValue:8]; user.name = @"yls"; [db insertDbObject:user]; }]; 方法二: STDbQueue *dbQueue = [STDbQueue dbWithPath:@"stdb_test/test_queue.sqlite"]; [dbQueue execute:^(STDb *db) { User *user = [[User alloc] initWithPrimaryValue:8]; user.name = @"yls"; [user insertToDb:db]; }]; 方法三: STDbQueue *dbQueue = [STDbQueue dbWithPath:@"stdb_test/test_queue.sqlite"]; [dbQueue execute:^(STDb *db) { [db executeUpdate:@"insert into User(?) values(?)" dictionaryArgs:@{@"name" : @"aaa"}]; }];
##### 3. 查询
// 获取所有用户 方法一: NSArray *users = [User allDbObjects]; 方法二: [dbQueue execute:^(STDb *db) { [db executeQuery:@"select * from User" resultBlock:^(NSArray *resultArray) { NSLog(@"%@", resultArray); }]; }];
// 条件获取数据 NSArray *users = [User dbObjectsWhere:@"_id=11" orderby:nil];
##### 4. 修改
// 首先从数据库中取出要修改的对象 方法一: NSArray *users = [User dbObjectsWhere:@"_id=11" orderby:nil]; if ([users count] > 0) { User *user = users[0]; user.name = @"学长"; // 更新到数据库 [user updateToDb]; } 方法二: [dbQueue execute:^(STDb *db) { [user updateToDb:db]; }];
##### 5. 删除
// 要删除的数据 方法一: User *user = _users[row]; // 从数据库中删除数据 [user removeFromDb]; 方法二: [dbQueue execute:^(STDb *db) { [db executeQuery:@"delete from User where id=8"]; }]; }]; // 批量删除 [User removeDbObjectsWhere:@"_id=%d", 4];
FAQ
----------------
No question asked so far.
Requirements
----------------
This project require :
+ ```iOS5```
+ ```ARC```
Licence
----------------
MIT Licence
Copyright (c) 2014 Thibault Carpentier <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.