GXDatabaseUtils 1.0

GXDatabaseUtils 1.0

测试已测试
Lang语言 Obj-CObjective C
许可 BSD
发布最后发布2016年4月

高晓祥维护。



  • 作者:
  • Gerry1218
  • 简化 sqlite 数据库 CRUD 操作。
  • 支持 ARC。
  • 支持 ios8 及之前。

Screen shot

如何开始

  • 将 src 和 catagory 目录下的文件复制到您的项目中
  • 下载 fmdb 相关文件
  • 将 libsqlite3.dylib 添加到项目中

支持数据类型

  • BOOL
  • unsigned int
  • NSInteger
  • long long
  • CGFloat
  • double
  • NSString
  • ios8 中的 BOOL
  • ios8 中的 Enum

支持 iOS 版本

iOS5 及以后

类成员和列名的关联

规则:列名是类的成员名称。

例如

// class
@interface GXBaseMessage : NSObject {

    NSString *address;
}

@property (nonatomic, assign) NSInteger count;
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSMutableArray *datas;
@end
成员名称 列名
address address
count _count
name _name
datas -

依赖关系

  • 需要 fmdb
  • NSObject 子类

架构

  • <catagory>

    • NSObject+serializable
  • <src>

    • GXDatabaseManager
    • GXSQLStatementManager
    • GXCache

使用方法

CRUD 操作

  • C-创建
  • R-检索
  • U-更新
  • D-删除

创建

NSString *dbPath = [NSHomeDirectory() stringByAppendingPathComponent:@"testDB.sqlite"];
BOOL res = [GXDatabaseManager databaseWithPath:dbPath];
res = [GXDatabaseManager createTable:@"t_message" withClass:[GXMessage class] withPrimaryKey:@"_msgId"];

检索

NSString *dbPath = [[NSBundle mainBundle] pathForResource:@"db" ofType:@"sqlite"];
BOOL res = [GXDatabaseManager databaseWithPath:dbPath];
if (!res) {
    NSLog(@"ERROR...");
}

NSString *w1 = kWhereString(@"_sessionUserid", @"=");
NSString *w2 = kWhereString(@"_msgId", @"=");
NSString *w = kWhereCombination(w1, @"AND", w2);

[GXDatabaseManager selectObjects:[GXMessageDBEntity class]
                       fromTable:@"t_message_chat"
                           where:w
                      withParams:@[@"1525851662", @"615734ef-2db1-427a-9505-b49ec6a8628c"]
                         orderBy:@"_msgTime"
                    withSortType:@"DESC"
                       withRange:NSMakeRange(0, 5)];

更新

// replace into
NSString *dbPath = [NSHomeDirectory() stringByAppendingPathComponent:@"testDB.sqlite"];
BOOL res = [GXDatabaseManager databaseWithPath:dbPath];

// create table
res = [GXDatabaseManager createTable:@"t_message" withClass:[GXMessage class] withPrimaryKey:@"_msgId"];

[GXDatabaseManager replaceInto:@"t_message" withObject:msg];
// update
NSString *dbPath = [NSHomeDirectory() stringByAppendingPathComponent:@"testDB.sqlite"];
BOOL res = [GXDatabaseManager databaseWithPath:dbPath];

if (!res) NSLog(@"ERROR");


NSString *w = kWhereString(@"_msgId", @"=");
[GXDatabaseManager updateTable:@"t_message"
                           set:@[@"_fontName"]
                         where:w
                    withParams:@[@"黑体", @"1ccaf308-8bb0-1e44-2f0b-98f308d03d57"]];

删除

NSString *dbPath = [[NSBundle mainBundle] pathForResource:@"db" ofType:@"sqlite"];
BOOL res = [GXDatabaseManager databaseWithPath:dbPath];
if (!res) NSLog(@"ERROR...");

[GXDatabaseManager deleteTable:@"t_message_chat" where:@"_msgId=?" withParams:@[@"1ccaf308-8bb0-1e44-2f0b-98f308d03d57"]];

许可

GXDatabaseUtils 在 MIT 许可下可用。有关更多信息,请参阅 LICENSE 文件。