简体   繁体   English

Delphi dbgrid没有显示mysql varchar列

[英]Delphi dbgrid is not showing mysql varchar columns

I'm using delphi XE2 and working on a mysql database project. 我正在使用delphi XE2并正在处理mysql数据库项目。 I have a mysql database which has a table consisting of four columns. 我有一个mysql数据库,该数据库的表包含四列。
在此处输入图片说明
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: 我正在使用TDatabase,TQuery,TDatasource和TDBGrid通过以下源代码连接到数据库:

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. 问题是当我尝试选择所有列和行(使用select * from persondb.person )并在dbgrid中显示它们时,没有显示varchar列,而我仅获得了两个int列。

在此处输入图片说明
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. 就像varchar列无法显示一样,例如select fname from persondb.person的sql select fname from persondb.person将导致select fname from persondb.person中的两个单单元格行。 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). 结果与使用sql select fname, lname from persondb.person的结果相同,这甚至不符合逻辑(因为我期望有2X2空表)。

在此处输入图片说明

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. 我还将数据库utf8的字符集更改为latin1,并认为问题可能出在那儿,但也没有运气。
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. 但是我认为正常的行为是dbgrid将varchar字段显示为(memo) ,每个人都在努力克服。
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. 使用dbExpress或Ado连接而不是BDE并不是一个好主意,因为它需要更多的时间来学习和更改代码。 I use oracle (maybe similiar case with mysql). 我使用oracle(也许与mysql类似)。 You should check your database structure. 您应该检查数据库结构。

In Oracle 11, dbgrid cannot display all columns with VARCHAR2 data type and CHAR unit. 在Oracle 11中,dbgrid无法显示具有VARCHAR2数据类型和CHAR单位的所有列。 dbgrid just display data with BYTE unit. dbgrid仅使用BYTE单元显示数据。 but in Oracle 9i, everything's fine. 但是在Oracle 9i中,一切都很好。

So, the solution is change the unit ( char to byte ). 因此,解决方案是将单位( char更改为byte )。 Here is the sql statement for oracle : 这是oracle的sql语句:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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