Thread Rating:
  • 0 Vote(s) - 0 Average
[HELP] Database Query to List string formatting
#1
Hi there,

I am working on a little script to assist in learning Python that will look at a directory and check the XBMC database to see if there are any files that are unscraped. I have everything done, the only issue I'm running into is that the file names coming from the XBMC database are not coming out cleanly. Here is the code that I'm using:

Code:
database = sqlite3.connect('C:\\MyVideos34.db') #Copied here for testing
cursor = database.cursor()
cursor.execute('SELECT strFilename FROM files,movie WHERE files.idFile = movie.idFile')

datalist = []
for row in cursor:
    datalist.append(row)

print datalist

This all works just fine, except that the files end up looking like this:

(u'Happy Gilmore (1996) [DVDRip].avi',)

Whereas when I use the os.listdir() function, I get a nice clean file name like:

Happy Gilmore (1996) [DVDRip].avi



Does anybody know how I can get my file names out of the database more cleanly? Or how I can go about cleaning them up? I need them to come out identical to the os.listdir() function because I will ultimately be comparing the lists to look for unscraped files.

Take it easy on me, this is my first real use of Python!

I greatly appreciate any assistance you could offer
Reply
#2
It looks like what is happening is that the SQL query pulls out a tuple of tuples, so I am ending up with a list of tuples. I'm at a bit of a loss on how to handle this scenario.
Reply
#3
I think I got it, I changed this:

Code:
datalist = []
for row in cursor:
    datalist.append(row)

To this:

Code:
datalist = []
for row in cursor:
    for item in row:
        datalist.append(item)
Reply
#4
You may use a custom row factory
http://docs.python.org/library/sqlite3.h...ow_factory
or simply do this:
datalist = []
for row in cursor:
datalist.append(row[0])
Reply
#5
Croaker Wrote:You may use a custom row factory
http://docs.python.org/library/sqlite3.h...ow_factory
or simply do this:
datalist = []
for row in cursor:
datalist.append(row[0])

Thank you. The code change I made above did work ultimately, but your second suggestion there is cleaner. I'll try it!
Reply



[HELP] Database Query to List string formatting00