STDbKit 2.3.0

STDbKit 2.3.0

测试已测试
语言语言 Obj-CObjective C
许可证 MIT
发布最新发布2017年7月

stlwtryanglishuan维护。



STDbKit 2.3.0

  • 作者
  • stlwtr

该库提供了一种简便的方法来访问 sqlite 数据库。

需求

此项目需要

  • iOS5
  • ARC

Podfile

要使用 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

使用说明

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. 插入到数据库
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"}];
}];

3. 查询
// 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];

4. 更新
// 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];
}];
5. 删除
// 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.3.0:

    • 支持 STIgnore、STDbPrimaryKey
  • 2.2.5:

    • 支持 .sqlite 文件加密
    • 完成 Readme.md 文档
  • V2.2.4

    • 支持事务管理
  • V2.2.1

    • 支持线程管理
  • V2.0.2

    • 添加将类对象转换为字典的方法
    • 添加将字典转换为类对象的方法
  • V2.0

    • 支持包含 STDbObject 类对象的数组
    • 支持包含 STDbObject 对象的字典
    • 在删除父对象后,删除关联的 STDbObject 类对象
  • V1.0.5

    • 支持 STDbObject 类对象包含另一个 STDbObject 类对象
  • V1.0.4

    • 添加 dbObject expireDate 属性,当数据过期时,它将自动从数据库中删除
  • 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 制作方法

  1. 分别在第 device 和模拟器下运行
  2. 右键单击 STDbKit.framework,选择 Show In Finder,找到上级目录,本项目是 release 版本,这里显示 Release-iphoneos,Release-iphonesimulator
  3. 将 Release-iphoneos,Release-iphonesimulator 文件夹复制到桌面
  4. 在终端运行 lipo -create ~/Desktop/Release-iphoneos/STDbKit.framework/STDbKit
    ~/Desktop/Release-iphonesimulator/STDbKit.framework/STDbKit
    -output ~/Desktop/STDbKit
  5. 在桌面文件夹 Release-iphoneos 上,将 STDbKit.framework 复制到桌面,将桌面上的 STDbKit 文件覆盖到 STDbKit.framework 中,制作完成
  6. 在项目中引用STDbKit.framework框架,支持模拟器和真机了
  • 引入依赖库sqlite3.dylib
  • 创建需要保存的数据类,该类需要继承类STDbObject

## 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.