Hi,
I'm (fairly) new to C++ and also to using dlls and SQLite etc. etc.
Ok, so I have a class which represents my database connection. When the
open() method is called, it should open the SQLite database. This works.
Next, I wnt to use the PRAGMA user_version command to see if the database
version matches the app version. (If not, then I intend to prompt the user
and offer to upgrade...)
So far so good, however when I try to actually do it, the file opens ok
(and I can close it), however when the prepare function is called, I get a
GPF. This happens with both the prepare and the prepare_V2 functions. I
know it is that line, because if I comment it out....!
pologies for the poor coding (I'm learning!) but could anyone tell me what
I'm doing wrong, please?
Many thanks
--
Paul
Code:-
dbConnection.h
#include "sqlite3.h"
typedef int (*SQLCALLBACK)(void *,int,char **,char **);
typedef int (*SQLOPEN)(const char *,sqlite3 **);
typedef int (*SQLPREPARE)(sqlite3 *db,const char *zSql,int
nBytes,sqlite3_stmt **ppStmt,const char **pzTail);
typedef int (*SQLSTEP)(sqlite3_stmt **ppStmt);
typedef int (*SQLFINALIZE)(sqlite3_stmt **ppStmt);
typedef char *(*SQLERRMSG)(sqlite3 *);
typedef void (*SQLCLOSE)(sqlite3 *);
class dbConnection{
public:
dbConnection();
bool isValid();
bool isInit();
bool openFile();
bool open(wxString filename);
bool closeFile();
wxString getFileName();
int versionCallback(void *NotUsed, int argc, char **argv, char
**azColName);
private:
SQLOPEN sqlOpenAdd;
SQLPREPARE sqlPrepareAdd;
SQLSTEP sqlStepAdd;
SQLFINALIZE sqlFinalizeAdd;
SQLERRMSG sqlErrMsgAdd;
SQLCLOSE sqlCloseAdd;
sqlite3 *db;
HINSTANCE hinstLib;
bool initialized;
bool dbValid;
wxString path;
};
dbConnection.cpp
#include <stdio.h>
#include "markbookplus.h"
#include "dbconnection.h"
dbConnection::dbConnection(){
db = NULL;
initialized = false;
dbValid = false;
hinstLib = LoadLibrary(TEXT("sqlite3.dll"));
if (hinstLib != NULL)
{
sqlOpenAdd = (SQLOPEN) GetProcAddress(hinstLib,
TEXT("sqlite3_open"));
initialized = (sqlOpenAdd != NULL);
sqlPrepareAdd = (SQLPREPARE) GetProcAddress(hinstLib,
TEXT("sqlite3_prepare"));
initialized &= (sqlPrepareAdd != NULL);
sqlStepAdd = (SQLSTEP) GetProcAddress(hinstLib,
TEXT("sqlite3_step"));
initialized &= (sqlStepAdd != NULL);
sqlFinalizeAdd = (SQLFINALIZE) GetProcAddress(hinstLib,
TEXT("sqlite3_finalize"));
initialized &= (sqlFinalizeAdd != NULL);
sqlErrMsgAdd = (SQLERRMSG) GetProcAddress(hinstLib,
TEXT("sqlite3_errmsg"));
initialized &= (sqlErrMsgAdd != NULL);
sqlCloseAdd = (SQLCLOSE) GetProcAddress(hinstLib,
TEXT("sqlite3_close"));
initialized &= (sqlCloseAdd != NULL);
}
}
bool dbConnection::isInit(){
return initialized;
}
bool dbConnection::isValid(){
return dbValid;
}
bool dbConnection::openFile(){
wxFileDialog dialog(NULL,
wxT("Choose MarkbookPlus Data
file"),
wxEmptyString,
wxEmptyString,
wxT("MarkBookPlus Data Files
(*.mbd)|*.mbd"),
wxOPEN);
if (dialog.ShowModal() == wxID_OK){
closeFile();
path = dialog.GetPath();
return open(path);
}
return false;
}
bool dbConnection::open(wxString filename){
// char *zErrMsg = 0;
int rc;
sqlite3 *newdb = NULL;
sqlite3_stmt **ppStmt;
const char **pzTail;
rc = (sqlOpenAdd) (filename.c_str(),&newdb);
if(rc != SQLITE_OK){
wxString msg;
msg.Printf(wxT("Can't open database:
%s"),(sqlErrMsgAdd)(newdb));
wxMessageBox(msg, wxT("File Open Failure"),
wxOK | wxICON_ERROR, NULL);
return false;
}
wxString getDBVersion = "PRAGMA user_version";
rc = (sqlPrepareAdd)(newdb,
getDBVersion.c_str(),getDBVersion.length(),ppStmt,pzTail);
if(rc != SQLITE_OK){
wxString msg;
msg.Printf(wxT("Can't process database:
%s"),(sqlErrMsgAdd)(newdb));
wxMessageBox(msg, wxT("File Open Failure"),
wxOK | wxICON_ERROR, NULL);
return false;
}
// rc = (sqlStepAdd)(ppStmt);
// if(rc == SQLITE_ROW){
// }else{
// wxString msg;
// msg.Printf(wxT("Can't process database:
%s"),(sqlErrMsgAdd)(newdb));
// wxMessageBox(msg, wxT("File Open Failure"),
// wxOK | wxICON_ERROR, NULL);
// return false;
// }
rc = (sqlFinalizeAdd)(ppStmt);
if(rc != SQLITE_OK){
wxString msg;
msg.Printf(wxT("Unable to release database statement:
%s"),(sqlErrMsgAdd)(newdb));
wxMessageBox(msg, wxT("File Open Failure"),
wxOK | wxICON_ERROR, NULL);
return false;
}
// rc = (sqlExecAdd)(newdb, "PRAGMA user_version", versionCallback, 0,
&zErrMsg);
// if( rc!=SQLITE_OK ){
// MessageBox(NULL, zErrMsg, "SQL error!",
// MB_ICONEXCLAMATION | MB_OK);
// }else{
// if(db != NULL){
// (sqlCloseAdd) (db);
// }
// db = newdb;
// HWND hStatus;
// hStatus = GetDlgItem(hwnd,IDC_MAIN_STATUS);
// }
db = newdb;
dbValid = true;
return true;
}
bool dbConnection::closeFile(){
if(dbValid){
(sqlCloseAdd)(db);
}
dbValid = false;
return true;
}
wxString dbConnection::getFileName(){
return path;
}
-----------------------------------------------------------------------------
To unsubscribe, send email to [EMAIL PROTECTED]
-----------------------------------------------------------------------------