python 生產者消費者demo
阿新 • • 發佈:2020-11-12
sqlite3_bind_****
int sqlite3_bind_text(sqlite3_stmt*,int,const char*,int,void(*)(void*));
sqlite3_bind_text是slite3_bind家族的一員。作用是將sqlite3_prepare_v2 中sql語句裡的佔為符 ? 替換成變數。
第一個引數是從sqlite3_prepare_v2中返回的stmt結構的指標pStmt;
第二個引數是要替換佔位符的座標,從1開始;
第三個引數是要替換的變數;
第四個引數簡單使用寫成-1;
第五個引數簡單使用寫成nullptr 或 SQLITE_STATIC。
使用這個API可以防止sql注入(會把佔位符當成一個變數插入到表中)。
在大量更新,插入資料時,也非常高效。一般流程如下:
sqlite3_prepare_v2(); for(data : row){ sqlite3_bind_**(...data1...); sqlite3_bind_**(...data2...); .... sqlite3_step(); sqlite3_reset(); }
sqlite3_finalize()
簡單例項
#include <iostream> #include <stdio.h> #include "sqlite3.h" using namespace std; int main() { const char *name = "jojo"; const char *password = "123456"; sqlite3 *db; sqlite3_stmt *pStmt; int rv; rv = sqlite3_open("./mdata.db", &db); if(rv != SQLITE_OK) goto error_exit; rv= sqlite3_prepare_v2(db, "insert into players values(?,?)", -1, &pStmt, nullptr); if(rv != SQLITE_OK) goto error_exit2; rv = sqlite3_bind_text(pStmt, 1, name, -1, SQLITE_STATIC); if(rv != SQLITE_OK) goto error_exit2; rv = sqlite3_bind_text(pStmt, 2, password, -1, SQLITE_STATIC); if(rv != SQLITE_OK) goto error_exit2; rv = sqlite3_step(pStmt); if(rv != SQLITE_DONE) goto error_exit2; sqlite3_finalize(pStmt); sqlite3_close; return 0; error_exit: sqlite3_close(db); fprintf(stderr, "can't open db\n"); return -1; error_exit2: sqlite3_finalize(pStmt); sqlite3_close(db); fprintf(stderr, "run error\n"); return -2; }