最近在做一个项目需要用到持久性存储,在Core Data以及FMDB中选择用哪个比较好,最后使用了FMDB,主要是因为它已经将SQLite都封装好了,使用起来比较简单,容易上手,不得不说老外真的很牛!😄
FMDB GitHub下载地址
使用方法主要还是要看文档。
首先当然就是倒入FMDB文件夹到我们自己的项目里,这样才能使用到它。
这里主要还是记录一些简单的使用方法
- 创建数据库:
- (void)createDateBase {
NSString *documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *fileName = [documents stringByAppendingPathComponent:@"user.sqlite"];
self.db = [FMDatabase databaseWithPath:fileName];
}
文件的保存路径就是/Documents/user.sqlite
- 建表
- (void)createTable {
[self openDateBase];
NSString *sql = @"create table UserTable(uId integer primary key autoincrement, date text, mileage integer, oilNumber text, money float, oilPrice float, litre double, percentage integer, place text, other text);";
BOOL b = [self.db executeUpdate:sql];
if (b) {
NSLog(@"创建表成功");
}else {
NSLog(@"创建表失败");
}
[self closeDateBase];
}
FMDB中打开关闭的方法是
[self.db open];
[self.db close];
这里我自己写了打开和关闭数据库的方法,主要是加了一个判断:
- (void)openDateBase {
if (self.db != nil) {
[self.db open];
}
}
- (void)closeDateBase {
if (self.db != nil) {
[self.db close];
}
}
- 保存数据到数据库
这里用到了简单的SQL语句
- (void)saveUserData {
[self openDateBase];
BOOL b = [self.db executeUpdate:@"insert into UserTable (date, mileage, oilNumber, money, oilPrice, litre, percentage, place, other) values (?, ?, ?, ?, ?, ?, ?, ?, ?);", _userDate, [NSNumber numberWithInteger:_userMileage], _userOilNumber, [NSNumber numberWithFloat:_userMoney], [NSNumber numberWithFloat:_userOilPrice], [NSNumber numberWithFloat:_userLitre], [NSNumber numberWithInteger:_userPercentage], _userPlace, _userOther];
if (b) {
NSLog(@"保存成功");
}else {
NSLog(@"保存失败");
}
[self closeDateBase];
}
- 当然还有查询,FMDB中提供了一个[r next]的遍历方法,只要存在下一个数据就会继续
- (void)queryData {
NSString *sql = @"select uId, date, mileage, oilNumber, money, oilPrice, litre, percentage, place, other from UserTable;";
[self openDateBase];
FMResultSet *r = [self.db executeQuery:sql];
_allDatas = [[NSMutableArray alloc] init];
while ([r next]) {
_userId = [r intForColumn:@"uId"];
_userDate = [r stringForColumn:@"date"];
_userMileage = [r intForColumn:@"mileage"];
_userOilNumber = [r stringForColumn:@"oilNumber"];
_userMoney = [r longForColumn:@"money"];
_userOilPrice = [r longForColumn:@"oilPrice"];
_userLitre = [r longForColumn:@"litre"];
_userPercentage = [r intForColumn:@"percentage"];
_userPlace = [r stringForColumn:@"place"];
_userOther = [r stringForColumn:@"other"];
NSLog(@"数据:%ld,%@,%ld,%@,%.2f,%.2f,%.2f,%ld,%@,%@", _userId, _userDate, _userMileage, _userOilNumber, _userMoney, _userOilPrice, _userLitre, _userPercentage, _userPlace, _userOther);
}
[self closeDateBase];
}
查询中用到了FMResultSet,当查询成功后会返回FMResultSet值,同时FMResultSet 提供了很多方便的方法来查询数据:
intForColumn:
longForColumn:
longLongIntForColumn:
boolForColumn:
doubleForColumn:
stringForColumn:
dateForColumn:
dataForColumn:
dataNoCopyForColumn:
UTF8StringForColumn:
objectForColumn:
我的项目里还用到了计算数据库里有多少行数
//计算数据库行数保存到_count中
- (void)countsFromDataBase {
[self openDateBase];
_count = [self.db intForQuery:@"select count(*) from UserTable"];
NSLog(@"该表count = %lu", _count);
[self closeDateBase];
}
这些都是我实际中用到的一些东西,当然不是很全面。