os.path.join and sqlite not playing well together
Question
Python 2.7 (Windows)
for root, dirs, files in os.walk(self.DiskLocation, topdown=False):
for name in files:
path = os.path.join(root, name)
print path #check it's what we think it is
self.cur.execute("SELECT * from myTable WHERE Path = ?", (path))
Yields this...
sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 54 supplied.
I was really confused as to what the 54 supplied bindings are, until I realised that my path variable is 54 characters. But why is sqlite interpreting it as a long list of characters rather than a string, and how do I stop it doing this?
Solution
The second argument to execute
is a sequence.
You wrote (path)
, but those are just regular parentheses, so that's equivalent to path
. Strings are sequences of their characters, thus Python is dutifully passing along all 54 characters in the sequence you provided.
What you want is (path,)
, which is a tuple of 1 element.
Tuples are created with commas, not parentheses.
If this is confusing you could just use [path]
; a list is fine here too. :)