deedex11
deedex11

Reputation: 61

MySQL syntax error?

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"

Upvotes: 0

Views: 108

Answers (1)

Richk
Richk

Reputation: 131

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()

Upvotes: 3

Related Questions