Problem with sqlite

Gerhard Häring gh at ghaering.de
Sat Mar 29 19:41:12 CET 2008


Ok, I'll review your code.

aiwarrior wrote:
> class db:
>     def __init__(self): #constructor
>         conn = sqlite3.connect('./db.db')
>         conn.isolation_level = None

Autocommit mode is mostly for newbies who forget to call commit.
Unfortunately, newbiews find enough other ways to shoot themselves in
the foot. So, in retrospect, maybe I should not have added that feature
to pysqlite ;-)

>         self.cursor = conn.cursor()
>         try:
>             self.cursor.execute("CREATE TABLE database
> (album,filepath)" )
>         except:
>             pass

try: except: pass without catching *specific* exceptions is generally a
very bad idea. If you're doing something stupid, or another error
happens than the one you want to ignore, you will never know this way.

>     def add_entry( self, eone , etwo ): #Add entry to database
>         self.cursor.execute( "INSERT INTO database (album,filepath)
> VALUES (?,?)", ( eone , etwo ) )
>         return 1 #TODO: exception handler
> 
>     def get_mediadb( self, print_db = False ):
>         self.cursor.execute( 'SELECT * FROM database' )
>         if (print_db == True):
>             print self.cursor.fetchall()

The if clause can be written as just "if print_db:".

>     def get_value( self, column ):
>         self.cursor.execute( "SELECT %s FROM database" % column )
>         for n in self.cursor:
>              print n
> 
>     def destructor(self):
>         self.cursor.close()

Just FYI, Python's "destructor" method is called "__del__", not
"destructor".

>     def walking_and_filling(db):
>         pass
> 
> 
> if __name__ == "__main__":
>     db = db()
>     #walking_and_filling( db )
>     for root, dirs, files in os.walk( '''foo_path/''',
> topdown=False ):
>         for name in files:
>             joined = os.path.join(root, name)
>             if (name[-3:] == 'mp3' and os.path.isfile( joined ) ):
>                 try:
>                     audio = MP3 (joined, ID3=EasyID3 )
>                     print (audio['album'])
>                     db.add_entry( joined, audio['album'] )
>                 except:
>                     pass

Now, this try: except: pass is most probably hiding the real error That
leads to the insert failing. Because you just ignored any errors, you
will never now what exactly went wrong here.

>     db.get_mediadb( print_db=True )
> 
> 
> When i execute this the database doesn't get filled with anything and
> the program stays running in memory for ever. [...]

HTH,

-- Gerhard



More information about the Python-list mailing list