I have searched on forums for the error I keep getting when running my code but it seems to be situation specific. My program connects to a database, and takes a line from a text file, extracts the name from the line, and uses that name to do a search query in the database. The following is the relevant code:
while line:
lines = line.split('\t')
if len(lines) > 1:
date = lines[0]
name = lines[2]
address = lines[3]
amount = int(float(lines[len(lines)-1]))
named = name.split()
first = named[1]
last = named[0]
zipc = lines[4]
cur.execute("SELECT `Date`, `Contrib`, `Amount`, `Street`, `City`
`State`, `Zip` FROM indiv_contribs WHERE Contrib = '%s, %s'" %
(last, first))
rows = cur.fetchall()
The error I keep getting is:
"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'malley, matthew'' at line 1"
1 Answer 1
If your language is Python, your SQL statement should look like:
cur.execute("""SELECT Date, Contrib, Amount, Street, City, State, Zip FROM indiv_contribs WHERE Contrib = %s, %s""", (last, first))
rows = cur.fetchall()
2 Comments
Contrib is a column containing last name and first name. Better do cur.execute("""SELECT Date, Contrib, Amount, Street, City, State, Zip FROM indiv_contribs WHERE Contrib = %s""", (last + ", " + first,))
CityandStatecolumns. I imagine that doesn't help.matthewcould have a'character in there