views:

97

answers:

2

Trying to insert values with Unicode Chars into a MySQL-database using Delphi 2010 and TADOConnection with no luck.

Connection with ODBC

Provider=MSDASQL.1;Persist Security Info=False;Data Source=mysrc;Initial Catalog=mydb

The SQL command:

INSERT INTO myTable (aCol) VALUES('Russian: русский язык')

Tried inserting it directly with

TADOConnection.Execute(SQL)

It only ends up in the database as "Russian: ??????? ????"

Also tried the method suggested here: http://www.3delite.hu/Object%20Pascal%20Developer%20Resources/delphiunicodemysqltutorial.html

With TADOQuery do
   begin
   SQL.Clear;
   SQL.Add('INSERT INTO myTable (aCol) VALUES(:p));
   Parameters.ParamByName('p').DataType := ftWideString;
   Parameters.ParamByName('p').Value := 'Russian: русский язык';
   ExecSQL;
end;

Making this in code doesn't work at all for me, only if I add parameters in designtime, but then it's still the same result in the database with questionmarks all over.

A: 

I believe this PHP script executes a SET NAMES utf8 query first

Col. Shrapnel
Probably, but I can't run that command using an ADO ODBC connection, the drivers says "not supported command"
Bulan
+1  A: 

It seems TADOConnection doesn't support Unicode, at least I can't get it to work.

If I instead use dbExpress TSQLConnection and TSQLQuery to insert to the database it works as intended. But I must do it with Parameters and not with directly with an INSERT command

                with qTarget.Params.ParamByName('p' + IntToStr(i)) do
                begin
                    DataType    := ftWideString;
                    Value           := Fields[i].AsWideString;
                end;
                qTarget.ExecSQL;
Bulan
Actually you should do: Value := Field[i].Value; As that would accept null values. If nulls are not valid then you don't need to set DataType to ftWideString, it will be deduced from the type you pass to Value and WideString seems to be the default VARCHAR representation anyway.
Trinidad
Ok, thx for the info, I will change that so nulls can be handled too
Bulan