Recent

Author Topic: [solved] String types : differences between LCL program and console program  (Read 1289 times)

mtournay

  • Jr. Member
  • **
  • Posts: 65
Hello everyone

I retrieve data from mssql database.

From an LCL program, when I do
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.  q:TSQLQuery;
  4.  s:String;
  5. begin
  6.  q := TSQLQuery.Create(self);
  7.  q.DataBase := ODBCConnection1;
  8.  q.SQL.Text := 'select * from tiers where id = 17799';
  9.  q.open;
  10.  s := q.FieldByName('prenom').AsString;
  11.  q.close;
  12.  q.free;
  13. end;

s contains accented string 'andré'

but
Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. uses sysutils, classes, odbcconn, SQLDB;
  4.  
  5. var
  6.  c: TODBCConnection;
  7.  t: TSQLTransaction;
  8.  q:TSQLQuery;
  9.  s:string;
  10. begin
  11.  c := TODBCConnection.Create(nil);
  12.  t := TSQLTransaction.Create(nil);
  13.  c.Transaction := t;
  14.  
  15.  c.params.add('Driver=SQL Server Native Client 11.0');
  16.  c.params.add('Server=...');
  17.  c.params.add('trusted_connection=no');
  18.  c.params.add('uid=...');
  19.  c.params.add('pwd=...');
  20.  c.params.add('MARS_Connection=yes');
  21.  c.params.add('database=...');
  22.  
  23.  q := TSQLQuery.Create(nil);
  24.  q.DataBase := c;
  25.  q.SQL.Text := 'SELECT * from tiers where id = 17799';
  26.  q.open;
  27.  s := q.FieldByName('prenom').AsString;
  28.  q.close;
  29.  q.free;
  30. end.

s contains 'andr'#$E9

If I declare s as wideString, it works
Where is the magic so that in LCL, I have my é in s??

I have 26k lines of code, I don't particularly want to change everything to widestring manually

Connection strings is the same in both code

Best regards
« Last Edit: May 07, 2024, 08:52:55 pm by mtournay »
laz 2.06 32b - fpc 3.04 32b - win10 64b

Khrys

  • Full Member
  • ***
  • Posts: 130
Re: String types : differences between LCL program and console program
« Reply #1 on: May 07, 2024, 09:52:31 am »
You'll need to specify the correct code page/character set; For ODBC connections I usually use this:

Code: Pascal  [Select][+][-]
  1. Connection.Params.Add('charset=utf-16');

Bart

  • Hero Member
  • *****
  • Posts: 5497
    • Bart en Mariska's Webstek
Re: String types : differences between LCL program and console program
« Reply #2 on: May 07, 2024, 01:42:14 pm »
In a plain fpc (console) program, string is AnsiString(CP_ACP), so it has the codepage of your system.
In Lazarus the strings codepage is CP_UTF8.
So if the databse returns UTF8, all will be fine.
If it returns UTF16, it'll be converted to UTF8 and you're fine.

In plain fpc, you'll have to cater for that yourself.

So, you need to know what codepage the databse returns and set the codepage of your strings accordingly.

Bart

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11992
  • FPC developer.
Re: String types : differences between LCL program and console program
« Reply #3 on: May 07, 2024, 07:52:15 pm »
( or simply toggle the utf8 tick mark in the application tab (the same where you choose the application icon))

mtournay

  • Jr. Member
  • **
  • Posts: 65
Re: String types : differences between LCL program and console program
« Reply #4 on: May 07, 2024, 08:52:39 pm »
OMFG marcov you are a god!
How long has this checkbox existed? This problem has been bothering me for years.
So far it has no real impact on business, so I didn't care! but today my boss was on my back !

Thank you very much, you saved me redevelopping all the app with a different language
laz 2.06 32b - fpc 3.04 32b - win10 64b

Hartmut

  • Hero Member
  • *****
  • Posts: 891
Re: String types : differences between LCL program and console program
« Reply #5 on: May 08, 2024, 12:49:40 pm »
( or simply toggle the utf8 tick mark in the application tab (the same where you choose the application icon))

I did not find this "utf8 tick mark in the application tab" in Lazarus 2.2.4 in menu Project / Project Options / Application. Does this not exist in Lazarus 2.2.4 ?

Or do you mean checkbox "ANSI codepage is UTF-8 (Windows 10 1902+)"?

The Help-Button does not explain what this checkbox does. Can please somebody explain more detailed? Thanks.

 

TinyPortal © 2005-2018