1、导入 libsqlite3.0.tbd库
2、#import <sqlite3.h>
3、执行语句
-(void)querySqlString:(NSString*)sql{
NSString * cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
NSString * sqliteName = @"sqlData.sqlite";
NSString * sqlitePath = [cachePath stringByAppendingPathComponent:sqliteName];
// 打开
sqlite3 * ppDb = nil;
BOOL openResult = sqlite3_open(sqlitePath.UTF8String, &ppDb);
if(!openResult){return;}
// 执行(增、删、改)
NSString * execSqlString = @"";
BOOL execResult = sqlite3_exec(ppDb, execSqlString.UTF8String, nil, nil, nil) == SQLITE_OK;
if(!execResult){return;}
// 查询
NSMutableArray * resArray = [NSMutableArray array];
NSString * querySqlString = @"";
sqlite3_stmt * ppStmt = nil;
BOOL prepareReslut = sqlite3_prepare_v2(ppDb, querySqlString.UTF8String, -1, &ppStmt, nil) == SQLITE_OK;
if(!prepareReslut){return;}
while (sqlite3_step(ppStmt) == SQLITE_ROW) {
// 获取所有列的个数
NSMutableDictionary * json = [NSMutableDictionary dictionary];
int columnCount = sqlite3_column_count(ppStmt);
for(int i=0;i<columnCount;i++){
// 获取列key值
const char * columnNameC = sqlite3_column_name(ppStmt, i);
NSString * columnName = [NSString stringWithUTF8String:columnNameC];
// 获取列value类型
int valueType = sqlite3_column_type(ppStmt, i);
id value = nil;
switch (valueType) {
case SQLITE_INTEGER:
value = @(sqlite3_column_int(ppStmt, i));
break;
case SQLITE_FLOAT:
value = @(sqlite3_column_double(ppStmt, i));
break;
case SQLITE_BLOB:
value = CFBridgingRelease(sqlite3_column_blob(ppStmt, i));
break;
case SQLITE_TEXT:
value = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(ppStmt, i)];
break;
default:
value = @"";
break;
}
[json setValue:value forKey:columnName];
}
[resArray addObject:json];
}
// 释放
sqlite3_finalize(ppStmt);
// 关闭
sqlite3_close(ppDb);
}