可以進(jìn)行數(shù)據(jù)庫建表棍弄,以及增刪改査等操作
使用前需要導(dǎo)入sqlite3數(shù)據(jù)庫包
SandBoxPath.h
#import <Foundation/Foundation.h>
@interface SandBoxPath : NSObject
//沙盒的主路徑
+ (NSString *)homePath;
//沙盒中Documents路徑
+ (NSString *)documents;
//沙盒中l(wèi)ibraray路徑
+ (NSString *)library;
//libraryCaches路徑
+ (NSString *)libraryCaches;
//temp路徑
+ (NSString *)temp;
@end
SandBoxPath.m
#import "SandBoxPath.h"
@implementation SandBoxPath
//得到沙盒主目錄
+ (NSString *)homePath
{
return NSHomeDirectory();
}
//得到Documents文件夾目錄
+ (NSString *)documents
{
return NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
}
//得到Library文件夾目錄
+ (NSString *)library
{
return NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).firstObject;
}
//得到caches文件夾
+ (NSString *)libraryCaches
{
return NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject;
}
//得到tem文件夾
+ (NSString *)temp
{
return NSTemporaryDirectory();
}
@end
DataBaseHelper.h
#import <Foundation/Foundation.h>
#import <sqlite3.h>
@interface DataBaseHelper : NSObject
+ (instancetype)sharedDatabaseHelper;
//創(chuàng)建數(shù)據(jù)庫文件路徑
- (void)dbFilePathWithFileName: (NSString *)fileName;
//除了查詢語句以外的所有語句
- (void)operationSQLWithSqlString: (NSString *)sqlstr;
//查詢語句
- (NSArray *)queryDataWithSqlstring: (NSString *)sqlString;
@end
DataBaseHelper.m
#import "DataBaseHelper.h"
#import "SandBoxPath.h"
@interface DataBaseHelper ()
@property (nonatomic,retain) NSString *fileName;
@end
@implementation DataBaseHelper
+ (instancetype)sharedDatabaseHelper
{
static DataBaseHelper *dataBaseHelper = nil;
@synchronized(self) {
if (!dataBaseHelper) {
dataBaseHelper = [[DataBaseHelper alloc] init];
}
return dataBaseHelper;
}
}
//創(chuàng)建數(shù)據(jù)庫文件路徑
- (void)dbFilePathWithFileName: (NSString *)fileName
{
//得到沙盒的documents路徑
NSString *docuPath = [SandBoxPath documents];
//判斷文件名是否帶后綴,如果有直接使用,如果沒有例隆,內(nèi)部為它拼接
if (![fileName containsString:@".sqlite"])
{
fileName = [fileName stringByAppendingString:@".sqlite"];
}
self.fileName = [docuPath
stringByAppendingPathComponent:fileName];
}
//打開或者創(chuàng)建數(shù)據(jù)庫
- (sqlite3 *)openOrCreateDB
{
sqlite3 *sqlDB = NULL;
//打開數(shù)據(jù)庫
int result = sqlite3_open(self.fileName.UTF8String, &sqlDB);
if (result == SQLITE_OK) {
NSLog(@"數(shù)據(jù)庫打開成功");
return sqlDB;
}
else
{
NSLog(@"數(shù)據(jù)庫打開失敗");
return NULL;
}
}
//除了查詢操作憔维,其他操作所執(zhí)行的方法
- (void)operationSQLWithSqlString: (NSString *)sqlstr
{
sqlite3 *sqlDB = [self openOrCreateDB];
int result = sqlite3_exec(sqlDB, sqlstr.UTF8String, NULL, NULL, NULL);
if (result == SQLITE_OK) {
NSLog(@"語句執(zhí)行成功");
//每次執(zhí)行完成之后微姊,需要關(guān)閉數(shù)據(jù)庫
sqlite3_close(sqlDB);
NSLog(@"%@",self.fileName);
}
else
{
NSLog(@"語句執(zhí)行失敗");
//每次執(zhí)行完成之后航攒,需要關(guān)閉數(shù)據(jù)庫
sqlite3_close(sqlDB);
}
}
//數(shù)據(jù)庫查詢,通用
- (NSArray *)queryDataWithSqlstring: (NSString *)sqlString
{
//初始化可變數(shù)據(jù)让簿,用來盛放所有記錄
NSMutableArray *resultArray = [[NSMutableArray alloc] init];
//打開數(shù)據(jù)庫
sqlite3 *sqlDB = [self openOrCreateDB];
//聲明stmt指針敬察,一會(huì)用來盛放
sqlite3_stmt *stament = NULL;
int result = sqlite3_prepare(sqlDB, sqlString.UTF8String, -1, &stament, NULL);
if (result == SQLITE_OK) {
//沒執(zhí)行一次step函數(shù),stament中就會(huì)完整的保存一條記錄
while (sqlite3_step(stament) == SQLITE_ROW) {
//確定該表有幾個(gè)字段拜英,其實(shí)就是有幾列
int columnCount = sqlite3_column_count(stament);
NSMutableDictionary *mDict = [NSMutableDictionary dictionary];
//遍歷所有的列
for (int i = 0; i < columnCount; i++) {
int type = sqlite3_column_type(stament, i);
//得到字段名稱
const char *attName = sqlite3_column_name(stament, i);
NSString *keyString = [NSString stringWithCString:attName encoding:NSUTF8StringEncoding];
switch (type) {
case SQLITE_INTEGER:
{
int value = sqlite3_column_int(stament,i);
[mDict setObject:[NSNumber numberWithInt:value] forKey:keyString];
}
break;
case SQLITE_TEXT:
{
NSString *valueString = [NSString stringWithCString:(const char *)sqlite3_column_text(stament, i) encoding:NSUTF8StringEncoding];
[mDict setObject:valueString forKey:keyString];
}
break;
case SQLITE_FLOAT:
{
float value = sqlite3_column_int(stament,i);
[mDict setObject:[NSNumber numberWithFloat:value] forKey:keyString];
}
break;
default:
break;
}
}
[resultArray addObject:mDict];
}
}
sqlite3_close(sqlDB);
sqlite3_finalize(stament);
return resultArray;
}
@end