這個代碼是接着上次說的,要用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() << "\t" << 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() << "\t" << sFenshu.c_str() << endl; } return true; }
說明:score 是我自己 在fuck.db創建的表,你們可以自己創建,創建教程前面也有~~
運行界面: