Recent

Author Topic: Read and write objects to SQLite database  (Read 1802 times)

Gritty

  • Newbie
  • Posts: 4
Read and write objects to SQLite database
« on: May 17, 2024, 09:34:07 pm »
A few months ago I started using free pascal for cgi scripts on the Gemini protocol.

I am making an app but have a need to read/write objects, such as TDictionary to/from sqlite.

I can get TStringList to export csv, but more complex objects don't seem obvious. I've searched the wiki, looked for methods and units, and searched here in the forum but couldn't find anything applicable.

In python the answer was JSON, but I don't see a similar function to load/dump objects to/from JSON in Free Pascal.

Thanks for any help.

cdbc

  • Hero Member
  • *****
  • Posts: 1234
    • http://www.cdbc.dk
Re: Read and write objects to SQLite database
« Reply #1 on: May 17, 2024, 10:03:59 pm »
Hi
You need to include e.g.: 'fpjson' amo, in your uses clause...
There are lots of json-related stuff in there.
Path = "...fpcsrc/packages/fcl-json/src/"
excerpt from readme.txt:
Quote
This package implements JSON support for FPC.

You might want to have a look at the lazarus jsonviewer tool, written using
fpJSON (see lazarus/tools/jsonviewer). It visualizes the fpJSON data and
shows how to program using fpjson.

JSON support consists of 3 parts:

unit fpJSON contains the data representation. Basically, it defines a set of
classes:

TJSONData
+- TJSONNumber
   +- TJSONIntegerNumber
   +- TJSONFloatNumber
   +- TJSONInt64Number
+- TJSONString
+- TJSONBoolean
+- TJSONNull
+- TJSONObject
+- TJSONArray
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

paweld

  • Hero Member
  • *****
  • Posts: 1056
Re: Read and write objects to SQLite database
« Reply #2 on: May 18, 2024, 08:27:21 am »
You can use RTTI to do this.
FPC has a unit fpjsonrtti that works well with objects and collections. Currently, only published properties are supported.
An example is attached.
Best regards / Pozdrawiam
paweld

Thaddy

  • Hero Member
  • *****
  • Posts: 14802
  • Censorship about opinions does not belong here.
Re: Read and write objects to SQLite database
« Reply #3 on: May 18, 2024, 08:50:12 am »
Another solution, provided your objects are derived from TPersistent, which is compiled in $m+ state, is simply to use the default object streaming mechanism, which is a built-in (Read/WriteComponentResFile). I have been using that for 10's of years, actually since early Delphi. everybody seems to forget that mechanism.
But Benny is right: if you require JSON you can also do that in FreePascal, there are multiple good json libraries for fpc, fpjson is the default and good.
Paweld option is similar to the above, but json format.
« Last Edit: May 18, 2024, 09:57:14 am by Thaddy »
bitrate is always calculated like this:sample rate * bitdepth * number of channels.

Gritty

  • Newbie
  • Posts: 4
Re: Read and write objects to SQLite database
« Reply #4 on: May 18, 2024, 10:35:19 pm »
Thank you for the quick responses and example.  I will check out fpjsonrtti.

Gritty

  • Newbie
  • Posts: 4
Re: Read and write objects to SQLite database
« Reply #5 on: May 22, 2024, 07:04:01 pm »
I tried it out and I was able to use the example code, and it works.  thanks!

However, I have a question: Can TDictionary<t> not be serialized?  Everytime I add items and serialize I get empty curly braces {} with no items inside.

If it's not an easy answer, I will post my code.

paweld

  • Hero Member
  • *****
  • Posts: 1056
Re: Read and write objects to SQLite database
« Reply #6 on: May 23, 2024, 01:05:08 pm »
In this case, you have to write the function that converts to/from JSON yourself. Please find attached a project extended with such an example.
Best regards / Pozdrawiam
paweld

Gritty

  • Newbie
  • Posts: 4
Re: Read and write objects to SQLite database
« Reply #7 on: May 23, 2024, 05:42:07 pm »
@paweld - thanks!  I see what you mean, having to create your own JSON objects and arrays and populating them yourself.  I thought maybe there was a streamlined way for generics but I guess there aren't.

Much thanks.

 

TinyPortal © 2005-2018