|
|

楼主 |
发表于 2006-8-15 16:40:20
|
显示全部楼层
Ok , here we go, go throughly the whole source code :
sqlite_db.h//
- #ifndef SQLITE_DB_H
- #define SQLITE_DB_H
- #include <string>
- #include <map>
- #include <vector>
- #include "sqlite.h"
- #include <stdexcept>
- class sqlite_exception : public std::runtime_error
- {
- public:
- explicit sqlite_exception(const string& what) : std::runtime_error( what )
- {}
- };
- typedef std::map<std::string,std::string> sqlite_fields;
- typedef std::vector<sqlite_fields> sqlite_result;
- class sqlite_db
- {
- private:
- sqlite* db_;
- static int sqlite_callback(void *param,int colcount,char **cols,char **colnames);
- public:
- sqlite_db():db_(NULL){}
- virtual ~sqlite_db(){if(db_)close();}
- const char *version()const{return SQLITE_VERSION;}
- void open(const std::string& db);
- void close();
- sqlite_result exec_sql(const std::string& query);
- void exec_dml(const std::string& query);
- };
- #endif
复制代码
sqlite_db.cpp//
- #include "sqlite_db.h"
- void sqlite_db::open(const std::string& db)
- {
- if(db_)close();
- char *errmsg = NULL;
- db_ = sqlite_open(db.c_str(),0,&errmsg);
- if(db_ == 0)
- {
- std::string error(errmsg);
- sqlite_freemem(errmsg);
- throw sqlite_exception(error);
- }
- if(errmsg != NULL) sqlite_freemem(errmsg);
- }
- void sqlite_db::close()
- {
- if(db_)sqlite_close(db_);
- db_ = NULL;
- }
- sqlite_result sqlite_db::exec_sql(const std::string& query)
- {
- if(!db_)throw sqlite_exception("No opened database");
- char* errmsg = NULL;
- sqlite_result result;
- int res = sqlite_exec(db_,query.c_str(),sqlite_callback,(void*)&result,&errmsg);
- if(res != SQLITE_OK)
- {
- std::string error(errmsg);
- sqlite_freemem(errmsg);
- throw sqlite_exception(error);
- }
- if(errmsg != NULL) sqlite_freemem(errmsg);
- return result;
- }
- void sqlite_db::exec_dml(const std::string& query)
- {
- if(!db_)throw sqlite_exception("No opened database");
- char* errmsg = NULL;
- int res = sqlite_exec(db_,query.c_str(),sqlite_callback,(void*)NULL,&errmsg);
- if(res != SQLITE_OK)
- {
- std::string error(errmsg);
- sqlite_freemem(errmsg);
- throw sqlite_exception(error);
- }
- if(errmsg != NULL) sqlite_freemem(errmsg);
- }
- int sqlite_db::sqlite_callback(void *param,int colcount,char **cols,char **colnames)
- {
- sqlite_result* result = reinterpret_cast<sqlite_result*>(param);
- sqlite_fields f;
- for(int i = 0;i < colcount;++i)
- {
- f[colnames[i]] = cols[i];
- }
- result->push_back(f);
- return 0;
- }
复制代码
sqlite_db_main.cpp//
- #include <iostream>
- #include <string>
- #include <conio.h>
- #include "sqlite_db.h"
- int main()
- {
- sqlite_db db;
- try
- {
- db.open("test.db");
- std::cout<<"sqlite version = "<<db.version()<<std::endl;
- // First we create the table
- std::string query("CREATE TABLE customers (Id integer,Name varchar(16)," \
- "City varchar(16));");
- db.exec_dml(query);
- // Add some records to the table
- query = "INSERT INTO customers VALUES (2004001,'Smith','Brussels');";
- db.exec_dml(query);
- query = "INSERT INTO customers VALUES (2004002,'Powell','London');";
- db.exec_dml(query);
- query = "INSERT INTO customers VALUES (2004003,'Brown','New-York');";
- db.exec_dml(query);
- // Read the records back
- query = "SELECT * FROM customers;";
- sqlite_result res = db.exec_sql(query);
- for(sqlite_result::size_type i = 0;i < res.size();++i)
- {
- sqlite_fields row = res[i];
- for(sqlite_fields::const_iterator it = row.begin();it != row.end();++it)
- {
- std::cout<<(*it).first<<" = "<<(*it).second<<std::endl;
- }
- std::cout<<std::endl;
- }
- }
- catch(sqlite_exception &ex)
- {
- std::cout<<ex.what()<<std::endl;
- }
- getch();
- return 0;
- }
-
复制代码
谢过先了,想用封装过的sqite3做点东东。 |
|