Recent

Author Topic: How to do SQL Query with special characters?  (Read 6786 times)

shobits1

  • Sr. Member
  • ****
  • Posts: 271
  • .
How to do SQL Query with special characters?
« on: February 17, 2017, 03:36:17 pm »
I have an MS-Access database (a .mdb file) that I want to do some work on.
the problem I'm facing is that the tables names have some special characters like: é è ° (french naming) and the fields names in some table have the same issue. the TSQLQuery always raises an error when ever I do a SELECT with specific fields. I tried to use [] and `` but nothing works..

Code: MySQL  [Select][+][-]
  1. # does not work
  2. SELECT N°Ins
  3.       ,Réf_Group
  4.       ,Réf_Section
  5. FROM `Etudiants`
  6.  
  7. # does not work
  8. SELECT `N°Ins`
  9.       ,`Réf_Group`
  10.       ,`Réf_Section`
  11. FROM Etudiants
  12.  
  13. # does not work
  14. SELECT [N°Ins]
  15.       ,[Réf_Group]
  16.       ,[Réf_Section]
  17. FROM Etudiants
  18.  
  19. # works but displays fields names with ?
  20. FROM Etudiants
  21.  

Please help me.

BTW... is it possible to use Zeos to connect to MS-Access database? how?

Also I'm using the last official release of lazarus (v1.6.2)
« Last Edit: February 17, 2017, 06:54:10 pm by shobits1 »

shobits1

  • Sr. Member
  • ****
  • Posts: 271
  • .
Re: How to do SQL Query with special characters?
« Reply #1 on: February 18, 2017, 10:04:58 am »
Anyone?  :'(

Thaddy

  • Hero Member
  • *****
  • Posts: 14367
  • Sensorship about opinions does not belong here.
Re: How to do SQL Query with special characters?
« Reply #2 on: February 18, 2017, 10:16:42 am »
Is the msaccess database in UTF8 format or in the French codepage?
As long as the database encoding is UTF8 it should work. If it is in the French MS codepage you will need to do some extra work.
« Last Edit: February 18, 2017, 10:18:44 am by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

shobits1

  • Sr. Member
  • ****
  • Posts: 271
  • .
Re: How to do SQL Query with special characters?
« Reply #3 on: February 18, 2017, 11:46:31 am »
it's not UTF8.. (it's an old .mdb file)
what extra work, I need to do?

BTW.. I tried the same file with delphi (XE6) and it works just fine. still I'd like to do this on lazarus (I need the FPSpreadSheet component).

Thaddy,,, any guide/hint/help will be appreciated.
« Last Edit: February 19, 2017, 06:48:20 am by shobits1 »

shobits1

  • Sr. Member
  • ****
  • Posts: 271
  • .
Re: How to do SQL Query with special characters?
« Reply #4 on: February 19, 2017, 03:46:23 pm »
Should I report this as bug or what??   %)

Cyrax

  • Hero Member
  • *****
  • Posts: 836
Re: How to do SQL Query with special characters?
« Reply #5 on: February 19, 2017, 04:04:17 pm »
Grab fpcupdeluxe and install development versions (the trunks) of FPC and Lazarus. Then do a test and if there is bug, then report.

paweld

  • Hero Member
  • *****
  • Posts: 995
Re: How to do SQL Query with special characters?
« Reply #6 on: February 19, 2017, 04:11:40 pm »
If database isn't utf8 format, then try like this:
Code: Pascal  [Select][+][-]
  1. Query1.SQL.Clear;
  2. Query1.SQL.Add(' SELECT [N°Ins], [Réf_Group], [Réf_Section] ');
  3. Query1.SQL.Add(' FROM Etudiants ');
  4. //convert query to database codepage
  5. Query1.SQL.Text:=UTF8ToCP1252(Query1.SQL.Text);
  6. Query1.Open;
Add LConvEncoding to uses section.
Best regards / Pozdrawiam
paweld

shobits1

  • Sr. Member
  • ****
  • Posts: 271
  • .
Re: How to do SQL Query with special characters?
« Reply #7 on: February 19, 2017, 05:39:55 pm »
Cyrax, the problem persist on at least the trunk from December's 25... this maybe due to FPC using UTF8 by default internally; I don't really know but I think this is the case since paweld's code works fine.

Thanks,, Paweld... converting the SQL query from UTF8 to ANSI did the trick.

Now, I can switch back from Delphi and continue my work.  :D

Cyrax

  • Hero Member
  • *****
  • Posts: 836
Re: How to do SQL Query with special characters?
« Reply #8 on: February 19, 2017, 07:11:50 pm »
Then you probaly need to add ${codepage utf8} somewhere after {$mode objfpc}{$H+} stanza.

shobits1

  • Sr. Member
  • ****
  • Posts: 271
  • .
Re: How to do SQL Query with special characters?
« Reply #9 on: February 19, 2017, 07:42:01 pm »
Then you probaly need to add {$codepage utf8} somewhere after {$mode objfpc}{$H+} stanza.
does not work.

 

TinyPortal © 2005-2018