[Interest] How to save QTextEdit content by QSqlQuery

Yves Bailly yves.bailly at sescoi.fr
Mon Jan 28 08:36:09 CET 2013


Le 26/01/2013 00:51, M. Bashir Al-Noimi a écrit :
> I want to save the content of QTextEdit to the database through QSqlQuery... how can I do that?
>
> Running textEdit->toHtml() won't work because the returned string has many illegal characters to
> execute into sql.

Assuming your're using some low-level SQL function and not QSqlQuery and the likes to
"build" the SQL command to execute, here's something that works, albeit probably not
very efficient:
QString html = textEdit->toHtml();
QByteArray utf8 = html.toUtf8();
QByteArray base64 = utf8.toBase64();
char const* data = base64.constData();
sql_writer_function(data);

To read the data back:
char const* data = sql_reader_function();
QByteArray base64(data);
QByteArray utf8 = QByteArray::fromBase64(base64);
QString html = QString::fromUtf8(utf8.constData());

A drawback is that your text is not stored in a human-readable way, but you get the idea.
You can also store a BLOB if your database backend supports this.

Regards,

-- 
      /- Yves Bailly - Software developer   -\
      \- Sescoi R&D  - http://www.sescoi.fr -/
"The possible is done. The impossible is being done. For miracles,
thanks to allow a little delay."



More information about the Interest mailing list