Page 1 of 1

(utf8 + mysql) Someone please get me on track :(

Posted: 27.05.2008, 19:35
by Sakura
I'm trying for almost 4 hours to squeeze some UTF8 from my mysql database into Delphi, with no success at all. Problem is, I cannot use Delphi stdlib functions since these are always using AnsiString's. This also applies to tableName.FieldByName(constFieldName).AsString()

I have a perfectly set up UTF8-only database with perfectly set up UTF8 tables inside, all using utf8_general_ci sorting. (Just telling so you see I know how to set up a database in UTF8 ;-))

Inside this table are some cyrillic utf-8 encoded records, most notably in a field called 'title'...

Here's what I came up with:

Code: Select all

VAR AValue   : Pointer;
VAR memsize  : Cardinal;
VAR Decoded  : WideString;
CONST TestFieldName = 'title';
begin
  tableKundgaben.First;
  memsize := tableKundgaben.FieldByName(TestFieldName).DataSize*3+2;
  try
    GetMem(AValue, memsize);
  except
    MessageDlg('Error allocating ''+IntToStr(memsize)+'' bytes.''', mtError, [mbOK], 0);
    Exit;
  End;
  try
    FillChar(AValue^, memsize, 0);

    If tableKundgaben.FieldByName(TestFieldName).IsBlob Then
      raise Exception.Create('OMCG! It''s a BLOB!');
    If NOT tableKundgaben.FieldByName(TestFieldName).GetData(AValue, True) Then
      raise Exception.Create('Cannot read *that* field, silly!');

    Decoded := UTF8Decode(UTF8String(AValue));
    Console.Lines.Add(Decoded);

  finally
    FreeMem(AValue, 65534);
  End;
end;
I have to call GetData(&buffer, true) to ensure I get the UTF8 encoded string AS-IS but all I seem to get is random nonsense. Well, not random since it's always the same rubbish. I guess I made a really silly mistake here ne? [js]

Mysql 4.11, Delphi 9, ZEOS 6.1

Posted: 28.05.2008, 11:56
by Sakura
Same "problem" with version 6.6beta of ZeosLib :(

Posted: 28.05.2008, 14:34
by Sakura
Problem solved, please close topic.

Solution: Use BDE+ODBC Connector