Saturday, July 5th 2008, 2:06pm UTC+1

You are not logged in.

  • Login
  • Register

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains how this page works. You must be registered before you can use all the page's features. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

pfid

Beginner

1

Wednesday, May 14th 2008, 10:00am

Qt + MAC + mysql + german umlaute

Hi,

i've got some problems with my program, when compiling and using the program on MAC. under windows and linux it works without problems.
the program uses a mysql database for displaying and storing data. qsql* classes are used for all the mysql stuff.

now the problem is, there is one input field, a qtextedit, which can be saved to the database when the user clicks the save button. the saving is done via sqlquery("update ..."). on linux+windows this works without problems. on MAC, however, i encounter problems as soon as there is any special characters (like öäü) in the text. the text will be saved to the database, but it will be cut off at the position of the first special character.
so, when you enter a text like "this is my ä text", the program will save "this is my " in the database. you can do the same on linux and windows, there the program will save "this is my ä text" correctly.

for this specific input field i already found a solution. when executing the sql query with qtextedit->text() as parameter, i added a ->toUft8(), so the qtextedit contents will be converted to utf8 before saving to database. this way you can again save special characters on MAC too.

but, since the programs main display is a QSqlRelationalTableModel + qtableview, i still have the same problems. records will be saved with the model->submitAll() method, and there is no way of implementing ->toUtf8(). so again, when someone inputs special characters in the tableview, they wont be saved.

in addition to this, special characters that are already saved in the database, will be selected and displayed correctly even on MAC. so when you save records with äöü on windows, you can see them correctly in the tableview on mac.

on the database side all characterset properties are set to utf8. " .. $> locale" on MAC shows utf8 encoding. in the code of the program i even set

Source code

1
2
QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8")); 
   QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));


when i exec the query "show variables" on MAC from within the program, i get these results:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
+---------------------------------+--------------------------------------------------------------+
| Variable_name                   | Value                                                        |
+---------------------------------+--------------------------------------------------------------+
| character_set_client            | utf8                                                         |
| character_set_connection        | utf8                                                         |
| character_set_database          | utf8                                                         |
| character_set_filesystem        | binary                                                       |
| character_set_results           | utf8                                                         |
| character_set_server            | utf8                                                         |
| character_set_system            | utf8                                                         |
| collation_connection            | utf8_general_ci                                              |
| collation_database              | utf8_general_ci                                              |
| collation_server                | utf8_general_ci                                              |


so, everything seems to be set to utf8. so whats wrong then? can someone help me please? :(
  • Go to the top of the page

2

Wednesday, May 14th 2008, 11:24am

execute the query:
show create table yourTable;
to see what charset your table uses
Nicolas
  • Go to the top of the page

pfid

Beginner

3

Wednesday, May 14th 2008, 12:43pm

creates a table with utf8 characterset & collation both on mac and linux.
  • Go to the top of the page

4

Wednesday, May 14th 2008, 3:37pm

show the code to prepare the update query
Nicolas
  • Go to the top of the page

pfid

Beginner

5

Thursday, May 15th 2008, 7:49am

Quoted

Originally posted by Nicolas SOUCHON
show the code to prepare the update query


i dont update the table model/view via seperate queries. instead, the records are edited through the tableview, and saved via model->submitAll();

as for the before mentioned qtextedit (which i fixed by adding .toUtf8()), this is the update query:

preparing:

Source code

1
2
3
4
5
6
update = new QSqlQuery;

   update->prepare("update table set "
                   "COMMENTS = :COMMENTS, "
                   "ACT_BEGIN = :ACT_BEGIN, "
                   ... [a lot of fields here] );


saving:

Source code

1
2
3
4
5
6
7
update->bindValue(":COMMENTS", textEditComments->toHtml().toUtf8());
   update->bindValue(":ACT_BEGIN", getDateOf(dateEditActBegin));
   ... [ alot of binds here]

  if (!update->exec()) 
  {
     showSqlError(...
  • Go to the top of the page

6

Thursday, May 15th 2008, 8:43am

try something like

Source code

1
2
3
4
5
6
7
QString te = textEditComments->toHtml();
QString qs;
qs.sprintf(
    "update table set comment=%s%s ...;", 
    (te == "") ? "" : "0x", te.toUtf8().toHex().data(), 
    ... );
update.exec( qs );

adapt to prepared query wich I have not used
Nicolas
  • Go to the top of the page

pfid

Beginner

7

Thursday, May 15th 2008, 8:47am

Quoted

Originally posted by Nicolas SOUCHON
try something like

Source code

1
2
3
4
5
6
7
QString te = textEditComments->toHtml();
QString qs;
qs.sprintf(
    "update table set comment=%s%s ...;", 
    (te == "") ? "" : "0x", te.toUtf8().toHex().data(), 
    ... );
update.exec( qs );

adapt to prepared query which I have not used


the update-query already works without problems when using textEdit->toHtml().toUtf8().

the table view/model doesn't work with special characters.
  • Go to the top of the page

Rate this thread