简体   繁体   中英

Delphi dbgrid is not showing mysql varchar columns

I'm using delphi XE2 and working on a mysql database project. I have a mysql database which has a table consisting of four columns.
在此处输入图片说明
I have two sample rows in this table.

在此处输入图片说明

I'm using a TDatabase, TQuery, TDatasource and a TDBGrid to connect to the databse with following source code:

dbgrid1.DataSource :=DataSource1 ;  
datasource1.DataSet :=Query1 ;  
database1.DatabaseName :='personDatabase';  
database1.AliasName :='mysqlodbc';  
database1.LoginPrompt :=false;  
database1.Connected :=true;  
query1.DatabaseName :=database1.DatabaseName;  
query1.SQL.Clear;  
query1.SQL.Add('select * from persondb.person;');  
query1.Active :=true;  

the problem is when I try to select all the columns and rows (with select * from persondb.person ) and show them in a dbgrid, varchar columns are not being displayed and I only get the two int columns.

在此处输入图片说明
It's like varchar columns are not show-able for example the sql select fname from persondb.person will result in two single celled row in dbgrid. the result is the same with sql select fname, lname from persondb.person which is not even logical (cause I expected a 2X2 empty table).

在此处输入图片说明

I also changed the character set of the database which was utf8 to latin1 and thought maybe the problem is there but no luck there too.
I googled hours and not even a similar problem to mine. but I leaned that the normal behavior to expect is dbgrid showing varchar fields as (memo) which everyone is trying to overcome.
so any help is appreciated.

It happened to me view days ago. Using dbExpress or Ado connection instead of BDE is not a good idea, because it needs more time to learn and change the code. I use oracle (maybe similiar case with mysql). You should check your database structure.

In Oracle 11, dbgrid cannot display all columns with VARCHAR2 data type and CHAR unit. dbgrid just display data with BYTE unit. but in Oracle 9i, everything's fine.

So, the solution is change the unit ( char to byte ). Here is the sql statement for oracle :

ALTER TABLE USERX.TABLENAME MODIFY (COLUMNNAME VARCHAR2(50 BYTE));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM