Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 10th, 2006, 10:32 AM   #1
Pizentios
Programming Guru
 
Pizentios's Avatar
 
Join Date: May 2004
Location: Brandon, Manitoba, Canada
Posts: 2,023
Rep Power: 7 Pizentios is on a distinguished road
Send a message via ICQ to Pizentios Send a message via MSN to Pizentios
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!
Pizentios is offline   Reply With Quote
Old Feb 10th, 2006, 10:46 AM   #2
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
Looks good man
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Feb 10th, 2006, 10:52 AM   #3
Pizentios
Programming Guru
 
Pizentios's Avatar
 
Join Date: May 2004
Location: Brandon, Manitoba, Canada
Posts: 2,023
Rep Power: 7 Pizentios is on a distinguished road
Send a message via ICQ to Pizentios Send a message via MSN to Pizentios
thanks :-)
__________________
Profanity is the one language that all programmers understand.

Check out my Blog <---updated Nov 30 2007!
Pizentios is offline   Reply With Quote
Old Feb 10th, 2006, 5:00 PM   #4
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
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
nnxion is offline   Reply With Quote
Old Feb 13th, 2006, 9:19 AM   #5
Pizentios
Programming Guru
 
Pizentios's Avatar
 
Join Date: May 2004
Location: Brandon, Manitoba, Canada
Posts: 2,023
Rep Power: 7 Pizentios is on a distinguished road
Send a message via ICQ to Pizentios Send a message via MSN to Pizentios
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!
Pizentios is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:12 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC