* 包含头文件: 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;";