我在论坛上搜索了运行代码时遇到的错误,但似乎是特定情况。 我的程序连接到数据库,从文本文件中取一行,从行中提取名称,并使用该名称在数据库中执行搜索查询。以下是相关代码:
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()
我不断得到的错误是:
“您的SQL语法有错误;请查看与您的MySQL服务器版本对应的手册,以便在第1行'malley,matthew'附近使用正确的语法”
答案 0 :(得分:3)
如果您的语言是Python,则您的SQL语句应如下所示:
cur.execute("""SELECT Date, Contrib, Amount, Street, City, State, Zip FROM indiv_contribs WHERE Contrib = %s, %s""", (last, first))
rows = cur.fetchall()