github上FMDB的repo
简单使用了数据库的操作
数据库管理工具用的免费的Base
如果你对sql语句不太熟悉,参见–点击学习
如果你对FMDB不太熟悉,可以看看唐巧的这篇在iOS开发中使用FMDB
好了,开始了
RecordItem是我的Model
直接上代码吧
#import "RecordItem.h"
#define PATH_OF_DOCUMENT [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]
#define DBPath [PATH_OF_DOCUMENT stringByAppendingPathComponent:@"resource.sqlite"]
@interface Persistent : NSObject
+(void)createTable;
+(void)insertIntoDB:(RecordItem *)record;
/**
* uniqueId --> cmd+param
**/
+(RecordItem *)queryOneRecordFromDB:(NSString *)uniqueId;
+(void)updateRecord:(RecordItem *)item;
+(void)deleteRecord:(RecordItem *)item;
@end
简单的CURD操作咯~~
#import "Persistent.h"
#import "FMDatabaseAdditions.h"
@implementation Persistent
+(void)createTable
{
// NSLog(@"DBPath ------- %@",DBPath);
FMDatabase* db = [FMDatabase databaseWithPath:DBPath];
if ([db open]) {
NSString* sqlString = @"CREATE TABLE IF NOT EXISTS RecordItem('uniqueid' Integer PRIMARY KEY AUTOINCREMENT,'uniquekey' text, 'response' text, 'timestamp' text,'statusCode' text)";
BOOL res = [db executeUpdate:sqlString];
if (!res) {
// NSLog(@"error when creating db table");
}else{
// NSLog(@"success when open db");
}
[db close];
}else
{
NSLog(@"error when open db");
}
}
+(void)insertIntoDB:(RecordItem *)record
{
// NSLog(@"DBPath ------- %@",DBPath);
FMDatabase * db = [FMDatabase databaseWithPath:DBPath];
if ([db open]) {
NSString * sql = @"insert into RecordItem (uniquekey, response,timestamp,statusCode) values(?,?,?,?) ";
BOOL res = [db executeUpdate:sql, record.uniqueKey,record.response,record.timestamp, record.statusCode];
if (!res) {
// NSLog(@"error to insert data");
} else {
// NSLog(@"succ to insert data");
}
[db close];
}
}
+(RecordItem *)queryOneRecordFromDB:(NSString *)uniqueId
{
// NSLog(@"DBPath ------- %@",DBPath);
FMDatabase * db = [FMDatabase databaseWithPath:DBPath];
if ([db open]) {
FMResultSet *rs = [db executeQuery:@"select * from RecordItem where uniquekey = ?",uniqueId];
while ([rs next]) {
RecordItem *item = [RecordItem new];
item.uniqueKey = [rs stringForColumnIndex:1];
item.response = [rs stringForColumnIndex:2];
item.timestamp = [rs stringForColumnIndex:3];
item.statusCode = [rs stringForColumnIndex:4];
// NSLog(@"uniquekey=%@, 'response'=%@, 'timestamp'=%@,'statusCode'=%@ ", item.uniqueKey,item.response, item.timestamp, item.statusCode);
[db close];
return item;
}
[db close];
}
return nil;
}
+(void)updateRecord:(RecordItem *)item
{
FMDatabase * db = [FMDatabase databaseWithPath:DBPath];
if ([db open]) {
NSLog(@"Err %d: %@", [db lastErrorCode], [db lastErrorMessage]);
NSString *sql = [NSString stringWithFormat:@"update RecordItem set response = '%@',timestamp = '%@',statusCode = '%@' where uniquekey = '%@'",item.response,item.timestamp,item.statusCode,item.uniqueKey];
BOOL res = [db executeUpdate:sql];
if (!res) {
NSLog(@"error to update data");
} else {
// NSLog(@"succ to update data");
}
[db close];
}
}
+(void)deleteRecord:(RecordItem *)item
{
FMDatabase * db = [FMDatabase databaseWithPath:DBPath];
if ([db open]) {
NSLog(@"Err %d: %@", [db lastErrorCode], [db lastErrorMessage]);
NSString *sqlString = [NSString stringWithFormat:@"delete from RecordItem where uniquekey = %@",item.uniqueKey];
BOOL res = [db executeUpdate:sqlString];
if (!res) {
NSLog(@"error to insert data");
} else {
// NSLog(@"success to insert data");
}
[db close];
}
}
@end