Problem with sqlite
aiwarrior
zubeido at yahoo.com.br
Sat Mar 29 16:21:23 EDT 2008
On Mar 29, 6:41 pm, Gerhard Häring <g... at ghaering.de> wrote:
> 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
I'm sorry about not saying showing the libraries. It was not on
purpose.
import os
import sqlite3
from mutagen.easyid3 import EasyID3
from mutagen.mp3 import MP3
##def tree(path):
## node = ()
## for node in os.listdir( path ):
## if( os.path.isdir( path + node )):
## tree(path+node)
## return path
class db:
def __init__(self): #constructor
conn = sqlite3.connect( './db.db' )
conn.isolation_level = None
self.cursor = conn.cursor()
try:
self.cursor.execute( "CREATE TABLE database
(album,filepath)" )
except:
pass
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()
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()
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
db.get_mediadb( print_db=True )
This is all the code. Some of that try pass code is just something i
glued to create a clean slate database file
More information about the Python-list
mailing list