Database for a plugin, how to?
#1
Greetings my fellow Kodi hackers!

My humble hacking adventure has now led me to a place where I need to store more values for each item in my library. A string atm. How would I achieve this?

I speculate that I cannot simply walk into the Kodi db and make a table or two?

I need to store some data on library update or artwork update.
Perhaps I can make another database that has a table with columns for the dbid and my data?

I already have my own routines for filling lists in my skin. So i speculate that the extraction of my "userdata" should not be a big issue.

Any thoughts?
Reply
#2
Yes, it's better not to interfere with Kodi databases, because they have different schemas in each version. Migrations are transparent to a user, but usually the databases are not compatible.

Actually, your question is too broad to answer it properly. Do you need to know where to store your persistent data or how to do it?

If your question is where, then addon profile folder ("/userdata/addon_data/your.addon.name") is the "official" place where you can store any addon-specific data.

As for how, Python offers you A LOT of ways. For simple storage needs pickling is enough.

If you really need a relational database, then Python standard library includes sqlite3 module. But it requires the knowledge of SQL commands.

If you don't want to bother with SQL, then you can use some ORM library. SQL Alchemy will be a huge overkill, but there are alternatives. Personally, I like peewee - a small ORM library with Django-like API. It consists only of 1 file with no external dependencies (if you don't use peewee plugins), so you can easily drop it into your addon.
Reply
#3
Thank you for taking time out to write a great answer Roman.

I was indeed asking on how to do it, but I thank you for the where information as well.
If you can help me narrow down the direction I need to take I will be most grateful. Truth spoken, I know nothing of ORM and Django and peewee..

I will attempt to look into the various options you mention. And just for the record, we are talking about doing this in python.

I realized that I need a solution that will somehow allow me to add a "sort by" to the Kodi functionality working on my stored data string.
I have dabbled with writing queries in SQL, so making a (fairly simple) query should not really be a problem... but mixing it into Kodi, and the data I get from the build-in library-access functions might?

Here is a better write-up of what I plan to achieve, in case you, or others, have even more to add to aid me in my quest:

1. Storing the data
I need to store a value based on the coverart image file. I have this value ready.
I imagine a table with the unique "DBID" and "myValue".

The storing should preferably happen whenever an item is added/refreshed in the database. But I would settle for a silent run on-load.
I haven't read up on trapping the item-is-added event, but I can imagine it would exist?

2. Retrieving the data
Seeing as I need to be able to sort on this "myValue", and list items accordingly, I need to get this value in bulk. Sometimes I would need to match up to 4.000 in one go.
I can retrieve the regular data from the Kodi database using the built-in library functions just fine.

I have no idea if it is possible to somehow link the Kodi db data with my db using SQL or some other way?
Atm. I might be smart enough to achieve getting the list of items from Kodi, and then afterwards getting the appropriate data from "my" database - one by one... Yup, that's how retarded I am.. (or the other way around, getting the wanted dbids from "my" table into a list, and then - one by one - get the items I need from the Kodi database)

I fear that method will be pretty slow, but I might be wrong - any thoughts?

Thanks,
Reply
#4
2 Torben

1. For storing simple key/value pairs you actually don't need a database. A pickled Python dictionary will do just fine. Read about Python pickling, it's rather simple.

As for "item is added" event, I don't think that there is one, but you can use xbmc.Monitor class to watch for library scan events and then retrieve the necessary data via JSON-RPC.

2. With 2 separate storages (a Kodi database and your custom storage) your 2 options seem to be fine. Naturally, you should walk "one by one" a smaller data set.
Again, I'd recommend to read about Python list/dictionary comprehension constructions that are specifically designed for walking over data sets. Unfortunately, Python has no analogue of LINQ from .NET world.
Reply
#5
I ended up using a few build-in tags as my own delimiterseperated lists.. While awaiting the custom tags I believe Krypton offers.
Reply

Logout Mark Read Team Forum Stats Members Help
Database for a plugin, how to?0