C++使用sqlite的方法
来源:互联网 发布:蔬菜网络销售平台 编辑:程序博客网 时间:2024/05/18 01:04
windows下sqlite的使用
1.先去官网下载sqllite文件
2.编译生成lib文件
将sqlite3.h(在sqlite-amalgamation-3071300.zip压缩包中)添加到工程。
sqlite3.lib复制到工程文件夹下。
工程属性中添加sqlite3.lib库依赖。
3.创建win32工程
代码如下:
/* 本程序测试sqlite数据库的增删改查 */ #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[]) { //打开路径采用utf-8编码 //如果路径中包含中文,需要进行编码转换 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; }
运行结果:
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,
0 0
- C使用SQLite的例子
- C++使用sqlite的方法
- SQLite加密的方法(c#)
- objective-c 中使用了sqlite数据库。字段定义为boolean型的处理方法
- sqlite的初步使用,JAVA + C#(winform)
- 使用java类测试sqlite的方法
- Android中使用SQLite的方法
- 使用特定的方法操作SQLite数据库
- sqlite c库使用
- SQLite使用rawQuery方法
- 【SQLite】SQLite的简单使用
- sqlite使用,c语言接口
- c++builder 使用sqlite数据库
- C#.NET下使用Sqlite
- SQLite数据库使用回调函数的简单C语言实例(摘自sqlite.org)
- C#.winform使用sqlite发布时提示无法找到SQLite.Interop.dll的问题
- sqlite3使用简介(内含解决sqlite内存的方法)
- 一次性解除Sqlite Developer使用限制的方法
- Android下拉刷新
- NSView中鼠标相应方法
- 2016年LTE推荐28本图书
- Mybatis逆向工程
- Shell替换:Shell变量替换,命令替换,转义字符
- C++使用sqlite的方法
- [系统操作][Linux]修改当前Ubuntu的启动方式
- MFC打开/保存文件对话框:CFileDialog
- ArcGIS教程:修改格网索引要素图层
- Java - Get Started with Trouble Shooting
- DataOutputStream/RandomAccessFile
- 纪念我的外婆
- 彻底理解ThreadLocal
- 数塔问题