SQLite 参数化查询
SQLite参数化查询
首先给出两个参考博客:
贴出一段自己用的代码:
#include <stdio.h> #include <stdlib.h> #include <sqlite3.h> int main(int argc, char* argv[]) { sqlite3 *db; char *zErrMsg = 0; int rc; char *sql; const char* data = "Callback function called"; sqlite3_stmt *res; /* Open database */ rc = sqlite3_open("test.db", &db); if( rc ){ fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); exit(0); }else{ fprintf(stderr, "Opened database successfully\n"); } /* Create SQL statement */ sql = "SELECT count(*) from COMPANY WHERE ID = ? AND AGE = '32'"; /* Execute SQL statement */ rc = sqlite3_prepare_v2(db, sql, -1, &res, 0); if (rc == SQLITE_OK) { sqlite3_bind_int(res, 1, 1);//查询第一个问好处ID为1的条目 printf("bind success!\n"); } else { fprintf(stderr, "Failed to execute statement: %s\n", sqlite3_errmsg(db)); } int step = sqlite3_step(res); if (step == SQLITE_ROW) { printf("%d: ", sqlite3_column_int(res, 0)); //sqlite3_column_* 有多种形式 //printf("%s\n", sqlite3_column_text(res, 1));//若存在第二个问号,需要此句,但本处不需要 } sqlite3_finalize(res); sqlite3_close(db); return 0; }