The sqllite that needs to be used in the project. If you have thought about the results of using the memory, it seems that it is not very useful. The closest one is vector, but the query method is not good, and the data has several fields, so we considered the database
It is actually quite simple, mainly a class.
The main function has the corresponding test function.
No bugs found. If any, please remind me! Thank you!
For details, see the main function.
SQL _lite_helper.h:
# Ifndef _ define __# define _ INCLUDE_ SQL _LITE_HELPER_H __# include sqlite3.h # pragma comment (lib, sqlite3.lib) class SQLiteHelper {public: SQLiteHelper () {} virtual ~ SQLiteHelper () {CloseDB () ;}// open the database int OpenDB (const char * path); // close the database int CloseDB (); // create a table int CreateTable (const char * create_table_state); // delete a table int DropTable (const char * table_name ); // query operation int Select (const char * select_state); // Insert operation int Insert (const char * insert_state); // Delete operation int Delete (const char * delete_state ); // Update int Update (const char * update_state); private: sqlite3 * sqlite_db _; // database pointer char * err_msg _; // error message bool is_close _; // disable the data identifier. // It is mainly used to display the data element static int CallBackFunc (void * NotUsed, int argc, char ** argv, char ** azColName) in the selece operation ); // execute the SQL statement int SqlStateExec (const char * SQL _state) ;};# endif
SQL _lite_helper.cpp:
#include iostreamusing namespace std;#include sql_lite_helper.hint SQLiteHelper::OpenDB(const char *path){int res = sqlite3_open(path, &sqlite_db_);if(res){cout << can't open database: << sqlite3_errmsg(sqlite_db_);sqlite3_close(sqlite_db_);return -1;}is_close_ = false;return 0;}int SQLiteHelper::CloseDB(){if (!is_close_){int res = sqlite3_close(sqlite_db_);if(res){cout << can't close database: << sqlite3_errmsg(sqlite_db_);return -1;}}return 0;}int SQLiteHelper::CreateTable(const char *table_name_and_field){string create_table_state = create table ;create_table_state += table_name_and_field;create_table_state += ;;int res = SqlStateExec(create_table_state.c_str());if (res != SQLITE_OK){ cout << create table failed. << err_msg_ << endl;return -1;}else{cout << create table successed. << endl;}return 0;}int SQLiteHelper::DropTable(const char *table_name){string sql_state = drop table ;sql_state += table_name;sql_state += ;;int res = SqlStateExec(sql_state.c_str());if (res != SQLITE_OK){ cout << drop table failed. << err_msg_ << endl;return -1;}else{cout << drop table successed. << endl;}return 0;}int SQLiteHelper::Select(const char *select_state){int res = SqlStateExec(select_state);if (res != SQLITE_OK){cout << select operate failed. << err_msg_ << endl;return -1;}else{cout << select operate successed. << endl;}return 0;}int SQLiteHelper::Insert(const char *insert_state){int res = sqlite3_exec(sqlite_db_, begin transaction;, CallBackFunc, 0, &err_msg_);res = SqlStateExec(insert_state);if (res != SQLITE_OK) {cout << insert operate failed. << err_msg_ << endl;return -1;} res = sqlite3_exec(sqlite_db_, commit transaction;, 0, 0, &err_msg_);cout << insert operate successed. << endl;return 0;}int SQLiteHelper::Delete(const char *delete_state){int res = SqlStateExec(delete_state);if (res != SQLITE_OK){cout << delete operate failed. << err_msg_ << endl;return -1;}else{cout << delete operate successed. << endl;}return 0;}int SQLiteHelper::Update(const char *update_state){int res = SqlStateExec(update_state);if (res != SQLITE_OK){cout << update operate failed. << err_msg_ << endl;return -1;}else{cout << update operate successed. << endl;}return 0;}int SQLiteHelper::CallBackFunc(void *not_used, int element_count, char **element, char **col_name){for(int index = 0 ; index < element_count ; index++){cout << col_name[index] << = << (element[index] ? element[index] : NULL) << , ;}cout << ;return 0; }int SQLiteHelper::SqlStateExec(const char *sql_state){return sqlite3_exec(sqlite_db_, sql_state, CallBackFunc, 0, &err_msg_);}
Main:
// Test_use_sqlite.cpp: defines the entry point of the console application. // # Include stdafx. h # include iostream # include sstreamusing namespace std; # include SQL _lite_helper.hSQLiteHelper SQL _lite_helper; // Test: creates a database table int TestCreateTable () {return SQL _lite_helper.CreateTable (test_table (id int, name varchar, age int);} // Test: int TestInsert () {for (int I = 1; I <10; ++ I) {std: stringstream str_ SQL; str_ SQL <insert into test_table values (; str_ SQL <I <, <(I + 10) <, <23 <); std: string str = str_ SQL .str (); SQL _lite_helper.Insert (str. c_str ();} return 0;} // Test: Test the function of deleting an element int TestDelete () {string str_ SQL = delete from test_table where id = 4 ;; return SQL _lite_helper.Delete (str_ SQL .c_str ();} // Test: update an element int TestUpdate () {string str_ SQL = update test_table set name = 'sqlite3 'where name = '17'; return SQL _lite_helper.Update (str_ SQL .c_str ();} // Test: Test the int Test query. Select () {string str_ SQL = select * from test_table; return SQL _lite_helper.Select (str_ SQL .c_str ();} // Test: Test to delete the table int TestDropTable () {return SQL _lite_helper.DropTable (test_table);} int main () {int res = SQL _lite_helper.OpenDB (. /Test. db3); res = TestCreateTable (); if (res! = 0) {return 0;} res = TestInsert (); if (res! = 0) {return 0;} TestSelect (); res = TestDelete (); if (res! = 0) {return 0;} res = TestUpdate (); if (res! = 0) {return 0;} TestSelect (); TestDropTable (); TestSelect (); return 0 ;}
Display result: