![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programming Guru
![]() ![]() |
SQLite3
Hey,
just finished my first c++ program that uses sqlite3. I made a class to make things a little neater. Here's the sqlite class i made: #include <sqlite3.h>
#include <string>
#include <vector>
class sql
{
public:
sql();
sql(const char *filename);
~sql();
bool execute(std::string query);
int get_nrow();
int get_ncol();
bool get_result();
std::vector<std::string> vcol_head;
std::vector<std::string> vdata;
const char *error;
private:
sqlite3 *db;
int result;
int row;
int col;
char **qresults;
char *error2;
};
sql::sql()
{
std::string filename = "database.db";
sql(filename.c_str());
}
sql::sql(const char *filename)
{
//this defaults to a file named database.db
result = sqlite3_open(filename, &db);
if ( result )
{
error = sqlite3_errmsg(db);
}
}
sql::~sql()
{
sqlite3_close(db);
}
bool sql::execute(std::string query)
{
result = sqlite3_get_table(db, query.c_str(), &qresults, &row, &col, &error2);
error = (const char *) error2;
if(vcol_head.size()<0) { vcol_head.clear(); }
if(vdata.size()<0) { vdata.clear(); }
if (result == SQLITE_OK)
{
for(int i=0; i < col; ++i)
{
vcol_head.push_back(qresults[i]); /* First row heading */
}
for(int i=0; i < col*row; ++i)
{
vdata.push_back(qresults[col+i]);
}
sqlite3_free_table(qresults);
return true;
}
else
{
return false;
}
}
int sql::get_nrow()
{
return row;
}
int sql::get_ncol()
{
return col;
}
bool sql::get_result()
{
if (result == SQLITE_OK)
{
return true;
}
else
{
return false;
}
}here's a program that uses the class to do a simple select. #include "sqlite_class.h"
#include <iostream>
#include <iterator>
#include <vector>
#include <string>
int main()
{
const char *filename = "test.db";
sql conn (filename);
std::string my_query;
if (conn.get_result())
{
//connected to the filename ok.
my_query = "SELECT * FROM tbl1;";
if (conn.execute(my_query))
{
//results ran ok.
//output number of rows and columns.
std::cout << "Number of rows: " << conn.get_nrow() << std::endl;
std::cout << "Number of cols: " << conn.get_ncol() << std::endl;
std::cout << "----------------------" << std::endl;
if( conn.vcol_head.size() > 0 )
{
std::cout << "Headings" << std::endl;
copy(conn.vcol_head.begin(), conn.vcol_head.end(), std::ostream_iterator<std::string>(std::cout,"\t"));
std::cout << std::endl << std::endl;
std::cout << "Data" << std::endl;
copy(conn.vdata.begin(), conn.vdata.end(), std::ostream_iterator<std::string>(std::cout,"\t"));
std::cout << std::endl;
}
else
{
std::cout << "There was no data returned." << std::endl;
}
}
else
{
std::cout << conn.error << std::endl;
return 1;
}
}
else
{
std::cout << conn.error << std::endl;
return 1;
}
return 0;
}Pretty easy to use that sqlite stuff. no need for a server, good for small websites (it has php support), or programs that don't need a heavy database backend. Only down fall (atleast in my eyes) is that it doesn't check data type when inserting. That means you could put a string into a int. However it is in the public domain, so it free to use for all projects including ones you make money off of :-)
__________________
Profanity is the one language that all programmers understand. Check out my Blog <---updated Nov 30 2007! |
|
|
|
|
|
#2 |
|
Programming Guru
![]() ![]() ![]() |
Looks good man
![]()
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
|
|
#3 |
|
Programming Guru
![]() ![]() |
thanks :-)
__________________
Profanity is the one language that all programmers understand. Check out my Blog <---updated Nov 30 2007! |
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Very nice, simple yet very powerful is what I love.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
#5 |
|
Programming Guru
![]() ![]() |
yeah, i like sqlite, it's one of the easyest db systems that i have used. It's nice for smaller apps, or apps that don't handle 10000's of rows of data because you don't need the whole server client setup.
__________________
Profanity is the one language that all programmers understand. Check out my Blog <---updated Nov 30 2007! |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|