1

I'm trying to save some data gathered from fields in MySQL db. Text contains some Polish characters, but Livecode sends all Polish chars as '?'. Here's part of my code:

  1. Declare variable

    put the unicodeText of field "Title" into tTitle put uniEncode(tTitle, "UTF8") into tTitle

  2. Send this to db:

    put "UPDATE magazyn SET NAZWA='" & tTitle & "'" into tSQLStatement revExecuteSQL gConnectionID,tSQLStatement, "SET NAMES 'utf8'"

For example, word "łąka" is saved as "??ka". I've tried uniEncode, uniDecode, everything is going wrong.

3 Answers3

0

Don't use any encoders/decoders. They will only add to the confusion.

When trying to use utf8/utf8mb4, if you see Question Marks (regular ones, not black diamonds),

  • The bytes to be stored are not encoded as utf8. Fix this. (Getting rid of the encoders may fix it.)
  • The column in the database is CHARACTER SET utf8 (or utf8mb4). Fix this.
  • Also, check that the connection during reading is utf8. I don't know the details of "Livecode"; look in its documentation. If you can't find anything, execute this SQL after connecting: SET NAMES utf8.
Rick James
  • 135,179
  • 13
  • 127
  • 222
  • Rick, how do I use CHARACTER SET utf8 ? And if you look at my code, I've used SET NAMES. Do I have to put it in opening connection to db code? – Daniel Dochód Jul 08 '16 at 07:08
  • Look at the table schema (`SHOW CREATE TABLE` or equivalent); it may say `CHARACTER SET ...` on the column definition, or it may only say it on the table definition. – Rick James Jul 08 '16 at 16:27
0

Ensure the column in the database is set to utf8 encoding.

Starting with LiveCode 7 all text in fields is unicode, specifically UTF-16. Before you send the text out to any external file or datastore, you need to encode it as UTF-8 (or whatever format you want to store it in. Use the LiveCode textEncode() function for this:

put textEncode(field "Title","utf-8") into tTitle
put "UPDATE magazine SET nazwa = :1" into tSQLStatement
revExecuteSQL gConnectionID, tSQLStatement, "tTitle"

Note: It's also a good idea to use the :N variable substitution method to reduce the risk of SQL code injection attacks.

When you read the data from the database use textDecode to convert back to UTF-16:

put textDecode(tRawDataFromDB,"UTF-16") into old tTitle
Devin
  • 593
  • 1
  • 3
  • 8
  • All columns and db are utf8_general_ci. I've tried uniEncode, still nothing :/ – Daniel Dochód Jul 08 '16 at 07:04
  • My apologies, I made a mistake in my code. Use the textEncode function, not uniEncode, which is deprecated. I've edited my code example to correct this. – Devin Jul 09 '16 at 13:20
0

Problem solved! Here's code:

get the unicodeText of field "Title"

put unidecode(it,"polish") into tTitle

it will save polish characters in a strange version, but for downloading i'm using this code:

set the unicodetext of fld "List" to uniencode(tList,"polish")

tList variable contains all data gathered from MySQL