Recent

Author Topic: [solved] MySQL table field defined as Char(1)  (Read 2096 times)

jcmontherock

  • Full Member
  • ***
  • Posts: 236
[solved] MySQL table field defined as Char(1)
« on: May 03, 2022, 11:24:25 am »
Hello,
I have a MySQL table which contains a field defined as Char(1). I use TSQLQuery to select data. To get the field value I am using:
Code: Pascal  [Select][+][-]
  1. String := SQLQuery.Fields[Column].AsString
  2.  
This does not work: the return value is empty. There is no "AsChar" property. Is somebody knows how to do that ?
« Last Edit: May 06, 2022, 10:00:10 am by jcmontherock »
Windows 11 UTF8-64 - Lazarus 3.2-64 - FPC 3.2.2

Zvoni

  • Hero Member
  • *****
  • Posts: 2330
Re: MySQL table field defined as Char(1)
« Reply #1 on: May 03, 2022, 11:27:01 am »
What's your SELECT look like?
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

jcmontherock

  • Full Member
  • ***
  • Posts: 236
Re: MySQL table field defined as Char(1)
« Reply #2 on: May 03, 2022, 11:35:36 am »
SELECT * FROM datable;
Windows 11 UTF8-64 - Lazarus 3.2-64 - FPC 3.2.2

dseligo

  • Hero Member
  • *****
  • Posts: 1222
Re: MySQL table field defined as Char(1)
« Reply #3 on: May 03, 2022, 11:54:07 am »
SELECT * FROM datable;

Now Zvoni will tell you that you shouldn't use 'select *'.  :)

But that isn't your problem.
I use char(1) all the time and it works perfectly well.
Show more code, this surely isn't your code:
Code: Pascal  [Select][+][-]
  1. String := SQLQuery.Fields[Column].AsString

You don't have variable named 'String' and you are missing semicolon at the end.
So, show your actual code, it will help to spot your error.

dseligo

  • Hero Member
  • *****
  • Posts: 1222
Re: MySQL table field defined as Char(1)
« Reply #4 on: May 03, 2022, 11:58:40 am »
This does not work: the return value is empty.

One more thing: are you absolutely sure you have something written in the that field? Maybe the field is empty in the database (i.e. you didn't insert anything in that field).

Zvoni

  • Hero Member
  • *****
  • Posts: 2330
Re: MySQL table field defined as Char(1)
« Reply #5 on: May 03, 2022, 11:58:48 am »
SELECT * FROM datable;
Now Zvoni will tell you that you shouldn't use 'select *'.  :)
Exactly because of that (and other reasons)
SQLQuery.Fields[Column] is ordinal Access (Column being integer)
Do you know that your "Char(1)" Field is the "column"-th Field?
Remember, ordinal Access is zero-based

Code: Pascal  [Select][+][-]
  1. MyString:=SQLQuery.FieldByName('MyCharField').AsString;
  2.  
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

dseligo

  • Hero Member
  • *****
  • Posts: 1222
Re: MySQL table field defined as Char(1)
« Reply #6 on: May 03, 2022, 12:11:30 pm »
SELECT * FROM datable;
Now Zvoni will tell you that you shouldn't use 'select *'.  :)
Exactly because of that (and other reasons)
SQLQuery.Fields[Column] is ordinal Access (Column being integer)
Do you know that your "Char(1)" Field is the "column"-th Field?
Remember, ordinal Access is zero-based

Code: Pascal  [Select][+][-]
  1. MyString:=SQLQuery.FieldByName('MyCharField').AsString;
  2.  

You are right of course, but I wanted to say that it also has to work with 'select *' and accessing fields by their indexes. If it doesn't, he made an error.
Of course, it is easier to maintain and have less errors if you use 'select field1, field2, ...' and then 'FieldByName('field1').AsString'.

Zvoni

  • Hero Member
  • *****
  • Posts: 2330
Re: MySQL table field defined as Char(1)
« Reply #7 on: May 03, 2022, 12:15:27 pm »
You are right of course, but I wanted to say that it also has to work with 'select *' and accessing fields by their indexes. If it doesn't, he made an error.
Of course, it is easier to maintain and have less errors if you use 'select field1, field2, ...' and then 'FieldByName('field1').AsString'.
Correct!
Though, Query.FieldByName('MyCharField') still works with "SELECT * FROM Table"
As a Rule of Thumb: Avoid ordinal/indexed Field-Access.
Your column is the 6th Column, you access it with Query.Fields[5],
and later another DBA INSERTS a new Column in Position 4, and KABOOM!

EDIT: To explain my aversion to "SELECT * FROM":
Imagine you have something like an Address-Book (or User-Accounts).
You use "SELECT * FROM" all through your code.
Everything looks nice.

On a sunny day, another developer decides: "Hey, let's add a BLOB-Column, so users can upload a picture of themselves"

I can guarantee your Hotline-phone will not stand still ("Why is the network so slow today?")

Another example is the company i work for: Our Mainframe is an IBM iSeries, which runs a DB2 with some 5000 Tables (Yes, you read it right: "Five Thousand")
I often have to write queries spanning some 10-20 Tables (INNER JOIN, LEFT JOIN, ROW_NUMBERS, you name it), and it's a RARITY in our System if a Table has LESS (!!!) than 50 Columns.
Our recordholder is a Table with some 400 Columns......

Bottom Line: At the latest when you have to join multiple tables, a SELECT * FROM will blast your performance to smitherens
« Last Edit: May 03, 2022, 12:26:11 pm by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

jcmontherock

  • Full Member
  • ***
  • Posts: 236
Re: MySQL table field defined as Char(1)
« Reply #8 on: May 03, 2022, 02:38:38 pm »
.FieldByName('MyCharField').AsString;

does not work better.
Windows 11 UTF8-64 - Lazarus 3.2-64 - FPC 3.2.2

Zvoni

  • Hero Member
  • *****
  • Posts: 2330
Re: MySQL table field defined as Char(1)
« Reply #9 on: May 03, 2022, 03:29:00 pm »
Then either your Field is empty, the Field has a different name or your SELECT doesn't work.
Have you checked the Result of your SELECT in MySQL Workbench?

EDIT: You have setup the TSQL-Trinity correctly (Connection, Transaction, Query)?
« Last Edit: May 03, 2022, 03:31:16 pm by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

jcmontherock

  • Full Member
  • ***
  • Posts: 236
Re: MySQL table field defined as Char(1)
« Reply #10 on: May 03, 2022, 04:12:16 pm »
The "Trinity" is OK and as well, all other fields value shown.
« Last Edit: May 03, 2022, 04:18:29 pm by jcmontherock »
Windows 11 UTF8-64 - Lazarus 3.2-64 - FPC 3.2.2

LacaK

  • Hero Member
  • *****
  • Posts: 691
Re: MySQL table field defined as Char(1)
« Reply #11 on: May 04, 2022, 07:50:26 pm »
What is charset/collation of column?

jcmontherock

  • Full Member
  • ***
  • Posts: 236
Re: MySQL table field defined as Char(1)
« Reply #12 on: May 05, 2022, 11:21:25 am »
"utf8mb4" and "utf8mb4_general_ci".
Fields are not empty: they contains one letter. I see them with HeidiSQL.
« Last Edit: May 05, 2022, 11:42:45 am by jcmontherock »
Windows 11 UTF8-64 - Lazarus 3.2-64 - FPC 3.2.2

Zvoni

  • Hero Member
  • *****
  • Posts: 2330
Re: MySQL table field defined as Char(1)
« Reply #13 on: May 05, 2022, 12:05:30 pm »
And did you set the Charset of the Connection?
https://www.freepascal.org/docs-html/fcl/sqldb/tsqlconnection.charset.html

Code: Pascal  [Select][+][-]
  1. ....
  2. MySQLConnection.User:='UserName';
  3. MySQLConnection.Password:='SomePassword';
  4. MySQLConnection.CharSet:='utf8mb4';
  5. MySQLConnection.Open;
  6.  
« Last Edit: May 05, 2022, 12:12:41 pm by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

jcmontherock

  • Full Member
  • ***
  • Posts: 236
Re: MySQL table field defined as Char(1)
« Reply #14 on: May 05, 2022, 02:58:24 pm »
I put "utf8" in my connection and it did not work for fields defined with char(1). All other fields, including those with accented char, were ok, but now with "utf8mb4" it all works.
I will explore to learn what is the difference between these 2 encoding mode.

 Thanks a lot.
« Last Edit: May 05, 2022, 03:07:29 pm by jcmontherock »
Windows 11 UTF8-64 - Lazarus 3.2-64 - FPC 3.2.2

 

TinyPortal © 2005-2018