Database access from Python script
#1
Question 
Hi,

I am currently trying to read my way into addon development for XBMC and I got a little confused with the bulk of (partly fresh, partly deprecated) info that is out there. I was hoping that someone could give me some pointers as to where to read next.

I am planning to implement an addon which reacts whenever an item was added to the XBMC library (I guess this should be a Python Script - Service plugin?).

Is it possible to access the xbmc database (read and write) from the Python script? Also, are there event-like structures to trigger the script (I found an OnUpdate somewhere in the documentation).

Also I wasn't able to find some kind of doxygen like API documentation for the Python methods, could someone please point me to a link for this?

Thanks alot!

taalas
(2012-05-03, 19:47)taalas Wrote: Also I wasn't able to find some kind of doxygen like API documentation for the Python methods, could someone please point me to a link for this?

Meh, as soon as I posted this I found

http://mirrors.xbmc.org/docs/python-docs/

So this part of the question is answered I guess Wink
Reply
#2
Currently, you can still use the http api which will let you query the db, but that's technically depreciated in Eden (it still works now, but will be removed in Frodo). Team-xbmc has decided to not allow direct sql execution on the db, but rather they want to move everything to the JSONRPC. Currently, not everything is implemented in the JSONRPC, so even though it's technically depreciated, you still need to use the httpapi.
Reply
#3
Ah, ok, thanks for clearing this up.

So there is no way of accessing the DB in the current version of the JSON RPC API? The only way (currently) is to use the HTTP API to make changes or query the DB? No built-in functions do this? (I haven't found any)
Reply
#4
You CAN, but it's actively discouraged. I don't believe an addon that directly edits the DB would be accepted into the official repo. That being said:
PHP Code:
try:
    
from sqlite3 import dbapi2 as sqlite
    
print "Loading sqlite3 as DB engine"
except:
    
from pysqlite2 import dbapi2 as sqlite
    
print "Loading pysqlite2 as DB engine"

DB os.path.join(xbmc.translatePath("special://database"), 'MyVideos60.db')
db sqlite.connect(DB)
rows db.execute('SELECT * FROM movie WHERE c07 > 2010'

rows is now a list of records of movies released after 2010. If you make any changes (like INSERT or DELETE statements), You need call db.commit() afterward or the changes will be discarded. When you're finished, call db.close() to unlock the db (only one process can access it at a time).
Reply
#5
You can react on database update events in currently nightlies I think (possibly also in the april snapshot?) via the Monitor class:

http://mirrors.xbmc.org/docs/python-docs...ml#Monitor

In the not-too-distant future you'll have additional information there as well, such as individual events for video added and so on - check the pull req's of Montellese on github.

And yes, please do NOT access the db directly - instead, let's work to get everything you want to be able to do into JSON-RPC so it can be done in a db-agnostic manner.

Cheers,
Jonathan
Always read the XBMC online-manual, FAQ and search the forum before posting.
Do not e-mail XBMC-Team members directly asking for support. Read/follow the forum rules.
For troubleshooting and bug reporting please make sure you read this first.


Image
Reply
#6
Thank you both for taking the time to explain the situation. I do understand the reason behind not wanting direct DB access and I would be happy to try out (upcoming) functionality in the JSON-RPC API. That said, could you elaborate on what I would have to do to request additional functionality in the JSON-RPC API?

The script I would like to implement is for a feature I wanted to have in XBMC for a long time:

On library update check video item path -> if similar filename already exists ask user whether to add a new item or "correct" the existing item

I often move lot's of files between share (from a NAS to an unRAID server) and I would love to have path autocorrection (to me this seemed like a feature that would benefit a lot of people, but after having asked in different threads I seem to be one of the few that have this "problem" Wink)
Reply
#7
(2012-05-03, 22:52)Bstrdsmkr Wrote: You CAN, but it's actively discouraged. I don't believe an addon that directly edits the DB would be accepted into the official repo. That being said:
PHP Code:
try:
    
from sqlite3 import dbapi2 as sqlite
    
print "Loading sqlite3 as DB engine"
except:
    
from pysqlite2 import dbapi2 as sqlite
    
print "Loading pysqlite2 as DB engine"

DB os.path.join(xbmc.translatePath("special://database"), 'MyVideos60.db')
db sqlite.connect(DB)
rows db.execute('SELECT * FROM movie WHERE c07 > 2010'

rows is now a list of records of movies released after 2010. If you make any changes (like INSERT or DELETE statements), You need call db.commit() afterward or the changes will be discarded. When you're finished, call db.close() to unlock the db (only one process can access it at a time).

Thank you, thank you, thank you!

I wanted to fetch Video bookmarks from DB and I couldn't find a way to do it using JSON (I believe JSON doesn't have API for this yet). I was using executehttpi, which is no longer supported, but your example saved me!
Reply

Logout Mark Read Team Forum Stats Members Help
Database access from Python script0