網(wǎng)上的數(shù)據(jù)封裝類都很復(fù)雜也不太容易修改,自己簡(jiǎn)單封裝了一個(gè)數(shù)據(jù)庫(kù)類
需要用的數(shù)據(jù)庫(kù)是SQLite
話不多說(shuō)上代碼 先是.h文件
#import <Foundation/Foundation.h>
@interface DataBaseHandle : NSObject
+ (instancetype)shareDataBaseHandle;
- (void)open:(NSString *)file;
- (void)closeDB;
- (void)updateSQL:(NSString *)SQL;
- (NSArray *)selectAllWithList:(NSString *)list AndListNumber:(NSInteger)num;
@end
接下來(lái)是.m文件
#import "DataBaseHandle.h"
#import <sqlite3.h>
@implementation DataBaseHandle
/** 創(chuàng)建單例數(shù)據(jù)庫(kù) */
+ (instancetype)shareDataBaseHandle {
static DataBaseHandle *dataBase = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
dataBase = [DataBaseHandle new];
});
return dataBase;
}
sqlite3 *db;
- (void)open:(NSString *)file {
if (db) {
NSLog(@"數(shù)據(jù)庫(kù)已開(kāi)啟!!!");
return;
}
int result = sqlite3_open(file.UTF8String, &db);
if (result == SQLITE_OK) {
NSLog(@"數(shù)據(jù)庫(kù)打開(kāi)成功!");
}else {
NSLog(@"數(shù)據(jù)庫(kù)打開(kāi)失敗!,code = %d", result);
}
}
- (void)closeDB {
int result = sqlite3_close(db);
if (result == SQLITE_OK) {
NSLog(@"數(shù)據(jù)庫(kù)關(guān)閉成功");
db = nil;
}else {
NSLog(@"數(shù)據(jù)庫(kù)關(guān)閉失敗.code:%d", result);
}
}
//增/刪/改
- (void)updateSQL:(NSString *)SQL {
int result = sqlite3_exec(db, SQL.UTF8String, nil, nil, nil);
if (result == SQLITE_OK) {
NSLog(@"成功!");
}else {
NSLog(@"失敗!.code:%d", result);
}
}
- (NSArray *)selectAllWithList:(NSString *)list AndListNumber:(NSInteger)num {
NSMutableArray *arr = [NSMutableArray new];
// SQL語(yǔ)句
NSString *selectSQL = [NSString stringWithFormat:@"SELECT * FROM %@ order by number desc", list];
//聲明一個(gè)stmt對(duì)象(結(jié)構(gòu)體)
sqlite3_stmt *stmt = nil;
int result = sqlite3_prepare_v2(db, selectSQL.UTF8String, -1, &stmt, nil);
if (result == SQLITE_OK) {
// 每行都執(zhí)行查詢語(yǔ)句
while (sqlite3_step(stmt) == SQLITE_ROW) {
//如果查詢條件匹配.通過(guò)sqlite3_column函數(shù)簇 取出值.
NSMutableArray *tempArr = [[NSMutableArray alloc] init];
for (int i = 0 ; i < num; i++) {
const unsigned char *str = sqlite3_column_text(stmt, i);
[tempArr addObject:[NSString stringWithUTF8String:(const char *)str]];
}
[arr addObject:tempArr];
[tempArr release];
}
//銷毀對(duì)象.
sqlite3_finalize(stmt);
}else {
NSLog(@"不能正常查詢.code:%d", result);
//銷毀對(duì)象.
sqlite3_finalize(stmt);
}
return arr;
}
@end
接下來(lái)是調(diào)用的方法
先設(shè)置打開(kāi)的數(shù)據(jù)庫(kù)路徑,再設(shè)置語(yǔ)句淮阐,增、刪沫换、改都用一個(gè)命令updateSQL
NSString *file = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"historyDB.sqlite"];
[[DataBaseHandle shareDataBaseHandle] open:file];
//創(chuàng)建SQL語(yǔ)句.
NSString *createTableSQL = @"CREATE TABLE IF NOT EXISTS history(number INTEGER PRIMARY KEY AUTOINCREMENT, content text)";
[[DataBaseHandle shareDataBaseHandle] updateSQL:createTableSQL];
添加語(yǔ)句也是一樣用updateSQL方法
NSString *SQL = [NSString stringWithFormat:@"insert into history(content) values('%@')", self.textFieldOfSearch.text];
[[DataBaseHandle shareDataBaseHandle] updateSQL:SQL];
查詢方法我返回了一個(gè)二維數(shù)組比如arr[0][0]
第一個(gè)0是表示第幾條數(shù)據(jù)
第二個(gè)0表示這一條數(shù)據(jù)的第幾個(gè)數(shù)據(jù)
比如數(shù)據(jù)是((1,張三, 男), (2, 李四, 女) )
arr[1][2]就能提取"女"
NSArray *result = [[DataBaseHandle shareDataBaseHandle] selectAllWithList:@"history" AndListNumber:2];
查詢語(yǔ)句中第一個(gè)參數(shù)是表名罢防,第二個(gè)數(shù)據(jù)是查詢每一條數(shù)據(jù)有幾列