Kodi Community Forum

Full Version: Connecting to a DB
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3 4 5
Hello everybody,

I run my own Access-database for my optical media, and I would like to create a simple addon for my own use to be able to quickly search a title in this Access-database.

So far I am still in the first steps of learning the basics of Kodi addons. My first problem is: Contary to the tutorials that I found, Kodi does not seem to pass arguments, I run into an out-of-bounds-error when accessing sys.argv[1] as told in the tutorials (sys.argv[0] is fine), but since i probably would not need this it seems neglectible.

Far more important would be an input box (I am sure this will come up later in the tutorial) and, of course, database connection. And here I am stuck. I learn there is a python package (pypyodbc) to connect to an Access-DB, but afaik it is not part of the Kodi-python package and I'd prefer not to install extra packages, since I do not know how to install these on eg. LibreELEC or a Fire Stick (or if this is even possible), both platforms that I use to run Kodi on.
When I search for something like 'Kodi addon SQL' I find a lot of warnings not to mess with Kodi's DB, which I do not intend to do, but so far little else. So - what are my options here? I need help in the basics of accessing an existing database. I think I'd be able to export my DB into MySQL if this would make things easier. But I need some guidance how to start and where to look...

Thanks,
vonson
(2020-12-03, 16:58)vonson Wrote: [ -> ]When I search for something like 'Kodi addon SQL' I find a lot of warnings not to mess with Kodi's DB

Funny, I've never seen those warnings. But, as Kodi is open-source, you could change things all you want if it will serve your purpose. Kodi has built-in client support for MySQL/MariaDB, and Kodi's default database for videos is a SQLite database file.

Creating a connector for an Access database would be quite a challenge. Are MySQL and its runaway sister MariaDB such impossible options for you?
Thanks for your fast reply.

It's not the database itself that is the problem. I have my Kodi-DB centralized via MariaDB, I assume it would not be too much of a problem to export the DB from Access to MariaDB.
I just have no idea where and how to start to connect to that data with the addon.
(2020-12-03, 16:58)vonson Wrote: [ -> ]Hello everybody,

I run my own Access-database for my optical media, and I would like to create a simple addon for my own use to be able to quickly search a title in this Access-database.

So far I am still in the first steps of learning the basics of Kodi addons. My first problem is: Contary to the tutorials that I found, Kodi does not seem to pass arguments, I run into an out-of-bounds-error when accessing sys.argv[1] as told in the tutorials (sys.argv[0] is fine), but since i probably would not need this it seems neglectible.

Far more important would be an input box (I am sure this will come up later in the tutorial) and, of course, database connection. And here I am stuck. I learn there is a python package (pypyodbc) to connect to an Access-DB, but afaik it is not part of the Kodi-python package and I'd prefer not to install extra packages, since I do not know how to install these on eg. LibreELEC or a Fire Stick (or if this is even possible), both platforms that I use to run Kodi on.
When I search for something like 'Kodi addon SQL' I find a lot of warnings not to mess with Kodi's DB, which I do not intend to do, but so far little else. So - what are my options here? I need help in the basics of accessing an existing database. I think I'd be able to export my DB into MySQL if this would make things easier. But I need some guidance how to start and where to look...

Thanks,
vonson
If you only want to use built in Python modules (which is probably a good idea given your restrictions), you might take a look at the SQLite3 package.

https://www.digitalocean.com/community/t...n-python-3

I'm not sure how you'd get you Access DB into a format that you could use with SQLite, but it's a start anyway.
(2020-12-03, 20:14)vonson Wrote: [ -> ]Thanks for your fast reply.

It's not the database itself that is the problem. I have my Kodi-DB centralized via MariaDB, I assume it would not be too much of a problem to export the DB from Access to MariaDB.
I just have no idea where and how to start to connect to that data with the addon.

You are welcome to take a look at the addon I support.  It has quite a bit of SQL database code which adds, deletes and modifies data in the the Kodi databases.  The source is a uPNP server where I sync records between my uPNP server database with Kodi.  You can look at some of the code to help get you started.   In your case the source could be your database export.  There are Kodi 18 and 19 versions of the addon on Git.  I am happy to answer some questions.  I'd suggest looking at the /resources/lib folder starting with the media.py and sync.py files.


Jeff
jbinkley60 - I downloaded the zip and will have a look into it, thank you for this!!!

However, I had the idea last night that it might be sufficient for me to simply export to an excel-list, i guess this would be much easier? This way the export could be automated.
There is not much actual database operation that I'd need to perform with the addon - all I need is search for a string in the title field, probably the director field, and _maybe_ sort by year.

Or am I making a mistake here?
(2020-12-04, 10:58)vonson Wrote: [ -> ]jbinkley60 - I downloaded the zip and will have a look into it, thank you for this!!!

However, I had the idea last night that it might be sufficient for me to simply export to an excel-list, i guess this would be much easier? This way the export could be automated.
There is not much actual database operation that I'd need to perform with the addon - all I need is search for a string in the title field, probably the director field, and _maybe_ sort by year.

Or am I making a mistake here?

Yes, if you export into CSV vs Excel it would be very easy to write import routines to insert int the Kodi database.  The question to consider is what do you want to do with the data when you import into the Kodi database ?  If you want to play video files and such then more data will be needed.  I suggest looking at the Kodi video database layout at this link .  You'll see in my code I update all of the various tables for full integration into Kodi. 


Jeff
ah, no no no, I do not want to import anything. I just want to be able to use the addon, when I want to watch a Blu ray, to find the location code of the Blu Ray.
I use a kind of weird sorting scheme on my shelf, non-alphabetical, and while i usually know how to find them my wife does not. So it is really just a quick search.
If possible i'd like to add some sort of title list that can be filtered and evtl. sorted, but that is more or less all I want to do with it.
(2020-12-04, 15:19)vonson Wrote: [ -> ]ah, no no no, I do not want to import anything. I just want to be able to use the addon, when I want to watch a Blu ray, to find the location code of the Blu Ray.
I use a kind of weird sorting scheme on my shelf, non-alphabetical, and while i usually know how to find them my wife does not. So it is really just a quick search.
If possible i'd like to add some sort of title list that can be filtered and evtl. sorted, but that is more or less all I want to do with it.

So I am slightly lost at what your trying to accomplish.  How are you playing Blu-Rays (through Kodi, via another player etc..) and are the codes you mention the location of the Blu-Ray files on your hard drive or something else ?   Lastly, so are you wanting an addon with some type of search/query window where you select from a list of titles or the list just displays all the information you want like title and code ?

Jeff
OK, I might have been too vague.

In the end, Kodi has nothing to do with it. I just want a quick way to find a title in my Blu ray collection, physically on the shelf. Find the title, have the location code displayed. The 'location code' is a code written on a label that sticks in the cover of the Blu ray.
Kodi is only used for this because that is what I have open most of the time, on the TV, and it would be fairly convenient to simply use it to find a Blu ray title instead of walking to my desktop computer, boot it, launch my DB in Access and then to start the search...

Ideally the addon would display the full list of Blu ray titles (even with cover images which are available via the Access database) and the corresponding location codes, and then filter this by an input box. Really nice would be optional sorting by title, year, director, ...

That's it. Kodi does not have to playback anything, no link has to be passed.
(2020-12-04, 22:07)vonson Wrote: [ -> ]OK, I might have been too vague.

In the end, Kodi has nothing to do with it. I just want a quick way to find a title in my Blu ray collection, physically on the shelf. Find the title, have the location code displayed. The 'location code' is a code written on a label that sticks in the cover of the Blu ray.
Kodi is only used for this because that is what I have open most of the time, on the TV, and it would be fairly convenient to simply use it to find a Blu ray title instead of walking to my desktop computer, boot it, launch my DB in Access and then to start the search...

Ideally the addon would display the full list of Blu ray titles (even with cover images which are available via the Access database) and the corresponding location codes, and then filter this by an input box. Really nice would be optional sorting by title, year, director, ...

That's it. Kodi does not have to playback anything, no link has to be passed.

Got it.  So a CSV export of your data and then the addon simply reads and displays your list.  No SQL would even be needed.  How many Blu-Rays are we talking about ?  I ask simply wondering whether you want to page though them or search / filter the list ?   This could be as simple as the Hello World addon with some code which reads the CSV file, displays so many lines with a next / PageDn button to read the next number of lines from your CSV file. A slightly more advanced version is to use video listitems which would allow you to display the images and even possibly metadata. 

If you look at the mezzmo.py file in my addon you can see a listitem example with code which looks like this to create the initial lisitem and artwork:

                li = xbmcgui.ListItem(title, iconImage=icon)
                li.setArt({'thumb': icon, 'poster': icon, 'fanart': backdropurl})

Then this section to set some more metadata (which you likely won't need)

                    info = {
                        'duration': getSeconds(duration_text),
                        'genre': genre_text,
                        'year': release_year_text,
                        'title': title,
                        'plot': description_text,
                        'director': creator_text,
                        'tagline': tagline_text,
                        'writer': writer_text,
                        'cast': artist_text.split(','),
                        'artist': artist_text.split(','),
                        'rating': rating_val,
                        'code': imdb_text,
                        'mediatype': categories_text,
                        'season': season_text,
                        'episode': episode_text,
                        'lastplayed': last_played_text,
                        'aired': aired_text,
                        'mpaa':content_rating_text,
                        'studio':production_company_text,
                        'playcount':playcount,
                        'trailer':trailerurl,
                        'tvshowtitle':album_text,
                    }
                    li.setInfo(mediaClass_text, info)

And finally this code to display the listitem:

xbmcplugin.addDirectoryItem(handle=addon_handle, url=itemurl, listitem=li, isFolder=False)



Jeff
Hey,

thanks, that looks promising, I will try to understand it and start with some experiments today. I think I'll go with the listitems idea.

To answer your question: Its roughly around 1000 optical media, DVDs and Blu rays. It would be nice to have a full list to scroll through, and a input box on top of the page to enter a term to filter the list.
Would it be possible to, when selecting an entry in the list, to change to a 'Details' window? Like the 'Information' in the 'Movies' section?
Would it even be possible to create several views, a listview and a poster wall view?
(2020-12-05, 11:23)vonson Wrote: [ -> ]Hey,

thanks, that looks promising, I will try to understand it and start with some experiments today. I think I'll go with the listitems idea.

To answer your question: Its roughly around 1000 optical media, DVDs and Blu rays. It would be nice to have a full list to scroll through, and a input box on top of the page to enter a term to filter the list.
Would it be possible to, when selecting an entry in the list, to change to a 'Details' window? Like the 'Information' in the 'Movies' section?
Would it even be possible to create several views, a listview and a poster wall view?

If you use the listitem approach all of this is possible and is implemented with my addon.   If you want to see a search window example look in the mezzmo.py file for:

def promptSearch():

That provides a search window and grabs the user input.


Jeff
man, thank you so much. I'll have a look into it and see if i can understand it.
Would probably be easier just to create strm files for each movie that have the unique ID stored in the strm. You can integrate them into your movie library. Selecting the strm calls your plugin and then your plugin extracts the ID from the strm and then displays the ID on the screen with xbmcgui.Dialog().ok(heading, message). Seems straightforward.
Pages: 1 2 3 4 5