本文中,我会给大家用几个小程序示例sqlITE C/C++ API的使用。
1.我们看下最简单的sqlite程序,通过sqlite3_open, sqlite3_exec, sqlite3_close来实现一个简单的数据库操作。
-
-
-
-
-
-
-
- #include<cstdlib>
- #include<cstdio>
- #include<sqlite3.h>
- static@H_404_120@int_sql_callback(void*notused,@H_404_120@intargc,@H_404_120@char**argv,@H_404_120@char**szColName)
- {
- @H_404_120@inti=0;
- printf("notused:0x%x,argc:%d\n",notused,argc);
- for(i=0;i<argc;i++)
- {
- printf("%s=%s\n",szColName[i],argv[i]==0?"NULL":argv[i]);
- }
- printf("\n");
-
- return0;
-
-
- @H_404_120@intmain(@H_404_120@intargc,@H_404_120@char**argv)
- const@H_404_120@char*ssql1="createtableusers(useridvarchar(20)PRIMARYKEY,ageint,birthdaydatetime);";
- const@H_404_120@char*ssql2="insertintousersvalues('wang',20,'1989-5-4');";
- const@H_404_120@char*ssql3="select*fromusers;";
- sqlite3*db=0;
- @H_404_120@char*pErrMsg=0;
- @H_404_120@intret=0;
-
- ret=sqlite3_open("./test.db",&db);
- if(ret!=sqlITE_OK)
- fprintf(stderr,"无法打开数据库:%s\n",sqlite3_errmsg(db));
- sqlite3_close(db);
- return1;
- printf("数据库连接成功\n");
-
- ret=sqlite3_exec(db,ssql1,_sql_callback,&pErrMsg);
- "sqlcreateerror:%s\n",pErrMsg);
- sqlite3_free(pErrMsg);
- sqlite3_close(db);
- return1;
- }
- printf("数据库建表成功!!\n");
-
- sql2,&pErrMsg);
- if(ret!=sqlITE_OK)
- "sqlinserterror:%s\n",pErrMsg);
- sqlite3_free(pErrMsg);
- printf("数据库插入数据成功!\n");
-
- sql3,"sqlerror:%s\n",108); list-style:decimal-leading-zero outside; margin:0px!important; padding:0px 3px 0px 10px!important"> sqlite3_free(pErrMsg);
- printf("数据库查询成功!!\n");
-
- db=0;
- return0;
- }
运行结果如下结果: