problem with update Topic is solved

In this forum you can discuss database related issues which can be wxWidgets related, but also generic in nature.
Post Reply
Wolfgang
I live to help wx-kind
I live to help wx-kind
Posts: 174
Joined: Mon Jan 28, 2019 8:22 am

problem with update

Post by Wolfgang » Tue Mar 19, 2019 8:07 pm

I get an exception, when I execute it.

Inside the richtext should be something like the file in the zip.

Code: Select all

	wxString oldt;
	wxRichTextXMLHandler *handler{ new wxRichTextXMLHandler };
	wxStringOutputStream out;
	
	handler->SetFlags(handler->GetFlags() | wxRICHTEXT_HANDLER_INCLUDE_STYLESHEET);
	m_befe2->GetBuffer().AddHandler(handler);
	m_befe2->GetBuffer().SaveFile(out, wxRICHTEXT_TYPE_XML);
	oldt= out.GetString().ToStdWstring();
	
		wxSQLite3StatementBuffer bufSQL;
		bufSQL.Format("update content set data2 = '%Q' where topic_id='1';", oldt);
Attachments
test3.zip
(4.33 KiB) Downloaded 46 times

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

Re: problem with update

Post by doublemax » Tue Mar 19, 2019 8:17 pm

I get an exception, when I execute it.
Catch the exception and print out the error message. It will tell you exactly what's wrong.

BTW: How did you convert the RTF to XML?
Use the source, Luke!

Wolfgang
I live to help wx-kind
I live to help wx-kind
Posts: 174
Joined: Mon Jan 28, 2019 8:22 am

Re: problem with update

Post by Wolfgang » Tue Mar 19, 2019 8:53 pm

Solved with

Code: Select all

	wxInt16 test = oldt.Replace("'", "''");
Sqlite printf did not work

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

Re: problem with update

Post by doublemax » Tue Mar 19, 2019 9:10 pm

wxSQLite3Statement.Bind(...) would have been the most reliable solution, no escaping necessary.

Snippet taken from the wxSQLite3 sample:

Code: Select all

    db.ExecuteUpdate("drop table emp;");
    db.ExecuteUpdate("create table emp(empno int, empname char(20), salary double);");
    tmStart = time(0);
    db.Begin();

    wxSQLite3Statement stmt2 = db.PrepareStatement("insert into emp values (?, ?, ?);");
    for (i = 0; i < nRowsToCreate; i++)
    {
      char buf[16];
      sprintf(buf, "EmpName%06d", i);
      stmt2.Bind(1, i);
      stmt2.Bind(2, buf);
      stmt2.Bind(3, (double) (i + 0.5));
      stmt2.ExecuteUpdate();
      stmt2.Reset();
    }

    db.Commit();
Use the source, Luke!

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

Re: problem with update

Post by utelle » Tue Mar 19, 2019 9:16 pm

Wolfgang wrote:
Tue Mar 19, 2019 8:53 pm
Solved with

Code: Select all

	wxInt16 test = oldt.Replace("'", "''");
Sqlite printf did not work
It would work if used correctly. There is a difference in the format codes %q and %Q. As described in the SQLite format code Q documentation, "The %Q substitution type also puts single-quotes on both ends of the substituted string." Format code %q would have worked, but using %Q resulted in doubled single quotes at both ends.

However, the much better, more efficient approach would be to bind parameter values to a prepared SQL statement.

Post Reply