-Basic C++ and sql kNowledge
-sqlite (Download)
Introduction
sqlite is an embedded sql database engine. Unlike most other sql databases,sqlite does not have a separate server process,but instead reads and writes directly to ordinary disk files. This makes sqlite an easy and good solution to store data for your application.
opening a database
The first thing you need to do,is open a database. If the database does not exist yet,it will be created.
1 | sqlite3 *database; |
sqlite3_open("Database.sqlite" ,&database); |
The first argument is the filename,the second is the sqlite3 database handle.
If everything goes right,sqlITE_OKis returned.
query's
Once the database is opened,you can actually start doing something. The following code shows how:
01
if
(sqlite3_prepare_v2(database,
"CREATE table a (b INTEGER,c INTEGER);"
{
int
cols = sqlite3_column_count(statement);
(
true
)
(
col = 0; col < cols; coL++)
char
*)sqlite3_column_text(statement,col);
//do something with it
;
The first thing you have to do is prepare the statement using sqlite3_prepare_v2(),if everything goes right,0); Font-family:monospace!important">sqlITE_OKwill be returned.
Then we actually have to execute the statement using sqlite3_step(). This function will return a value which we will need to determine our next action.
If the query is not supposed to return anything,like with CREATE tableand INSERT,we just have to finalize the statement using sqlite3_finalize()to avoID a memory leak. If the query returns colums of data,0); Font-family:monospace!important">SELECT,the function will return sqlITE_ROW. If that happens,we will need read the data.
First we will need to kNow the amount of columns it has returned. We can do this using sqlite3_column_count(). Then all we have to do is request the columns using sqlite3_column_text().
Closing the database
Now all we have to do is close the database,so the data is saved.
sqlite3_close(database);
Examples
I have written the following class to make it some simpler:
#include "Database.h"#include <iostream>Database::Database(char* filename){ database = NulL; open(filename);}Database::~Database(){}bool Database::open(char* filename){ if(sqlite3_open(filename,&database) == sqlITE_OK) return true; return false;}vector<vector<string> > Database::query(char* query){ sqlite3_stmt *statement; vector<vector<string> > results; if(sqlite3_prepare_v2(database,query,0) == sqlITE_OK) { int cols = sqlite3_column_count(statement); int result = 0; while(true) { result = sqlite3_step(statement); if(result == sqlITE_ROW) { vector<string> values; for(int col = 0; col < cols; coL++) { std::string val; char * ptr = (char*)sqlite3_column_text(statement,col); if(ptr) { val = ptr; } else val = ""; // this can be commented out since std::string val; // initialize variable 'val' to empty string anyway values.push_back(val); // Now we will never push NulL } results.push_back(values); } else { break; } } sqlite3_finalize(statement); } string error = sqlite3_errmsg(database); if(error != "not an error") cout << query << " " << error << endl; return results;}voID Database::close(){ sqlite3_close(database);}header:
view source print ?
#ifndef __DATABASE_H__
#define __DATABASE_H__ #include <string> #include <vector> #include <sqlite3.h> using
namespace
std;
classDatabase
{ public:
Database(* filename);
~Database(); boolopen(
* filename);
vector<vector<string> > query(* query);
voIDclose();
private};
#endif And the following pIEce of code shows how to use it:
#include <iostream>#include "Database.h"using namespace std;int main(){ Database *db; db = new Database("Database.sqlite"); db->query("CREATE table a (a INTEGER,b INTEGER);"); db->query("INSERT INTO a VALUES(1,2);"); db->query("INSERT INTO a VALUES(5,4);"); vector<vector<string> > result = db->query("SELECT a,b FROM a;"); for(vector<vector<string> >::iterator it = result.begin(); it < result.end(); ++it) { vector<string> row = *it; cout << "Values: (A=" << row.at(0) << ",B=" << row.at(1) << ")" << endl; } db->close();}
http://www.dreamincode.net/forums/topic/122300-sqlite-in-c/
稍作修改
总结以上是内存溢出为你收集整理的Using SQLite in C++ with Code::blocks全部内容,希望文章能够帮你解决Using SQLite in C++ with Code::blocks所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)