wxSQLite3 craches in my case ...

Talk here about issues with one of the components hosted at wxCode, or suggest features for it.
Post Reply
dkaip
Super wx Problem Solver
Super wx Problem Solver
Posts: 334
Joined: Wed Jan 20, 2010 1:15 pm

wxSQLite3 craches in my case ...

Post by dkaip »

Hello.
I have made a table...

Code: Select all

wxString leftCtrlData =TableName+wxT(" (id INTEGER PRIMARY KEY AUTOINCREMENT unique , human_description blob unique, code int unique, function blob unique, description blob )");
db->ExecuteUpdate(leftCtrlData);
Inserting a row all are ok.

Code: Select all

    wxString sql=wxT("insert into ");
    sql+=TableName;
    sql+=wxT(" ( human_description , code , function , description ) values ('");
    sql+="human_description";
    sql+=wxT("' , '");
    sql+=wxString::Format(wxT("%d"),0);
    sql+=wxT("' , '");
    sql+="funct";
    sql+=wxT("' , '");
    sql+="description";
    sql+=wxT("')");
    db->ExecuteUpdate(sql);
Ok until now, the row exist.
But if i will try the same, app crashes.
If wxSQLite3 see that some field is same with others must return without crash?
Thank you.
Jim
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxSQLite3 craches in my case ...

Post by doublemax »

wxSQLite3 throws exceptions. Catch it and the error message will tell you what's wrong.

Code: Select all

wxString leftCtrlData =TableName+wxT(" (id INTEGER PRIMARY KEY AUTOINCREMENT unique , human_description blob unique, code int unique, function blob unique, description blob )");
However, this should probably start with "UPDATE tablename ...".

The rest of the statement looks like a "CREATE TABLE ..." statement through.
Use the source, Luke!
dkaip
Super wx Problem Solver
Super wx Problem Solver
Posts: 334
Joined: Wed Jan 20, 2010 1:15 pm

Re: wxSQLite3 craches in my case ...

Post by dkaip »

Sorry, this is all code...
wxString sql=wxT("CREATE TABLE if not exists ");
sql+=TableName+wxT(" (id INTEGER PRIMARY KEY AUTOINCREMENT unique , human_description blob unique, code int unique, function blob unique, description blob )");

///////////////////////////////////////

wxString sql=wxT("insert into ");
sql+=TableName;
sql+=wxT(" ( human_description , code , function , description ) values ('");
sql+=human_description;
sql+=wxT("' , '");
sql+=wxString::Format(wxT("%d"),code);
sql+=wxT("' , '");
sql+=funct;
sql+=wxT("' , '");
sql+=description;
sql+=wxT("')");
db->ExecuteUpdate(sql);
db->ExecuteUpdate(sql);//here the crash ...
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxSQLite3 craches in my case ...

Post by doublemax »

Code: Select all

db->ExecuteUpdate(sql);
db->ExecuteUpdate(sql);//here the crash ... 
That code doesn't make sense either, i guess it's a bad copy-paste.

Anyway, catch the exception and check the error. The sample that comes with wxSQLite3 shows how to do it.

I would also suggest to use prepared statements, then you don't have to worry about characters in variables like "human_description" that might need to get escaped.
Use the source, Luke!
utelle
Moderator
Moderator
Posts: 1125
Joined: Tue Jul 05, 2005 10:00 pm
Location: Cologne, Germany
Contact:

Re: wxSQLite3 craches in my case ...

Post by utelle »

dkaip wrote: Tue Jun 28, 2022 7:49 pm

Code: Select all

wxString sql=wxT("insert into ");
sql+=TableName;
sql+=wxT(" ( human_description , code , function , description ) values ('");
sql+=human_description;
sql+=wxT("' , '");
sql+=wxString::Format(wxT("%d"),code);
sql+=wxT("' , '");
sql+=funct;
sql+=wxT("' , '");
sql+=description;
sql+=wxT("')");
db->ExecuteUpdate(sql);
db->ExecuteUpdate(sql);//here the crash ...
  1. As doublemax already told you wxSQLite3 uses exceptions to report errors. Therefore you should always enclose calls to wxSQLite3 in a try-catch block (see the minimal sample coming with wxSQLite3 how to do that).
  2. Concatenating an SQL statement from string values is really bad style, increases the risk of getting exceptions due to invalid SQL syntax (a simple apostrophe in one of the data values can already cause trouble), and finally makes your application vulnerable to SQL injection. You should always use prepared SQL statements to insert data into the database, except maybe for trivial use cases with constant data.
  3. It is not surprising that the second call to ExecuteUpdate fails (and crashes due to missing try-catch block): you execute exactly the same SQL statement a second time. This will inevitably violate the unique constraints you defined in your CREATE TABLE statement - this is an error causing an exception to be thrown.
  4. If you know in advance that your INSERT statements can violate the table's unique constraints, you may specify how SQLite should resolve such violations. For example, you could tell SQLite to ignore errors by writing "INSERT OR IGNORE ..." in your SQL statement. The problem with such an approach is that an error situation will then be ignored, even if the reason is something other than a unique constraint violation.
  5. Your assumption "If wxSQLite3 see that some field is same with others must return without crash?" is completely wrong. wxSQLite3 does not inspect the error messages returned by SQLite - that is the task of your application! And you have to use try-catch blocks to do that.
dkaip
Super wx Problem Solver
Super wx Problem Solver
Posts: 334
Joined: Wed Jan 20, 2010 1:15 pm

Re: wxSQLite3 craches in my case ...

Post by dkaip »

Thank you, i catch and i am taking ...
I know the bad of use in my code...
error 19
constraint failed[2067]: UNIQUE constraint failed: function
Very important the answer for me.
Thank you
Jim
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxSQLite3 craches in my case ...

Post by doublemax »

dkaip wrote: Tue Jun 28, 2022 9:24 pm Thank you, i catch and i am taking ...
I know the bad of use in my code...
error 19
constraint failed[2067]: UNIQUE constraint failed: function
Very important the answer for me.
Thank you
Jim
I haven't found it in the sqlite docs, but i doubt a BLOB type can be UNIQUE. Do you really need this?
Use the source, Luke!
dkaip
Super wx Problem Solver
Super wx Problem Solver
Posts: 334
Joined: Wed Jan 20, 2010 1:15 pm

Re: wxSQLite3 craches in my case ...

Post by dkaip »

I haven't found it in the sqlite docs, but i doubt a BLOB type can be UNIQUE. Do you really need this?
No, i will change to TEXT.
Thank you
Jim
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7458
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: wxSQLite3 craches in my case ...

Post by ONEEYEMAN »

doublemax,
We are talking SQLite and not some nbig client/server DB.
Nevertheless using BLOB as UNIQUE field is a really bad design.

Thank you.
Post Reply