代码:Database2.7z
Database.h:
1 #pragma once 2 3 #include<string> 4 #include<vector> 5 #include<Windows.h> 6 #include<tchar.h> 7 #include<iostream> 8 9 #include"sqlite3.h" 10 11 using namespace std; 12 13 class Database{ 14 public: 15 Database(); 16 ~Database(); 17 bool initDatabase();//打开数据库,成功返回true 18 bool createTable(std::string tablename);//在数据库中创建一个表 19 void showData();//输出数据库中的数据 20 bool insertDatabase(string tablename, string name, string number); 21 bool queryDatabase(string tableName); 22 void dropTable(string tableName); 23 void deleteData(string tableName); 24 void changeData(string tableName); 25 26 private: 27 sqlite3* db; 28 sqlite3_stmt* stmt; 29 }; 30 31 static int callback(void* data, int argc, char** argv, char** azColName);
Database.cpp:
1 #pragma once 2 3 #include"Database.h" 4 5 vector<string> vstr; 6 vector<string> vnumber; 7 8 static int callback(void* data, int argc, char** argv, char** azColName){ 9 for (int i = 0; i < argc; i++){ 10 switch (i + 1){ 11 case 0: 12 break;//id已弃用 13 case 1: 14 vstr.push_back(string(argv[i])); break; 15 case 2: 16 vnumber.push_back(string(argv[i])); break; 17 default: 18 break; 19 } 20 } 21 return 0; 22 } 23 24 Database::Database(){ 25 26 } 27 28 Database::~Database(){ 29 30 } 31 32 bool Database::initDatabase(){ 33 int rc = sqlite3_open("TestDB.db", &db); 34 if (rc){ 35 MessageBox(NULL, _T("数据库打开失败!"), 0, 0); 36 return false; 37 } 38 return true; 39 } 40 41 bool Database::createTable(std::string tablename){ 42 string sql = "CREATE TABLE TEST ( " 43 "NAME TEXT NOT NULL," 44 "NUMBER TEXT NOT NULL)"; 45 char* zErrMsg = 0; 46 int rc = sqlite3_exec(db, sql.c_str(), callback, 0, &zErrMsg); 47 if (rc != SQLITE_OK){ 48 return false; 49 } 50 return true; 51 } 52 53 void Database::showData(){ 54 if (vstr.size() == 0){ 55 cout << "数据库中没有数据! "; 56 return; 57 } 58 for (vector<string>::iterator i = vstr.begin(); i != vstr.end(); i++){ 59 cout << i->data() << " "; 60 } 61 cout << " "; 62 for (int i = 0; i < vnumber.size(); i++){ 63 cout << vnumber[i] << " "; 64 } 65 cout << " "; 66 } 67 68 bool Database::insertDatabase(string tablename, string name, string number){ 69 string sql = "INSERT INTO " + 70 tablename + 71 " ( NAME, NUMBER)" + 72 " VALUES('" + 73 name + 74 "','" + 75 number + 76 "');"; 77 char* zErrMsg = 0; 78 int rc = sqlite3_exec(db, sql.c_str(), callback, 0, &zErrMsg); 79 if (rc != SQLITE_OK){ 80 MessageBox(NULL, sql.c_str(), NULL, NULL); 81 return false; 82 } 83 return true; 84 } 85 86 bool Database::queryDatabase(string tableName){ 87 string sql = "SELECT * FROM "; 88 sql += tableName; 89 char* zErrMsg = 0; 90 int rc = sqlite3_exec(db, sql.c_str(), callback, 0, &zErrMsg); 91 return true; 92 } 93 94 void Database::dropTable(string tableName){ 95 string sql = "DROP TABLE "; 96 sql += tableName; 97 char* zErrMsg = 0; 98 int rc = sqlite3_exec(db, sql.c_str(), callback, 0, &zErrMsg); 99 } 100 101 void Database::deleteData(string tableName){ 102 string sql = "DELETE FROM "; 103 sql += tableName; 104 char* zErrMsg = 0; 105 int rc = sqlite3_exec(db, sql.c_str(), callback, 0, &zErrMsg); 106 } 107 108 void Database::changeData(string tableName){ 109 string sql = "UPDATE TEST SET NAME = 'MYTEST'"; 110 char* zErrMsg = 0; 111 int rc = sqlite3_exec(db, sql.c_str(), callback, 0, &zErrMsg); 112 }
main.cpp:
1 #include"Database.h" 2 3 int main(){ 4 Database* db = new Database; 5 db->initDatabase(); 6 db->createTable(""); 7 db->insertDatabase("TEST", "test", "1"); 8 db->changeData("vv"); 9 db->queryDatabase("TEST"); 10 db->showData(); 11 db->deleteData("TEST"); 12 db->dropTable("TEST"); 13 delete db; 14 system("pause"); 15 return 0; 16 }