Skip to main content
Known Participant
August 19, 2013
Answered

creating tables (or other persistent data structures) in Lightroom Lua

  • August 19, 2013
  • 1 reply
  • 2656 views

I have a complex data structure I want to be able to add to a Lightroom plugin.  I know Lightroom uses a SQLite database. Can I add tables to that database from either inside a Lightroom plugin or externally?  I see no documentation of any API capabilities granting direct queries of the SQLite database and as of now it's looking like the only technique I have at my disposal is to create my own homebrewed database system using the file system. I would like that not to be the case.  Does anyone have any guidance for me?

This topic has been closed for replies.
Correct answer johnrellis

A couple of thoughts:

- Rather than storing your external tables as CSV files, consider storing them as Lua-syntax tables.  The native Lua loader is extremely fast.  (My plugins have stored data using both formats.)

- If your external tables are already in relational format, you could consider having your plugin create a SQLite database on the side, invoking the "sqlite3" command-line program to read and write the database.  Not very high performance, of course, but perhaps sufficient for your needs.  "sqlite3" is pretty fast for a command-line program.

1 reply

johnrellis
Legend
August 19, 2013

The SDK API lets a plugin define and store per-image scalar metadata fields (strings, enums, URLs).  Such a field can be made visible to the user or not, and it can be made searchable, both by the user through the UI and by the plugin through the API.  So your plugin could define a metadata string field and then use the smart-collection API to search for all images whose string field meets the specified criteria.

While it is technically possible to modify the SQLite catalog database, most plugin authors haven't found it viable.  There is no SDK API for accessing the catalog via SQL.  Also, LR keeps the database locked while it is running, so a plugin needs to do a song and dance of writing a command script containing SQL queries, exiting LR, executing the script, recording the result in a file, restarting LR.  And of course, the database schema is undocumented and subject to arbitrary change with each minor release of LR. 

johnrellis
Legend
August 19, 2013

Storing persistent data in the file system can be pretty straightforward and easy to program in Lua.  A plugin can write its persistent data to a file using Lua syntax and then load the file using the native Lua loader.  This can be very fast, loading perhaps megabytes of data in a second or two, and it only takes a small numbe of lines of code to read and write nested tables.