简述:这是一个 iOS 的 Sqlite 数据库的查询组件。比较简洁,而且只关注查询部分。
根据查询时指定要获取的类型得到指定的数据格式,本组件共提供四种结果类型。
id | name |
0 | A |
1 | B |
dictionary结果类型
{
id = (0,1);
name = (A,B)
}
array结果类型
(
(0,1),
(A,B)
)
arrayDictionary结果类型
(
{
id = 0;
name = A;
},
{
id = 1;
name = B;
}
)
arrayArray结果类型
(
(0,A),
(1,B)
)
dictionary、array、arrayDictionary、arrayArray 该四种结果类型可以相互转换。
代码如下:
//
// MZCQuery.h
// MZCKit
//
// Created by yococo on 15/11/14.
// Copyright (c) 2015年 yococo. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <sqlite3.h>
#import "MZCQueryResult.h"
@interface MZCQuery : NSObject
@property(nonatomic,readonly) NSString* path;
+ (instancetype) defaultQuery;
+ (instancetype) defaultQueryWithPath:(NSString*) path;
- (instancetype) initWithPath:(NSString*) path;
#pragma mark -
- (MZCQueryResult*) queryResultWithSQl:(NSString*) sql Type:(MZCQueryResultType) type;
/*
type 默认为 MZCQueryResultTypeArrayDictionary.
*/
- (MZCQueryResult*) queryResultWithSQl:(NSString*) sql;
@end
//
// MZCQueryResult.h
// MZCKit
//
// Created by yococo on 15/11/14.
// Copyright (c) 2015年 yococo. All rights reserved.
//
#import <Foundation/Foundation.h>
typedef NS_ENUM(NSUInteger, MZCQueryResultType) {
MZCQueryResultTypeDictionary,
MZCQueryResultTypeArray,
MZCQueryResultTypeArrayDictionary,
MZCQueryResultTypeArrayArray,
};
@interface MZCQueryResult : NSObject
@property(nonatomic,readonly) MZCQueryResultType type;
@property(nonatomic,readonly) NSArray* fields;
@property(nonatomic,readonly) id result;
#pragma mark -
- (instancetype) initWithType:(MZCQueryResultType)type fields:(NSArray*)fields result:(id)result;
#pragma mark - 转换
//将对象 转换成 另一个类型的对象
- (MZCQueryResult*) resultByConvertToType:(MZCQueryResultType) type;
//将对象的结果 转换成 另一个类型的结果
- (id) convertToType:(MZCQueryResultType) type;
@end