C + + operation Sqllite

Source: Internet
Author: User

The project needs to use the sqllite, have thought about the use of memory results, as if they are not used, the closest is the vector, but the query is not good, and the data has several fields, so consider the database


Sqllite has now reached 3, good fast, as if this database is not weak! Just use it first!

==================================================================


It's actually quite simple, mostly a class.


There is a corresponding test function inside the main function

Temporarily did not find the bug, if it appears, please be sure to remind me! Thank you!

For details, see main function

Link to the test project:

http://download.csdn.net/detail/zengraoli/7457843



Sql_lite_helper.h:

#ifndef  __include_sql_lite_helper_h__#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 database int closedb ();//create a table int createtable (const char *create_table_state);//Delete a sheet 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 operation int update (const char *update_state);p rivate:sqlite3 *sqlite_ db_;//Database pointer char* err_msg_;//error message bool is_close_;//The identity of the closed data//display data element used primarily in selece operation static int callbackfunc (void * notused, int argc, char **argv, char **azcolname);//Execute SQL statement int sqlstateexec (const char *sql_state);}; #endif


Sql_lite_helper.cpp:

#include "iostream" using namespace std; #include "sql_lite_helper.h" int 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." << er R_msg_ << endl;return-1;} Else{cout << "CREATE table successed." << Endl;} return 0;} int Sqlitehelper::D roptable (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 <& Lt "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 fail ed. "<< Err_msg_ << endl;return-1;} res = sqlite3_exec (sqlite_db_, "Commit transaction;", 0, 0, &err_msg_); cout << "Insert operate successed." <& Lt Endl;return 0;} int Sqlitehelper::D elete (const char *delete_state) {int res = sqlstateexec (delete_state); (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 <& Lt "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; ind ex < Element_count; index++) {cout << col_name[index] << "=" << (Element[index]? Element[index]: "NULL") << ",";} cout << "\ n"; 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 "Sstream" using namespace std; #include "sql_lite_helper.h" Sqlitehelper sql_lite_helper;//Test: Creating a database table int testcreatetable () {return sql_lite_ Helper. CreateTable ("test_table (id int, name varchar, age int)");} TEST: Insert Data int Testinsert () {for (int i= 1; i < ++i) {std::stringstream str_sql;str_sql << "INSERT INTO Test _table values ("; Str_sql << i <<", "<< (i +) <<", "<< + <<"); "; std::string str = STR_SQL.STR (); Sql_lite_helper. Insert (Str.c_str ());} return 0;} Test: Tests Delete an element int testdelete () {string str_sql= "delete from test_table where id=4;"; Return Sql_lite_helper. Delete (Str_sql.c_str ());} Test: Testing updates 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: Testing Query int Testselect () {string str_sql= "select * from test_table;"; Return Sql_lite_helper. Select (str_sql.c_STR ());} Test: Tests Delete 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;}


Show Results:


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.