1.使用Sqlite需要添加框架集libsqlite3.dylib以及import頭文件<sqlite3.h>。
2.Sqlite函數庫是使用c開發的,所以SQL查詢語句需要使用char*保存,NSString轉Char*請使用[NSString UTF8String]。
3.使用Sqlite查詢某字段含某關鍵字的SQL語句寫法是"select * from tb_table where title like '%hello%';",其中hello是要查詢的關鍵字,SQL語句后有分號。
4.百分號的轉換,NSString中需要格式化的字符串中百分號使用%%表示,而char*中百分號也是使用%%表示。
例如:NSLog(@"%%%@%%",@"hello"),控制臺會打印出%hello%。
printf([[NSString stringWithFormat:@"%%%@%%",@"hello"] UTF8String]),控制臺則會打印出hello。
所以,如果需要使用像第三點那樣模糊查詢,需要使用下面的方法。
NSString *queryStr = [NSString stringWithFormat:@"select * from tb_table where title like '%%%%%@%%%%';",@"hello"];
再執行sqlite3_prepare_v2(database, [queryStr UTF8String], -1, &statement,NULL),即可將tb_table表中所有title字段中含有hello的條目查詢出來。