SQLITE的C++调用示例

前端之家收集整理的这篇文章主要介绍了SQLITE的C++调用示例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

#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; }

猜你在找的Sqlite相关文章