It is hard for me to load some sql in python in order to navigate correctly through MySQLdb. This is the pythons string formatting that kills me.
My sql statement uses the LIKE keyword with wildcards. I tried several different things in Python. The problem is that one day I will get one of them, there is a line of code in MySQLdb that comes off in a string format.
Attempt 1:
"SELECT tag.userId, count (user.id) as totalRows FROM user INNER JOIN tag ON user.id = tag.userId WHERE user.username LIKE '%% s%'"% (query)
This is not an option. I get a value error:
ValueError: unsupported character '' '' (0x27) with index 128
Attempt 2:
"SELECT tag.userId, count (user.id) as totalRows FROM user INNER JOIN tag ON user.id = tag.userId WHERE user.username LIKE '\ %% s \%'"% (Request)
I get the same result from attempt 1.
Attempt 3:
like = "LIKE '%" + str (query) + "%'" totalq = "SELECT tag.userId, count (user.id) as totalRows FROM user INNER JOIN tag ON user.id = tag.userId WHERE user.username "+ like
This correctly creates the totalq variable, but now when I go to run the query, I get errors from MySQLdb:
File "build / bdist.macosx-10.6-universal / egg / MySQLdb / cursors.py", line 158, in execute query = query% db.literal (args) TypeError: not enough arguments for format string
Attempt 4:
like = "LIKE '\%" + str (query) + "\%'" totalq = "SELECT tag.userId, count (user.id) as totalRows FROM user INNER JOIN tag ON user.id = tag.userId WHERE user .username "+ like
This is the same result as attempt 3.
It all seems really strange. How can I use wildcards in sql operations with python?
python sql format sql-like like
gngrwzrd Jun 28 '10 at 17:33 2010-06-28 17:33
source share