Recent

Author Topic: I guess I asked the wrong question before :'( Pascal databases  (Read 2081 times)

OC DelGuy

  • Full Member
  • ***
  • Posts: 222
I guess I asked the wrong question before :'( Pascal databases
« on: January 07, 2021, 05:13:28 am »
Previously I asked about controls to display pascal records.  I thought I needed the controls in  the "Data Controls", "Data Access", and "SQLdb" tabs.  Turns out you need a database program to use them and they seem pretty complex.
And, as far as I can gather, the database table seems to be created in the database program and not in the Pascal code!

I have an existing database I created with Pascal Types:
Code: Pascal  [Select][+][-]
  1. type
  2.   Item = record
  3.     Description : String;
  4.     Quan : Byte;
  5.     Price : Real;
  6.   end;
  7.  
  8. var
  9.   Inventory : array[1..2000] of Item;

How can I display these records in a GUI program?
Free Pascal Lazarus Version #: 4.6
Date: 16 MAR 2026
FPC Version: 3.2.2
Revision: Lazarus_4_6
x86_64-win64-win32/win64

speter

  • Hero Member
  • *****
  • Posts: 535
Re: I guess I asked the wrong question before :'( Pascal databases
« Reply #1 on: January 07, 2021, 05:20:39 am »
Have a look at some of the documents relating to database oriented (lazarus) programming, including
https://wiki.freepascal.org/Lazarus_Database_Overview
and
https://wiki.freepascal.org/SQLdb_Tutorial0  (there are at least 4 tutorials)

I have an existing database I created with Pascal Types...

Can you please explain what you mean by the above!?

cheers
S.
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

lucamar

  • Hero Member
  • *****
  • Posts: 4217
Re: I guess I asked the wrong question before :'( Pascal databases
« Reply #2 on: January 07, 2021, 05:51:55 am »
I have an existing database I created with Pascal Types:
Code: Pascal  [Select][+][-]
  1. type
  2.   Item = record
  3.     Description : String;
  4.     Quan : Byte;
  5.     Price : Real;
  6.   end;
  7.  
  8. var
  9.   Inventory : array[1..2000] of Item;

How can I display these records in a GUI program?

Did you do it with normal, flat-file access? as in:

Code: Pascal  [Select][+][-]
  1. var
  2.   MyDatabase: file of Item;
  3. {... etc...}
  4.   Write(MyDatabase, Inventory[i]);
  5.   { and so on }

Then you'll have to either move it to a more "formal" database (say, a SQLite one, or even a TBufDataset) or read the data normally and use normal (non-database) controls. This last is not difficult (much less if your data set is really that small): just react to some event (e.g. a key or button press), write or read your "database" and populate some controls with the data.

It realy depends on exactly what you want to do: if it's just to show records as in a TDBGrid you could populate a TStringGrid instead, if to edit the data record by record, some TLabels, TEdits and TButtons, and so on.
« Last Edit: January 07, 2021, 05:56:01 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

PascalDragon

  • Hero Member
  • *****
  • Posts: 6418
  • Compiler Developer
Re: I guess I asked the wrong question before :'( Pascal databases
« Reply #3 on: January 07, 2021, 09:17:59 am »
How can I display these records in a GUI program?

You could implement something like CSVDataset for your custom database format (the unit is relatively short and should be understandable) and then you can use the database controls without problems. The main “magic” is inside TCSVDataPacketReader.ReadNextRow and TCSVDataPacketReader.LoadFieldDefs.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8578
Re: I guess I asked the wrong question before :'( Pascal databases
« Reply #4 on: January 07, 2021, 11:08:10 am »
How can I display these records in a GUI program?

You could implement something like CSVDataset for your custom database format (the unit is relatively short and should be understandable) and then you can use the database controls without problems. The main “magic” is inside TCSVDataPacketReader.ReadNextRow and TCSVDataPacketReader.LoadFieldDefs.

That could be a fun general-purpose project. I've been working on a parser for a particular type of binary file which I implemented cautiously on a byte-by-byte basis so as to be able to report accurately on unexpected EOF etc., and I'd imagine that something like that could be made into a component with a formatting template or language definition provided at runtime and diagnostics to a stream.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

wp

  • Hero Member
  • *****
  • Posts: 13645
Re: I guess I asked the wrong question before :'( Pascal databases
« Reply #5 on: January 07, 2021, 11:08:27 am »
I guess you are a beginner: you should forget about databases, nothing to get started!

You always talk about "displaying" data. Therefore a stringgrid is absolutely sufficient for this purpose. I am attaching a simple demo project which creates some dummy inventory items and displays them in a grid. Try to understand every line of this project, I added lots of comments. Ask what you don't understand.

This demo creates the inventory by code. In the next step you should read the inventory from a file. But I thought it is more motivating when the first step gives you something to see.

 

TinyPortal © 2005-2018