#include <Windows.h> #include <stdio.h> #include "sqlite3.h" void test_sql_open() { int nret = 0; sqlite3* db = 0; nret = sqlite3_open("test.db",&db); if(nret==sqlITE_OK) printf("test_sql_open: success! sqlite3*:%u /n",db); else printf("test_sql_open: Failed! sqlite3*:%u /n",db); sqlite3_close(db); } void test_sql_open_create_table() { int nret = 0; sqlite3* db = 0; nret = sqlite3_open("test.db",db); char* errmsg = 0; nret = sqlite3_exec(db,"create table Table1 (id int,comments varchar(128))",&errmsg); if(nret==sqlITE_OK) printf("sqlite3_exec: success! sqlite3*:%u,error message: %s /n",db,errmsg); else printf("sqlite3_exec: Failed! sqlite3*:%u,errmsg); sqlite3_close(db); } void test_sql_open_insert_data() { int nret = 0; sqlite3* db = 0; nret = sqlite3_open("test.db","insert into Table1 (id,comments) values (1,'Hello,how are you?')",errmsg); sqlite3_close(db); } void test_sql_open_insert_data_using_prepare() { int nret = 0; sqlite3* db = 0; nret = sqlite3_open("test.db",db); sqlite3_stmt *stmt=0; char* errmsg = 0; nret = sqlite3_prepare(db,comments) values (99,?)",-1,&stmt,0); if(nret==sqlITE_OK) printf("sqlite3_prepare: success! sqlite3*:%u,errmsg); else printf("sqlite3_prepare: Failed! sqlite3*:%u,errmsg); char str[] = "How do you do?"; nret = sqlite3_bind_text(stmt,1,str,errmsg); //int id = 888; //nret = sqlite3_bind_int(stmt,id); //if(nret==sqlITE_OK) // printf("sqlite3_prepare: success! sqlite3*:%u,errmsg); //else // printf("sqlite3_prepare: Failed! sqlite3*:%u,errmsg); nret = sqlite3_step(stmt); nret = sqlite3_finalize(stmt); sqlite3_close(db); } void test_sql_open_insert_data_using_prepare2() { int nret = 0; sqlite3* db = 0; nret = sqlite3_open("test.db",comments) values (?,2,errmsg); int id = 888; nret = sqlite3_bind_int(stmt,id); if(nret==sqlITE_OK) printf("sqlite3_prepare: success! sqlite3*:%u,errmsg); nret = sqlite3_step(stmt); nret = sqlite3_finalize(stmt); sqlite3_close(db); } int main(__in int _Argc,__in char ** _Argv) { printf("Hello db!/n"); //test_sql_open(); //test_sql_open_create_table(); //test_sql_open_insert_data(); test_sql_open_insert_data_using_prepare(); return 0; }
原文链接:https://www.f2er.com/sqlite/202917.html