Page 1 of 1

problem with update

Posted: Tue Mar 19, 2019 8:07 pm
by Wolfgang
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);

Re: problem with update

Posted: Tue Mar 19, 2019 8:17 pm
by doublemax
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?

Re: problem with update

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

Code: Select all

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

Re: problem with update

Posted: Tue Mar 19, 2019 9:10 pm
by doublemax
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();

Re: problem with update

Posted: Tue Mar 19, 2019 9:16 pm
by utelle
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.