KKDSqlite帮助您管理iOS应用程序上的sqlite数据库。
KKDSqlite可通过CocoaPods获取。要安装
它,只需将以下行添加到您的Podfile中
pod 'KKDSqlite'
为您的项目创建数据库。此库使用预创建的sqlite数据库。您可以使用SQLiteStudio或类似程序创建sqlite数据库。
在演示项目中,您将看到sqlitedatabse(demoDB.sqlite),其中数据库中有两个表。
CREATE TABLE Countries (
id PRIMARY KEY,
name VARCHAR (50)
);
CREATE TABLE Cities (
id PRIMARY KEY,
countryId INTEGER,
name VARCHAR (50)
);
创建模型。在创建模型时,表中列的名称和类中属性的名称应相同。
--- .h file
#import "SqliteBaseData.h"
@interface Country : SqliteBaseData
@property (nonatomic) NSString* name;
@end
--- .m file
#import "Country.h"
@implementation Country
//this method should be overriden
+(NSString *)tableName{
return @"Countries";
}
@end
--- .h file
#import "SqliteBaseData.h"
@interface City : SqliteBaseData
@property (nonatomic) NSString* name;
@property (nonatomic) int countryId;
@end
--- .m file
#import "City.h"
@implementation City
+(NSString *)tableName{
return @"Cities";
}
//this method is important while saving country and it's cities in one transaction.
//For foreign keys, you need to set value in this method.
-(void)bindToParent:(SqliteBaseData *)parent{
_countryId = parent.id;
}
@end
在执行任何命令之前,您应该使用数据库名称配置sqliteManager。建议在appDelegate中这样做。
#import "SqliteManager.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//in the sample project, the name of the database file is demoDB.sqlite
[SqliteManager configure:@"demoDB.sqlite"];
return YES;
}
要从sqlite数据库获取数据,您可以使用这些方法;
+(NSMutableArray <SqliteBaseData*>*) modelListFromDB:(NSDictionary *) params;
+(NSMutableArray <SqliteBaseData*>*) modelListFromDB:(NSDictionary *) params orderBy:(NSString*) orderBy;
+(NSMutableArray <SqliteBaseData*>*) modelListWithCommand:(NSString *) command;
如果您想从sqlite数据库中选择所有国家,您只需要做
NSMutableArray *countryList = [Country modelListFromDB:nil];
如果您想对结果进行排序
//do not forget to add "order by"
//you can add more columns, for example "order by firstName desc, lastName asc"
NSMutableArray *countryList = [Country modelListFromDB:nil orderBy:@"order by name"];
如果您想通过过滤countryId = 1选择城市,您只需要做
//you can add more parameters, keys values of dictionary should be one of the column names in cities table
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:@(1), @"countryId", nil];
NSMutableArray *cityList = [City modelListFromDB:params];
如果您想执行自己的命令
//You should select cities.*, because instance list of cityModel will be created by result
NSMutableArray *cityList = [City modelListWithCommand:@"SELECT cities.* FROM cities INNER JOIN countries ..."];
回调在主线程中执行。
+(void) modelListFromDB:(NSDictionary *) params success:(void(^)(NSMutableArray <SqliteBaseData*>*))callback;
+(void) modelListFromDB:(NSDictionary *) params orderBy:(NSString*) orderBy success:(void(^)(NSMutableArray <SqliteBaseData*>*))callback;
+(void) modelListWithCommand:(NSString *) command success:(void(^)(NSMutableArray <SqliteBaseData*>*))callback;
如果您想通过回调从sqlite数据库中选择所有国家
[Country modelListFromDB:nil
success:^(NSMutableArray<SqliteBaseData *> *list) {
_countryList = list;
[_table reloadData];
}
];
如果您想对结果进行排序
[Country modelListFromDB:nil
orderBy:@"order by name"
success:^(NSMutableArray<SqliteBaseData *> *list) {
_countryList = list;
[_table reloadData];
}
];
如果您想通过过滤countryId = 1选择城市,您只需要做
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:@(1), @"countryId", nil];
[City modelListFromDB:params
success:^(NSMutableArray<SqliteBaseData *> *list) {
_cityList = list;
[_table reloadData];
}
];
如果您想执行自己的命令
//You should select cities.*, because instance list of cityModel will be created by result
[City modelListWithCommand:@"SELECT cities.* FROM cities INNER JOIN countries ..."
success:^(NSMutableArray<SqliteBaseData *> *list) {
_cityList = list;
[_table reloadData];
}
];
如果您有一个继承自SqliteBaseData的模型,您只需调用saveMe方法即可创建或更新。
如果实例的id为0,将生成创建方法并执行。
如果id不为0,将生成更新方法并执行。
City *city = [City new];
city.name = @"Paris";
[city saveMe];
//as city id is 0, a new row would be added to cities table.
//After inserting, id of this instance will automatically set.
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:@(1), @"id", nil];
NSMutableArray *cityList = [City modelListFromDB:params];
City *city = [cityList objectAtIndex:0];
city.name = @"Paris";
[city saveMe];
//as city id is not 0, the related row will be updated.
如果您有一个继承自SqliteBaseData的模型,您只需调用deleteMe方法即可删除。
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:@(1), @"id", nil];
NSMutableArray *cityList = [City modelListFromDB:params];
City *city = [cityList objectAtIndex:0];
[city deleteMe];
根据您的目的,您可以使用以下方法之一;
-(BOOL)saveMeWith:(NSArray <SqliteBaseData*>*) dataList;
-(BOOL)saveMeWithChildren:(NSArray <SqliteBaseData*>*) dataList;
-(BOOL)deleteMeWith:(NSArray <SqliteBaseData*>*) dataList;
如果您使用saveMeWithChildren方法;
在保存(创建或更新)父实例后,对于dataList中的每个实例都会调用bindToParent方法。
//in City.m file
-(void)bindToParent:(SqliteBaseData *)parent{
_countryId = parent.id;
}
Country *country = [[Country alloc] initWithName:@"A Name"];
NSMutableArray<SqliteBaseData *> *cityList = [NSMutableArray new];
[cityList addObject:[[City alloc] initWithName:@"city 1"];
[cityList addObject:[[City alloc] initWithName:@"city 2"];
[cityList addObject:[[City alloc] initWithName:@"city 3"];
[cityList addObject:[[City alloc] initWithName:@"city 4"];
[cityList addObject:[[City alloc] initWithName:@"city 5"];
[country saveMeWithChildren:cityList];
//after country is saved, id value of the country will be set.
//before saving cityList, bindToParent method will be called for every instances
//and for this example the countryId of city instances will be set
//If saveMeWith method is used, bindToParent method will not be called
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:@(1), @"id", nil];
NSMutableArray *countryList = [Country modelListFromDB:params];
Country *country = [countryList objectAtIndex:1];
params = [NSDictionary dictionaryWithObjectsAndKeys:@(country.id), @"countryId", nil];
NSMutableArray *cityList = [City modelListFromDB:params];
//to delete all items in one transaction
[country deleteMeWith:cityList];
Kadir Kemal Dursun (https://github.com/KadirKemal)
KKDSqlite在MIT许可证下可用。有关更多信息,请参阅LICENSE文件。