Sqlite3 and wxwidgets callbacks Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
giro
Knows some wx things
Knows some wx things
Posts: 41
Joined: Fri Sep 01, 2006 7:47 am

Sqlite3 and wxwidgets callbacks

Post by giro »

I have this portion of code

Code: Select all


static int Callback(void *pArg, int argc, char **argv, char **columnNames){
  return 0;
}


MyFrame::MyFrame( wxWindow *parent, wxWindowID id, const wxString &title,
    const wxPoint &position, const wxSize& size, long style ) :
    wxFrame( parent, id, title, position, size, style )
{
    
	

	wxStandardPaths fn;
	wxString db_arxiu = fn.GetDataDir()+ _T("\\test.db");
	char *errMsg=0;
	int rc;
	sqlite3 *db;
	sqlite3_open(db_arxiu.ToAscii(), &db); // creem db
    rc = sqlite3_exec(db,"CREATE TABLE grups(id INTEGER PRIMARY KEY, grup CHAR(255), subgrup INTEGER );",Callback,0, &errMsg);
	if(rc!=SQLITE_OK)
	{
		wxLogMessage(_T("ERROR CREAT TAULES"));
		sqlite3_free(errMsg);
	}
	
	
	sqlite3_close(db);

	
}
The problem is that if taht i cant access to Myframe class in Callback function


If i define

Code: Select all

 int MyFrame::Callback(void *pArg, int argc, char **argv, char **columnNames){
  return 0;
}
This make me and error because sqlite3_exec wait for a int function and compiler say me that this is int (_cdle *)

Any idea how to solve it?

thk.
Giro.
elmo
Experienced Solver
Experienced Solver
Posts: 56
Joined: Mon Dec 26, 2005 9:23 pm
Location: Poland, Warsaw

Post by elmo »

Code: Select all

int sqlite3_exec(
  sqlite3*,                     /* An open database */
  const char *sql,              /* SQL to be executed */
  sqlite_callback,              /* Callback function */
  void *,                       /* 1st argument to callback function */
  char **errmsg                 /* Error msg written here */
);
Use the fourth argument to pass the pointer.
I.e.

Code: Select all

static int Callback(void *frame, int argc, char **argv, char **columnNames){
  
  ((MyFrame*) frame)->someFrameMethod();
  return 0;
}

[...]
rc = sqlite3_exec(db,"CREATE TABLE grups(id INTEGER PRIMARY KEY, grup CHAR(255), subgrup INTEGER );",Callback,this, &errMsg)
[...]
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Post by doublemax »

there's also wxSqlite which might make your life a little easier.
Use the source, Luke!
giro
Knows some wx things
Knows some wx things
Posts: 41
Joined: Fri Sep 01, 2006 7:47 am

Post by giro »

I create this.

Code: Select all

static int Callback(void *pArg, int argc, char **argv, char **columnNames){
	((MyFrame*) pArg)->someFrameMethod();
  return 0;
}

void MyFrame::someFrameMethod(void)
{

	wxMessageBox(_T("yupi"));
}
and i execute

Code: Select all

rc = sqlite3_exec(db,"CREATE TABLE grups(id INTEGER PRIMARY KEY, grup CHAR(255), subgrup INTEGER );",Callback,this, &errMsg);
but messagebox never appear. Not error compiling, but dont work.

thk.
Giro.
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Post by doublemax »

i don't know the inner workings of sqlite, but i guess the callback is for retrieving data, and therefore won't be called for a "CREATE TABLE" command.
Use the source, Luke!
elmo
Experienced Solver
Experienced Solver
Posts: 56
Joined: Mon Dec 26, 2005 9:23 pm
Location: Poland, Warsaw

Post by elmo »

http://www.sqlite.org/capi3ref.html#sqlite3_exec wrote:If one or more of the SQL statements are queries, then the callback function specified by the 3rd argument is invoked once for each row of the query result.
I think that create table does not return any row -> there is no need to execute callback function.
doublemax wrote:there's also wxSqlite which might make your life a little easier.
And slower ;] (I tell this from my experience).

edit:
doublemax was a bit faster ;]
Last edited by elmo on Wed Oct 25, 2006 9:48 pm, edited 1 time in total.
giro
Knows some wx things
Knows some wx things
Posts: 41
Joined: Fri Sep 01, 2006 7:47 am

Post by giro »

OKey, is possible Create table dont return

But if i execute

Code: Select all

rc = sqlite3_exec(db,"SELECT * FROM grups;",Callback,this, &errMsg);
In errMsg i have "library routine called out of sequence"

Giro.
giro
Knows some wx things
Knows some wx things
Posts: 41
Joined: Fri Sep 01, 2006 7:47 am

Post by giro »

Finaly work, i make a mistake i close db, before make select.

I am dummy, sorry.

Giro.
Post Reply