python - MySQL 语法错误?

标签 python mysql database

我在论坛上搜索了运行代码时不断出现的错误,但它似乎是特定情况的。 我的程序连接到数据库,从文本文件中获取一行,从该行中提取名称,然后使用该名称在数据库中执行搜索查询。相关代码如下:

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

我不断收到的错误是:

"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"

最佳答案

如果您的语言是 Python,您的 SQL 语句应如下所示:

cur.execute("""SELECT Date, Contrib, Amount, Street, City, State, Zip FROM indiv_contribs WHERE Contrib = %s, %s""", (last, first))
rows = cur.fetchall()

关于python - MySQL 语法错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8320965/

相关文章:

python - 使用 Python 计算文件中的二元组(两个单词对)

python - 如何在 Django 模型中指定字段元组的唯一性

mysql - 跳过mysql中的第一行导出到linux中的.txt文件

php - 没有选择 : Cannot execute queries while other unbuffered queries are active (INSERT INTO/ANALYZE TABLE/BEGIN TRANSACTION)

database - Docker 数据库文件 : inside or outside the container?

MySQL 仅当列中的行重复时才选择条件

python - 我想使用 Drive Api 从 google 云端硬盘下载文件

c# - 尝试打印 Crystal 报表时出错

php - 连接搜索表单到MySQL数据库并显示结果

python - pool.map() 如何在内部分配工作?