* 包含頭文件: import "sqlite3.h"
sqlite的方法
* 1、sqlite3 *db,數據庫句柄,跟文件句柄FILE很類似
* 2、sqlite3_stmt *stmt,這個相當於ODBC的Command對象,用於保存編譯好的SQL語句
* 3、sqlite3_open() 打開數據庫,沒有數據庫時創建
* 4、sqlite3_exec() 執行非查詢的sql語句
* 5、Sqlite3_step() 在調用sqlite3_prepare後,使用這個函數在記錄集中移動
* 6、sqlite3_close()關閉數據庫
*
* 還有一系列用於從記錄集字段中獲取數據,e.g
* 1、sqlite3_column_text() 取text類型的數據
* 2、sqlite3_column_blob() 取blob類型數據
* 3、sqlite3_column_int() 取int類型數據
// 邦定第一個int參數
sqlite3_bind_int(stmt, 1, 1);
// 邦定第二個字符串參數
sqlite3_bind_text(stmt, 2, [title UTF8String], -1, SQLITE_TRANSIENT);
完整的流程代碼
NSString *sql = [NSString stringWithFormat:@"select NAME from education where ID = %d", index];
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(dictDatabase, [sql UTF8String], -1, &statement, nil) != SQLITE_OK) {
sqlite3_close(dictDatabase);
} else {
if (sqlite3_step(statement) == SQLITE_ROW) {
char *cResult = (char *)sqlite3_column_text(statement, 0);
if (cResult) {
result = [NSString stringWithUTF8String:cResult];
}
}
}
sqlite3_finalize(statement);
相關查詢語句
NSString *sql = [NSString stringWithFormat:@"select NAME from education where ID = %d", index];
NSString *sql = [NSString stringWithFormat:@"select BIG_ID from vocation where id = %d",index] ;
NSString *sql = @"select DISTINCT BIG_NAME ,BIG_ID from vocation ORDER BY BIG_ID";
sql = [NSString stringWithFormat:@"select BIG_NAME from vocation where BIG_ID = %d", index];
sql = [NSString stringWithFormat:@"select ICONURL from vocation where ID = %d",index];
NSString *sql = @"select count(ID) from education";
NSString *sql = @"select count(DISTINCT BIG_ID) from vocation";
NSString *sql = @"select NAME from province order by ID asc";
NSString *sql = [NSString stringWithFormat:@"select ID from city where NAME like '%@\%%' and PRO_ID = %d", cityName, provinceId];
NSString *sqlStr = @"select icon, name from emotions where type = ? order by icon asc;";