这个代码是接着上次说的,要用VS2013操作数据库,首先要配置好环境,创建好数据库表等。
不明白的翻我前面2篇看看~~~
关于前面的用到的goto 语句,这个我也是参考其他博主写的,现在我注释掉了,毕竟goto咱也不懂,哈哈哈~~~
下面给上我的代码,不出意外直接复制粘贴可以使用:
#define _CRT_SECURE_NO_WARNINGS #include "stdafx.h" #include "sqlite3.h" #include <iostream> #include <string> #include <Windows.h> using namespace std; sqlite3 * pDB = NULL; //查找 bool SelectUser(); //增加 bool AddUser(const string& sName, const string& sFenshu); //删除 bool DeleteUser(const string& sName); //修改 bool ModifyUser(const string& sName, const string& sFenshu); int _tmain(int argc, _TCHAR* argv[]) { //打开路径采用utf-8编码 //如果路径中包含中文,需要进行编码转换 int nRes = sqlite3_open("D:\sqlite\fuck.db;", &pDB); if (nRes != SQLITE_OK) { cout << "Open database fail: " << sqlite3_errmsg(pDB); //goto QUIT; sqlite3_close(pDB); } else { system("color A"); cout << "打开数据库成功!" << endl; cout << "**************************C++连接sqlite数据操作**************************" << endl; cout << "1.查询" << endl; cout << "2.添加" << endl; cout << "3.删除" << endl; cout << "4.修改" << endl; cout << "0.退出" << endl; cout << "**************************C++连接sqlite数据操作**************************" << endl; cout << "请选择你需要的操作" << endl; } int a; char name[20]; char fenshu[20]; while (true) { cin >> a; switch (a){ case 1: cout << "你选择了查询" << endl; SelectUser(); cout << "**************************请继续选择操作或/退出**************************" << endl; break; case 2: cout << "你选择了添加" << endl; cout << "*******************请输入你要添加的信息如:xiaoming,18*******************" << endl; //cin >> id; cout << "name:" << endl; scanf("%s",&name); cout << "fenshu:" << endl; scanf("%s",&fenshu); //cout <<name << fenshu << endl; if (AddUser(name,fenshu)){ cout << "添加成功!" << endl; cout << "**************************请继续选择操作或/退出**************************" << endl; } break; case 3: cout << "你选择了删除" << endl; cout << "********************请输入你要删除的信息如:xiaoming*********************" << endl; //删除 cout << "name:" << endl; scanf("%s", &name); if (DeleteUser(name)) { cout << "信息删除成功!" << endl; cout << "**************************请继续选择操作或/退出**************************" << endl; } break; case 4: cout << "你选择了修改" << endl; cout << "*******************请输入你要修改的信息如:xiaoming,18*******************" << endl; //cin >> id; cout << "name:" << endl; scanf("%s", &name); cout << "fenshu:" << endl; scanf("%s", &fenshu); if (ModifyUser(name,fenshu)) { cout << "信息修改成功!" << endl; cout << "**************************请继续选择操作或/退出**************************" << endl; } break; default: cout << "你选择了退出" << endl; cout << "goodbye" << endl; exit(0); } } //查找 /*if (!SelectUser()) { goto QUIT; }*/ QUIT: sqlite3_close(pDB); return 0; } 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") << ", "; } //sqlite3_free_table(azColName); cout << endl; return 0; } bool SelectUser() { char* cErrMsg; // int res = sqlite3_exec(pDB, "select student.id, student.name,score.fenshu from student, score WHERE student.id= score.id;", UserResult, 0, &cErrMsg); int res = sqlite3_exec(pDB, "select * from score", UserResult, 0, &cErrMsg); if (res != SQLITE_OK) { cout << "select fail: " << cErrMsg << endl; return false; } return true; } //添加数据 bool AddUser( const string& sName, const string& sFenshu) { string strSql = ""; strSql += "insert into score(name,fenshu)"; strSql += "values('"; strSql += sName; strSql += "',"; strSql += sFenshu; strSql += ");"; char* cErrMsg; int nRes = sqlite3_exec(pDB, strSql.c_str(), 0, 0, &cErrMsg); if (nRes != SQLITE_OK) { cout << "add score fail: " << cErrMsg << endl; return false; } else { cout << "add score success: " << sName.c_str() << " " << sFenshu.c_str() << endl; } return true; } //删除数据 bool DeleteUser(const string& sName) { string strSql = ""; strSql += "delete from score where name='"; strSql += sName; strSql += "';"; char* cErrMsg; int nRes = sqlite3_exec(pDB, strSql.c_str(), 0, 0, &cErrMsg); if (nRes != SQLITE_OK) { cout << "delete score fail: " << cErrMsg << endl; return false; } else { cout << "delete score success: " << sName.c_str() << endl; } return true; } //修改数据 bool ModifyUser(const string& sName, const string& sFenshu) { string strSql = ""; strSql += "update score set fenshu ="; strSql += sFenshu; 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 score fail: " << cErrMsg << endl; return false; } else { cout << "modify score success: " << sName.c_str() << " " << sFenshu.c_str() << endl; } return true; }
说明:score 是我自己 在fuck.db创建的表,你们可以自己创建,创建教程前面也有~~
运行界面: