Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!




Share:
Notifications
Clear all

[Solved] Lazarus - How to fix a Database field showing as (memo) in a DBGrid

2 Posts
1 Users
0 Likes
8,812 Views
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2660
Topic starter  

Sometimes, a text field of a database table or query will be interpreted as a ftMemo blob, and the grid will show "(memo)" instead of the actual text.Quite annoying at times and I'm still surprised that this happens to remain unaddressed in both Lazarus and Delphi ...

Anyhoo .. how do we fix this?

Option 1 (easiest) - Modify your SQL Query

Now this option has the intend to make the "Memo Blob" look like a regular string. We can do this with a simply type cast.

Note : this might be slightly different, depending on your SQL Database. I have used this with SQLite, and MySQL, and heard it works in PostgreSQL as well.

Say your query is something like this:

SELECT
  "Filename", 
  "Date"
FROM
  "mytable";

And let's assume "Filename" is the offending field.

Now (make sure to remove the fields from the TQuery component!) change your query by casting the TEXT as a VARCHAR like so:

SELECT
  CAST( "Filename" as VARCHAR) as "Filename", 
  "Date"
FROM
  "mytable";

You will now see that the actual text is being displayed and not the "(memo)" text.

Option 2 - OnGetText Event

First we need to add fields to our TQuery. Right click the TQuery and select "Edit Fields".
In the upcoming window click the "+" button to at least add the field that shows as "(memo)".

Now click the newly added field. You'll see that a TMemoField has been added in the object inspector.
Click the "Events" tab in the object inspector and double click the "OnGetText" event.

Now add this code to the event handler (assuming: TQuery = qrySomeQuery, fieldname = "SomeField", which creates the TMemoField called "qrySomeQuerySomeField"):

  aText := Sender.AsString;
  DisplayText:=true;

So we get something like this, which now makes (at runtime) show the actual text instead if (memo):

procedure TForm1.qrySomeQuerySomeFieldGetText(Sender: TField; var aText: string; DisplayText: Boolean);
begin
  aText := Sender.AsString;
  DisplayText:=true;
end; 

   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2660
Topic starter  

This seemed to work fine with older MySQL versions, or I remembered it wrong.

Anyhoo, this works:

SELECT
CAST( "Filename" as CHAR) as "Filename",
  "Date"
FROM
"mytable";

 

However, when using the GetText event, to convert a text to a string, then this is no longer needed in current versions of Lazarus Pascal.


   
ReplyQuote

Like what you see and you'd like to help out? 

The best way to help is of course by assisting others with their questions here in the forum, but you can also help us out in other ways:

- Do your shopping at Amazon, it will not cost you anything extra but may generate a small commission for us,
- send a cup of coffee through PayPal ($5, $10, $20, or custom amount),
- become a Patreon,
- donate BitCoin (BTC), or BitCoinCash (BCH).

Share: