C++使用SQLite步骤及示例
时间:2021-07-01 10:21:17
帮助过:3人阅读
4、 创建示例工程
? 创建win32控制台工程SQLiteTest。
? sqlite3.h(在sqlite-amalgamation-3071300.zip压缩包中)添加到工程。
? sqlite3.lib复制到工程文件夹下。
? 工程属性中添加sqlite3.lib库依赖。
Configuration Properties->Linker->Input->Additional Dependencies添加sqlite3.lib。
? 程序代码为:
[cpp] view plain
copy
-
- #include "stdafx.h"
- #include "sqlite3.h"
- #include <iostream>
- using namespace std;
-
- sqlite3 * pDB = NULL;
-
- bool AddUser(const string& sName, const string& sAge);
- bool DeleteUser(const string& sName);
- bool ModifyUser(const string& sName, const string& sAge);
- bool SelectUser();
-
- int _tmain(int argc, _TCHAR* argv[])
- {
-
-
- int nRes = sqlite3_open("D:\\sqlite\\test.db", &pDB);
- if (nRes != SQLITE_OK)
- {
- cout<<"Open database fail: "<<sqlite3_errmsg(pDB);
- goto QUIT;
- }
-
-
- if ( !AddUser("zhao", "18")
- || !AddUser("qian", "19")
- || !AddUser("sun", "20")
- || !AddUser("li", "21"))
- {
- goto QUIT;
- }
-
-
- if (!DeleteUser("zhao"))
- {
- goto QUIT;
- }
-
-
- if (!ModifyUser("sun", "15"))
- {
- goto QUIT;
- }
-
-
- if (!SelectUser())
- {
- goto QUIT;
- }
-
- QUIT:
- sqlite3_close(pDB);
-
- return 0;
- }
-
- bool AddUser(const string& sName, const string& sAge)
- {
- string strSql = "";
- strSql += "insert into user(name,age)";
- strSql += "values(‘";
- strSql += sName;
- strSql += "‘,";
- strSql += sAge;
- strSql += ");";
-
- char* cErrMsg;
- int nRes = sqlite3_exec(pDB , strSql.c_str() ,0 ,0, &cErrMsg);
- if (nRes != SQLITE_OK)
- {
- cout<<"add user fail: "<<cErrMsg<<endl;
- return false;
- }
- else
- {
- cout<<"add user success: "<<sName.c_str()<<"\t"<<sAge.c_str()<<endl;
- }
-
- return true;
- }
-
- bool DeleteUser(const string& sName)
- {
- string strSql = "";
- strSql += "delete from user where name=‘";
- strSql += sName;
- strSql += "‘;";
-
- char* cErrMsg;
- int nRes = sqlite3_exec(pDB , strSql.c_str() ,0 ,0, &cErrMsg);
- if (nRes != SQLITE_OK)
- {
- cout<<"delete user fail: "<<cErrMsg<<endl;
- return false;
- }
- else
- {
- cout<<"delete user success: "<<sName.c_str()<<endl;
- }
-
- return true;
- }
-
- bool ModifyUser(const string& sName, const string& sAge)
- {
- string strSql = "";
- strSql += "update user set age =";
- strSql += sAge;
- strSql += " where name=‘";
- strSql += sName;
- strSql += "‘;";
-
- char* cErrMsg;
- int nRes = sqlite3_exec(pDB , strSql.c_str() ,0 ,0, &cErrMsg);
- if (nRes != SQLITE_OK)
- {
- cout<<"modify user fail: "<<cErrMsg<<endl;
- return false;
- }
- else
- {
- cout<<"modify user success: "<<sName.c_str()<<"\t"<<sAge.c_str()<<endl;
- }
-
- return true;
- }
-
- static int UserResult(void *NotUsed, int argc, char **argv, char **azColName)
- {
- for(int i = 0 ; i < argc ; i++)
- {
- cout<<azColName[i]<<" = "<<(argv[i] ? argv[i] : "NULL")<<", ";
- }
- cout<<endl;
-
- return 0;
- }
-
- bool SelectUser()
- {
- char* cErrMsg;
- int res = sqlite3_exec(pDB, "select * from user;", UserResult , 0 , &cErrMsg);
-
- if (res != SQLITE_OK)
- {
- cout<<"select fail: "<<cErrMsg<<endl;
- return false;
- }
-
- return true;
- }
? 编译成功后,将sqlite3.dll复制到SQLiteTest.exe同一目录下,运行SQLiteTest.exe。
运行结果:
[plain] view plain
copy
- add user success: zhao 18
- add user success: qian 19
- add user success: sun 20
- add user success: li 21
- delete user success: zhao
- modify user success: sun 15
- id = 2, name = qian, age = 19,
- id = 3, name = sun, age = 15,
- id = 4, name = li, age = 21,
5、 SQLite管理工具
可视化管理工具,推荐使用:SQLite Expert,见:http://www.sqliteexpert.com/。
C++使用SQLite步骤及示例
标签: