DBGrid 显示“(MEMO)”作为字符串字段的值
我正在尝试使用 Lazarus 和 SQLdb 组件编写一个简单的 SQLite 应用程序。
我连接到数据库并填充 TDBGrid
。问题是所有文本字段列都显示值“(MEMO)”,而不是数据库中的字符串。
I'm trying to write a simple SQLite application using Lazarus and the SQLdb components.
I connect to the database and populate a TDBGrid
. Problem is that all columns that are text fields display the value "(MEMO)" rather then the string in database.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
我找到了一个简单的解决方案:
必须启用
DBGrid
中的dgDisplayMemoText
属性。I have found a simple solution:
The property
dgDisplayMemoText
from theDBGrid
must be enabled.我忘记了它的来源,但这就是我对 tdbgrid 中的备注字段所做的事情。
bluish 关于 gettext 事件的说法是正确的,这是在代码中实现它的方法:
创建一个名为 MemoDifier 的类:
在代码的实现部分,输入以下内容:
然后单击窗体中的 tdbgrid 控件和 Object Inspector(Lazarus IDE),单击事件选项卡,向下滚动以找到 OnPrepareCanvas 事件。双击它生成代码。然后修改代码以满足您的需要,例如 tdbgrid 控件的名称:
变量 MemoFieldReveal 指向类 MemoDifier。不要忘记修改索引 (Items[x]) 以指向显示 (MEMO) 文本的 tdbgrid 项目/字段的索引号。
I forgot the source of this but this is what I am doing with memo fields in tdbgrid.
bluish is right about the gettext event, this is how to implement it in the code:
Create a class called MemoDifier:
At the implementation section of your code, put this:
Then click the tdbgrid control in your form and at the Object Inspector(Lazarus IDE), click the Events tab, scroll below to find the OnPrepareCanvas event. Double click it to generate the code. Then modify the code to suit to your needs such as the name of your tdbgrid control:
The variable MemoFieldReveal points to the class MemoDifier. Don't forget to modify the index (Items[x]) to point to your index number of the tdbgrid items/fields which shows the (MEMO) text.
备注字段无法显示在
TDBGrid
中。将TDBMemo
添加到表单并将其连接到相同的TDataSource
。在这种情况下,您将在备忘录中看到文本。Memo fields cannot be shown in the
TDBGrid
. AddTDBMemo
to the form and connect it to the sameTDataSource
. You will see the text in your memo in this case.老问题,但我在寻找类似问题时遇到了它,但是是通过代码而不是使用 DBGrid 检索的数据;当我使用
DisplayText
或Text
方法检索字符串字段时,我得到了“(memo)”而不是正确的值。答案其实很简单……TSQLQuery 类拥有一组名为
AsXxx
的方法来根据数据类型获取数据。这里使用 AsString 来内联分配一个变量。此外,如果您在设计时不知道变量的类型(例如,您想为任何类型的表设计通用函数),那么 TSqlQuery 对象的 FieldDefs 属性可以为您提供帮助。
它拥有一个 DataType 属性,允许选择要使用的 AsXxx 函数。这里以为例。
如果您在调试时查看数据类型,您会注意到,使用 SQLite,任何文本都被视为 ftMemo,而不是 ftString,因为那里只有一种类型的文本。
Old question but I came across it while looking for a similar issue, but on the data retrieved by code rather than with a DBGrid; When I retrieved a string fields with the methods
DisplayText
orText
, I got "(memo)" instead of the correct value.The answer is actually simple… The TSQLQuery class owns a set of methods called
AsXxx
to get the data according to a type of data. Here use AsString to assign a variable inline.More, if you don't know at design time of which type the variable is (for example, you want to design a generical function for any kind of table) with its FieldDefs property of your TSqlQuery object can help you.
It owns a DataType property that allows to choose which AsXxx function to use. Here for example.
And if you look at the datatype while debugging, you will notice that with SQLite, any text is seen as a ftMemo, not a ftString for there is only one type of text there.
正如 IRC 上所述,您可能需要将查询字段添加到表单中(以便为它们生成“字段”组件),然后实现
TMemoField.GetText
事件。看看在对象检查器中输入“字段”字段是否会弹出一个编辑器来生成组件(在 Zeos iirc 中是这样做的)。
As said on IRC, you probably need to add the fields of your query to the form, (so that "field" components are generated for them) and then implement the
TMemoField.GetText
event.See if entering the "fields" field in the object inspector brings up an editor to generate the components (it does so in Zeos iirc).
我在 MySQL 和 Tgrid 中有相同的内容,所以我希望答案是相同的,因为它非常简单:-)
说如果 s 是导致问题的字段,那么不要写
write
I have the same in MySQL and Tgrid so I'm hoping the answer is the same as it is quite simple :-)
Say if s is the field causing the problem then Instead of writing
write
一个看似简单的解决方案是在列类型中使用类似 VARCHAR(n) 的内容来限制字段中 TEXT 的长度,其中 n 是允许的最大字符数。
An apparently simple solution is to limit the length of the TEXT in the field using something like VARCHAR(n) in the column type where n is the maximum number of allowed characters.
另一种选择
如果您使用 TZConection,请在连接数据库时将此行添加到您的代码中
Another option
If you are using TZConection add this line to you code when you Connect you database
经过一些测试,我可以看到虽然字段被视为“MEMO”,但对于“string”字段,在创建表时只需保留“VARCHAR”选项而不是“TEXT”即可。
after some tests, I could see that although the fields are treated as "MEMO", for "string" fields, just keep the "VARCHAR" option instead of "TEXT" when creating the table.
您还可以在 sql 中简单地使用 TRIM:
这个技巧也适用于 Lazarus。
You can also simple use TRIM in your sql:
This trick also works in Lazarus.
如果是oracle数据库,更改Tquery并放入选择字符串....cast(field as varchar2(100) as field....
If oracle database, change Tquery and put on select string .... cast(field as varchar2(100) as field ....
本文给出了解决方案:在Delphi的TDBGrid中显示和编辑MEMO字段。
在这里,我总结了您必须执行的操作:
OnGetText = MyDataSetMyFieldGetText
添加到属于您的数据的TMemoField
(此处名为MyField
) 集合(例如TTable
,此处名为MyDataSet
)界面
>类型
>在表单定义中,添加实施
>添加这个方法This article gives a solution: Displaying and editing MEMO fiels in Delphi's TDBGrid.
Here I summarize what you have to do:
OnGetText = MyDataSetMyFieldGetText
to theTMemoField
(here namedMyField
) belonging to your data set (for example aTTable
, here namedMyDataSet
)in the .pas >
interface
>type
> inside your form definition, addin the .pas >
implementation
> add this method