sqlite3와 wxWidgets 를 이용해서 아래와 같이 만들어 보았다.
StekiDB.h
#ifndef __STEKIDB_h__
#define __STEKIDB_h__
#include <wx/msgdlg.h>
#include <wx/string.h>
#include <wx/list.h>
#include <sqlite3.h>
class StekiDBConnection
{
private:
wxString dbName;
public:
sqlite3 *db;
StekiDBConnection(const wxString &dbName);
virtual ~StekiDBConnection();
void close();
};
class StekiDBResultSet : public wxList
{
public:
StekiDBResultSet();
~StekiDBResultSet();
};
class StekiDBStatement
{
private:
StekiDBConnection *conn;
public:
StekiDBStatement(StekiDBConnection *conn);
virtual ~StekiDBStatement();
StekiDBResultSet * executeQuery( const wxString &sql );
};
class StekiDBValue : public wxObject
{
public:
wxString *value;
wxString *colName;
public:
StekiDBValue(wxString *colName, wxString *value){ this->colName=colName; this->value=value; }
virtual ~StekiDBValue(){ free(value); free(colName); }
};
#endif
StekiDB.cpp
#include "StekiDB.h"
/**
** StekiDBConnection
**
**/
StekiDBConnection::StekiDBConnection(const wxString &dbName){
this->dbName=dbName;
char *zErrMsg = 0;
int rc;
rc = sqlite3_open(dbName, &db);
if( rc )
{
wxMessageBox(wxT("Can't open database: ") + wxString(sqlite3_errmsg(db)));
}
}
StekiDBConnection::~StekiDBConnection(){
close();
}
void StekiDBConnection::close(){
if(db!=NULL) sqlite3_close(db);
}
/**
** callback_resultset
**
**/
static int callback_resultset(void *pArg, int argc, char **argv, char **azColName)
{
StekiDBResultSet *rset=(StekiDBResultSet *)pArg;
wxList *list=new wxList();
int i;
for(i=0; i<argc; i++)
{
//printf("%s=%s, ", azColName[i], argv[i] ? argv[i] : "NULL");
wxString *colName = new wxString( azColName[i] );
wxString *str = new wxString( argv[i] ? argv[i] : "NULL" );
StekiDBValue *value = new StekiDBValue( colName, str );
list->Append( value );
}
rset->Append( list );
return 0;
}
/**
** StekiDBResultSet
**
**/
StekiDBResultSet::StekiDBResultSet(){
}
StekiDBResultSet::~StekiDBResultSet(){
this->DeleteContents(true);
}
/**
** StekiDBStatement
**
**/
StekiDBStatement::StekiDBStatement(StekiDBConnection *conn){
this->conn=conn;
}
/**
** StekiDBStatement
**
**/
StekiDBStatement::~StekiDBStatement(){
}
StekiDBResultSet * StekiDBStatement::executeQuery( const wxString &sql ){
char *zErrMsg = 0;
StekiDBResultSet *rset = new StekiDBResultSet();
int rc = sqlite3_exec(conn->db, sql.c_str(), callback_resultset, rset, &zErrMsg);
if( rc!=SQLITE_OK )
{
wxMessageBox(wxT("SQL error: ") + wxString(zErrMsg));
}
return rset;
}
Example.cpp
void MyFrame::OnMyDBReadBtn(wxCommandEvent&)
{
StekiDBConnection *conn = new StekiDBConnection("steki.db");
StekiDBStatement *stmt = new StekiDBStatement(conn);
StekiDBResultSet *rset = stmt->executeQuery( "select * from x" );
for ( wxList::Node *node = rset->GetFirst(); node; node = node->GetNext() ){
wxList *rsetlist = (wxList *)node->GetData();
for ( wxList::Node *node2=rsetlist->GetFirst();node2;node2=node2->GetNext() )
{
StekiDBValue *value = (StekiDBValue *)node2->GetData();
wtc->AppendText( *value->value );
wtc->AppendText( "," );
}
wtc->AppendText("\n");
}
free (rset);
free (stmt);
free (conn);
}
StekiDB.h
#ifndef __STEKIDB_h__
#define __STEKIDB_h__
#include <wx/msgdlg.h>
#include <wx/string.h>
#include <wx/list.h>
#include <sqlite3.h>
class StekiDBConnection
{
private:
wxString dbName;
public:
sqlite3 *db;
StekiDBConnection(const wxString &dbName);
virtual ~StekiDBConnection();
void close();
};
class StekiDBResultSet : public wxList
{
public:
StekiDBResultSet();
~StekiDBResultSet();
};
class StekiDBStatement
{
private:
StekiDBConnection *conn;
public:
StekiDBStatement(StekiDBConnection *conn);
virtual ~StekiDBStatement();
StekiDBResultSet * executeQuery( const wxString &sql );
};
class StekiDBValue : public wxObject
{
public:
wxString *value;
wxString *colName;
public:
StekiDBValue(wxString *colName, wxString *value){ this->colName=colName; this->value=value; }
virtual ~StekiDBValue(){ free(value); free(colName); }
};
#endif
StekiDB.cpp
#include "StekiDB.h"
/**
** StekiDBConnection
**
**/
StekiDBConnection::StekiDBConnection(const wxString &dbName){
this->dbName=dbName;
char *zErrMsg = 0;
int rc;
rc = sqlite3_open(dbName, &db);
if( rc )
{
wxMessageBox(wxT("Can't open database: ") + wxString(sqlite3_errmsg(db)));
}
}
StekiDBConnection::~StekiDBConnection(){
close();
}
void StekiDBConnection::close(){
if(db!=NULL) sqlite3_close(db);
}
/**
** callback_resultset
**
**/
static int callback_resultset(void *pArg, int argc, char **argv, char **azColName)
{
StekiDBResultSet *rset=(StekiDBResultSet *)pArg;
wxList *list=new wxList();
int i;
for(i=0; i<argc; i++)
{
//printf("%s=%s, ", azColName[i], argv[i] ? argv[i] : "NULL");
wxString *colName = new wxString( azColName[i] );
wxString *str = new wxString( argv[i] ? argv[i] : "NULL" );
StekiDBValue *value = new StekiDBValue( colName, str );
list->Append( value );
}
rset->Append( list );
return 0;
}
/**
** StekiDBResultSet
**
**/
StekiDBResultSet::StekiDBResultSet(){
}
StekiDBResultSet::~StekiDBResultSet(){
this->DeleteContents(true);
}
/**
** StekiDBStatement
**
**/
StekiDBStatement::StekiDBStatement(StekiDBConnection *conn){
this->conn=conn;
}
/**
** StekiDBStatement
**
**/
StekiDBStatement::~StekiDBStatement(){
}
StekiDBResultSet * StekiDBStatement::executeQuery( const wxString &sql ){
char *zErrMsg = 0;
StekiDBResultSet *rset = new StekiDBResultSet();
int rc = sqlite3_exec(conn->db, sql.c_str(), callback_resultset, rset, &zErrMsg);
if( rc!=SQLITE_OK )
{
wxMessageBox(wxT("SQL error: ") + wxString(zErrMsg));
}
return rset;
}
Example.cpp
void MyFrame::OnMyDBReadBtn(wxCommandEvent&)
{
StekiDBConnection *conn = new StekiDBConnection("steki.db");
StekiDBStatement *stmt = new StekiDBStatement(conn);
StekiDBResultSet *rset = stmt->executeQuery( "select * from x" );
for ( wxList::Node *node = rset->GetFirst(); node; node = node->GetNext() ){
wxList *rsetlist = (wxList *)node->GetData();
for ( wxList::Node *node2=rsetlist->GetFirst();node2;node2=node2->GetNext() )
{
StekiDBValue *value = (StekiDBValue *)node2->GetData();
wtc->AppendText( *value->value );
wtc->AppendText( "," );
}
wtc->AppendText("\n");
}
free (rset);
free (stmt);
free (conn);
}