最近在做一個(gè)項(xiàng)目需要用到持久性存儲(chǔ),在Core Data以及FMDB中選擇用哪個(gè)比較好,最后使用了FMDB,主要是因?yàn)樗呀?jīng)將SQLite都封裝好了,使用起來比較簡(jiǎn)單,容易上手,不得不說老外真的很牛!??
FMDB GitHub下載地址
使用方法主要還是要看文檔。
首先當(dāng)然就是倒入FMDB文件夾到我們自己的項(xiàng)目里,這樣才能使用到它。
這里主要還是記錄一些簡(jiǎn)單的使用方法
- 創(chuàng)建數(shù)據(jù)庫(kù):
- (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(@"創(chuàng)建表成功");
}else {
NSLog(@"創(chuàng)建表失敗");
}
[self closeDateBase];
}
FMDB中打開關(guān)閉的方法是
[self.db open];
[self.db close];
這里我自己寫了打開和關(guān)閉數(shù)據(jù)庫(kù)的方法,主要是加了一個(gè)判斷:
- (void)openDateBase {
if (self.db != nil) {
[self.db open];
}
}
- (void)closeDateBase {
if (self.db != nil) {
[self.db close];
}
}
- 保存數(shù)據(jù)到數(shù)據(jù)庫(kù)
這里用到了簡(jiǎn)單的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];
}
- 當(dāng)然還有查詢,F(xiàn)MDB中提供了一個(gè)[r next]的遍歷方法,只要存在下一個(gè)數(shù)據(jù)就會(huì)繼續(xù)
- (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(@"數(shù)據(jù):%ld,%@,%ld,%@,%.2f,%.2f,%.2f,%ld,%@,%@", _userId, _userDate, _userMileage, _userOilNumber, _userMoney, _userOilPrice, _userLitre, _userPercentage, _userPlace, _userOther);
}
[self closeDateBase];
}
查詢中用到了FMResultSet,當(dāng)查詢成功后會(huì)返回FMResultSet值,同時(shí)FMResultSet 提供了很多方便的方法來查詢數(shù)據(jù):
intForColumn:
longForColumn:
longLongIntForColumn:
boolForColumn:
doubleForColumn:
stringForColumn:
dateForColumn:
dataForColumn:
dataNoCopyForColumn:
UTF8StringForColumn:
objectForColumn:
我的項(xiàng)目里還用到了計(jì)算數(shù)據(jù)庫(kù)里有多少行數(shù)
//計(jì)算數(shù)據(jù)庫(kù)行數(shù)保存到_count中
- (void)countsFromDataBase {
[self openDateBase];
_count = [self.db intForQuery:@"select count(*) from UserTable"];
NSLog(@"該表count = %lu", _count);
[self closeDateBase];
}
這些都是我實(shí)際中用到的一些東西,當(dāng)然不是很全面。