wxSqlite3 successive commits  [SOLVED]

Talk here about issues with one of the components hosted at wxCode, or suggest features for it.
raananb
Super wx Problem Solver
Super wx Problem Solver
Posts: 295
Joined: Fri Oct 27, 2006 4:35 pm
Location: Paris, France
Contact:

wxSqlite3 successive commits

Postby raananb » Wed Jan 25, 2012 9:38 pm

the code below executes with no errors.
However, when executed twice in a row for two differnet ordinals (with a very small time interval), the first record is deleted, but the second record is not deleted and will show up when the db is reloaded. How do I make sure that the second record is deleted?

wxString command = wxT("DELETE FROM Mouvements WHERE Ordinal = *");
command.Replace(wxT("*"),wxString::Format(wxT("%d"),ordinal));
accountsDB->ExecuteUpdate(command);
wxSQLite3Transaction t(accountsDB);
t.Commit();



(my sql experience is obviously very limited...)

User avatar
doublemax
Moderator
Moderator
Posts: 11527
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxSqlite3 successive commits

Postby doublemax » Wed Jan 25, 2012 11:55 pm

Code: Select all

wxSQLite3Transaction t(accountsDB);
This line should be above the ExecuteUpdate()
Use the source, Luke!

raananb
Super wx Problem Solver
Super wx Problem Solver
Posts: 295
Joined: Fri Oct 27, 2006 4:35 pm
Location: Paris, France
Contact:

Re: wxSqlite3 successive commits

Postby raananb » Thu Jan 26, 2012 7:15 am

doublemax wrote:

Code: Select all

wxSQLite3Transaction t(accountsDB);
This line should be above the ExecuteUpdate()


Makes no difference.

utelle
Moderator
Moderator
Posts: 767
Joined: Tue Jul 05, 2005 10:00 pm
Location: Cologne, Germany
Contact:

Re: wxSqlite3 successive commits  [SOLVED]

Postby utelle » Thu Jan 26, 2012 8:30 am

raananb wrote:the code below executes with no errors.
However, when executed twice in a row for two differnet ordinals (with a very small time interval), the first record is deleted, but the second record is not deleted and will show up when the db is reloaded. How do I make sure that the second record is deleted?

Code: Select all

wxString command = wxT("DELETE FROM Mouvements WHERE Ordinal = *");
command.Replace(wxT("*"),wxString::Format(wxT("%d"),ordinal));
accountsDB->ExecuteUpdate(command);
wxSQLite3Transaction t(accountsDB);
t.Commit();


Without seeing the code how those statements are executed a second time it's hard to tell which actions take place on the database in the end. It could be that the compiler "optimized" your code resulting in wrong behaviour. The wxSQLite3Transaction constructor implicitly opens a transaction.

I would recommend to explicitly start a transaction (using accountsDB->Begin()) and to explicitly commit (using accountsDB->Commit()) instead o wxSQLite3Transaction.

Additionally I would recommend to use a prepared statement (wxSQLite3Statement) to bind the values of ordinal to your SQL statement.

Regards,

Ulrich

raananb
Super wx Problem Solver
Super wx Problem Solver
Posts: 295
Joined: Fri Oct 27, 2006 4:35 pm
Location: Paris, France
Contact:

Re: wxSqlite3 successive commits

Postby raananb » Thu Jan 26, 2012 9:48 am

Cleaning up the 'ordinal' management solved this problem.

Thanks.


Return to “wxCode”

Who is online

Users browsing this forum: No registered users and 2 guests