Python - MySQL Connector语法错误

时间:2016-03-14 21:09:53

标签: python mysql

这可能是非常简单的事情,但是我想弄清楚我在这里做错了什么。我的目标:使用Connector / Python执行简单的INSERT操作,以便与我的MySQL数据库进行通信。我的代码:

def insert(title, artist, img):

    conn = mysql.connector.connect(***credentials***)
    cursor = conn.cursor()

    title = title.lower().replace(' ', '_')
    path = 'img/' + title + '.png'
    sp.misc.imsave(path, img)

    add_img = ("INSERT INTO ref "
            "(song_title, artist, path) "
            "VALUES (%s, %s, %s)")

    img_data = (title, artist, path)

    # execute and commit 
    cursor.execute(add_img, img_data)
    conn.commit()

    cursor.close()
    conn.close()

此: insert("Back in Black Live Mono", "ACDC", img) 给我:

ProgrammingError: 1064 (42000): 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 'references (song_title, artist, path) VALUES ('Back in'
Black Live Mono', 'ACDC',' at line 1`

图像保存成功,但从不执行INSERT语句。有任何想法吗?谢谢。

1 个答案:

答案 0 :(得分:1)

REFERENCESMySQL reserved word。如果要将其用作表名,则必须在反引号中引用它:

add_img = ("INSERT INTO `references` "
        "(song_title, artist, path) "
        "VALUES (%s, %s, %s)")