Just tested the tips:
Library.query.filter_by(media["path"]).items.append(item)

but i get TypeError: filter_by() takes exactly 1 argument (2 given)

Oh what the heck, while im writing here, should i use the filter_by function
(when i get this to work) to see if i get empty list to check if a Item
already exists (matching primary keys) before i create the new Item instance
(otherwise i get exception) or is it just best to just do a try except
block?

On 26 February 2011 01:04, Magnus Pettersson <[email protected]>wrote:

> Here is some more info, since i posted i have read around more and i
> reordererd stuff and kinda have fixed my problem now, since im pretty newbie
> with all this database stuff i dont have a clue why it works now and not
> before. here is a breakdown of what i do now. Basically i have made a
> model.py file with the entities:
>
> model.py
> =======================================
> from elixir import Entity, setup_all
> from elixir import Field, Unicode, ManyToOne, OneToMany
>
> class File(Entity):
>     path = Field(Unicode, required=True)
>     filename = Field(Unicode, required=True)
>     ext = Field(Unicode,required=True)
>     season = Field(Unicode,required=False)
>     episode = Field(Unicode,required=False)
>     item = ManyToOne("Item")
>
> class Item(Entity):
>     path = Field(Unicode, primary_key=True)
>     title = Field(Unicode, primary_key=True)
>     type = Field(Unicode,required=True)
>     year = Field(Unicode(4),default="0000",primary_key=True)
>     imdbID = Field(Unicode,required=False)
>     genres = Field(Unicode,required=False)
>     library = ManyToOne("Library")
>     files = OneToMany("File")
>
> class Library(Entity):
>     path = Field(Unicode, primary_key=True)
>     name = Field(Unicode)
>     items = OneToMany("Item")
>
> #Setup tables
> setup_all()
> =======================================
>
> mediaDatabase.py
> =======================================
> import os
> from model import *
> from elixir import metadata, session, create_all
> dbfile = "mediaDatabase.sqlite"
> metadata.bind = "sqlite:///%s" % dbfile
>
> class Database:
>     def initDb(self):
>         if not os.path.exists(dbfile):
>             create_all()
>
>     def saveData(self):
>         session.commit()
>
>     def parseMediaItem(self,media):
>
>         item =
> Item(path=media["path"],title=media["title"],type=media["type"],year=media["year"])
>
>         for f in media["files"]:
>             file = File(path=f["path"],filename=f["rawname"],ext=f["ext"])
>             file.season = f["season"]
>             file.episode = f["episode"]
>             item.files.append(file)
>
>         libs = Library.query.all()
>         foundlib = False
>         for lib in libs:
>             if lib.path == item.path:
>                 lib.items.append(item)
>                 foundlib = True
>                 break
>
>         if not foundlib:
>             print "parseMediaItem: No library database found, should not
> happen!!(BUG)"
>
>         return item
>
> ======================================
>
> mediaGui.py  .. #Just the relevant lines
> ======================================
> from model import Library, Item, File
> from mediaDatabase import Database
>
> DB = Database()
>
> #Classes and gui stuff here
> #calling ex. DB.parseMediaItem(m) where m is a instance of a class
> inheriting Dict
> #or calling DB.saveData() and so on.
> #Even querying Items like movielist = Item.query.all()
>
> if __name__ == "__main__":
>     DB.initDb()
>     app = QtGui.QApplication(sys.argv)
>     myapp = MainForm()
>     myapp.show()
>     sys.exit(app.exec_())
>
> ======================================
>
> Anyone who knows why it works now and not before? hehe...
>
> Diez: Thanks for the suggestion with how to query the library! It looks as
> a much better way than looping trough the library :)
> oh and the application isnt multi-threaded or processed
>
>
>
>
> On 25 February 2011 23:51, Diez B. Roggisch <[email protected]> wrote:
>
>>
>> Am 25.02.2011 um 21:30 schrieb Magnus Pettersson:
>>
>>
>>  I have written all classes that handles the sqlite database in one
>>> python file called "mediaDatabase.py" and then i want to import that
>>> module into another file "mediaGui.py" to be able to query and write
>>> to the database. but i always get a sqlalchemy.exc.OperationalError
>>> that table does not exist or databse is locked (think it was the
>>> "table does not exist" if i actually created and filled and some
>>> entrys into the database.sqlite file from within the mediaDatabase.py,
>>> and i get "database is locked" when i created the sqlite file and
>>> entities from within the "mediaGui.py" file
>>>
>>> Here is the locked exception:
>>> sqlalchemy.exc.OperationalError: (OperationalError) database is locked
>>> u'INSERT INTO mediadatabase_item (path, title, type, year, "imdbID",
>>> genres, library_path) VALUES (?, ?, ?, ?, ?, ?, ?)' (u'V:/Movies/',
>>> u'Bedtime Stories', 'movie', u'2008', None, None, None)
>>>
>>> I tried to read around about sqlalchemy and found out that i need to
>>> do something with the metadata or session but i cant figure out what &
>>> how & where to put stuff
>>>
>>> A extract from mediaDatabase.py:
>>>
>>> from elixir import *
>>> dbfile = "mediaDatabase.sqlite"
>>>
>>> class Database:
>>>   def initDb(self):
>>>       metadata.bind = "sqlite:///%s" % dbfile
>>>       setup_all()
>>>       if not os.path.exists(dbfile):
>>>           create_all()
>>>
>>>   def saveData(self):
>>>       session.commit()
>>>
>>>   def parseMediaItem(self,media):
>>>       item =
>>> Item(path=media["path"],title=media["title"],type=media["type"])
>>>       libs = Library.query.all()
>>>       for lib in libs:
>>>           if lib.path == item.path:
>>>               lib.items.append(item)
>>>               foundlib = True
>>>               break
>>>
>>> class File(Entity):
>>>   path = Field(Unicode, required=True)
>>>   filename = Field(Unicode, required=True)
>>>   ext = Field(Unicode,required=True)
>>>   item = ManyToOne("Item")
>>>
>>> class Library(Entity):
>>>   path = Field(Unicode, primary_key=True)
>>>   name = Field(Unicode)
>>>   items = OneToMany("Item")
>>>
>>>
>>> And some lines from mediaGui.py :
>>>
>>> from mediaDatabase import Database,Item,Library,File
>>> MDB = Database()
>>>
>>> class MainForm(QtGui.QMainWindow, Ui_MainWindow):
>>>     #init etc here
>>>
>>>     def update(self):
>>>            #some code here
>>>           MDB.parseMediaItem(m)  # HERE comes the error, right now
>>> when writing its a
>>>
>>
>>
>> This looks all a bit weird - the DB-initialization class for example
>> should be a singleton to prevent multiple instantiation, you shouldn't loop
>> over all libs and then pick the matching one, but instead make a query and
>> directly filter for it:
>>
>> Library.query.filter_by(media["path"]).items.append(Item(..., ))
>>
>> Your main problem I can't really comment on though, as you don't give the
>> full stacktrace. Also, please give us the advice you already have found, so
>> we can take a look.
>>
>> Is your application by any chance multi-theraded or multi-process (do
>> several processes access the same DB-file)?
>>
>> Diez
>>
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "SQLElixir" group.
>> To post to this group, send email to [email protected].
>> To unsubscribe from this group, send email to
>> [email protected].
>> For more options, visit this group at
>> http://groups.google.com/group/sqlelixir?hl=en.
>>
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"SQLElixir" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sqlelixir?hl=en.

Reply via email to