edit source with try-catch.
StekiDB.h
#ifndef __STEKIDB_h__
#define __STEKIDB_h__
#include <wx/string.h>
#include <wx/list.h>
#include <sqlite3.h>
class StekiDBException
{
private:
wxString msg;
public:
StekiDBException(const wxString &msg){ this->msg.Append(msg); };
virtual ~StekiDBException(){};
const wxString getMessage() const {
return msg;
}
};
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 )
{
throw ( StekiDBException(wxString("Can't open database: ").Append( wxString(sqlite3_errmsg(db)))) );
// 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(){
}
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 )
{
throw ( StekiDBException(wxString("SQL error: ").Append( wxString(zErrMsg))) );
// wxMessageBox(wxT("SQL error: ") + wxString(zErrMsg));
}
return rset;
}
Example.cpp
void MyFrame::OnMyDBReadBtn(wxCommandEvent&)
{
StekiDBConnection *conn;
StekiDBStatement *stmt;
StekiDBResultSet *rset;
try {
conn = new StekiDBConnection("steki.db");
stmt = new StekiDBStatement(conn);
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");
}
} catch (StekiDBException e) {
wxMessageBox(e.getMessage());
}
if(rset!=NULL) free (rset);
if(stmt!=NULL) free (stmt);
if(conn!=NULL) free (conn);
}