简体   繁体   English

Python MySQL INSERT抛出ProgrammingError:1064

[英]Python MySQL INSERT throws ProgrammingError: 1064

First-time question. 初次提问。 I am writing a Python application for personal use, which reads metadata from MP3 files I've collected on CD-ROMs and inserts it into a MySQL database. 我正在编写一个供个人使用的Python应用程序,它从我在CD-ROM上收集的MP3文件中读取元数据并将其插入到MySQL数据库中。 At least, it should--but when it comes to the actual INSERT statement, the program is throwing a ProgrammingError: "1064: You have an error in your SQL syntax," etc. 至少应该,但是,当涉及到实际的INSERT语句时,程序将引发ProgrammingError:“ 1064:SQL语法有错误,”等。

In trying to write this program, I've learned how to create parameterized queries, instantiate the cursor object with cursor_class = MySQLCursorPrepared , and instantiate the database connection with use_pure = True . 在尝试编写该程序时,我学习了如何创建参数化查询,如何使用cursor_class = MySQLCursorPrepared实例化光标对象以及如何使用use_pure = True实例化数据库连接。 I've searched the Web for similar problems but come up dry. 我已经在网上搜索了类似的问题,但是干了。

Here is the offending code (it's the cursor.execute line specifically that throws the exception; for debugging purposes I've temporarily removed the try/except blocks): 这是有问题的代码(特别是cursor.execute行引发异常;出于调试目的,我临时删除了try / except块):

table = "mp3_t"
# Parameterized query for SQL INSERT statement
query = '''
INSERT INTO %s
(track_num, title, artist, album, album_year, genre, discname)
VALUES
(%s, '%s', '%s', '%s', %s, '%s', '%s')
'''

conn = self.opendb(self.config)
cursor = conn.cursor(cursor_class = MySQLCursorPrepared)
for track in tracklist:
    print("Counter: {}".format(counter))
    # Tuple for parameterized query
    input = (table, track['track_num'], track['title'],
             track['artist'], track['album'], track['album_year'],
             track['genre'], track['discname'])
    print(query % input) # What is the actual query?
    cursor.execute(query, input)

The database table is defined with the following SQL: 使用以下SQL定义数据库表:

CREATE TABLE mp3_t (
    id int NOT NULL PRIMARY KEY AUTO_INCREMENT,
    track_num int NOT NULL,
    title VARCHAR(255) NOT NULL,
    artist VARCHAR(255) NOT NULL,
    album VARCHAR(255) NOT NULL,
    album_year int NOT NULL,
    genre VARCHAR(255) NOT NULL,
    discname VARCHAR(255) NOT NULL);

For debugging, I've got a print statement that outputs the query that's being sent to MySQL (the error message is particularly unhelpful for trying to pinpoint the cause of the problem), for example: 对于调试,我有一个print语句输出发送给MySQL的查询(错误消息对于查明问题的原因特别无益),例如:

INSERT INTO mp3_t
    (track_num, title, artist, album, album_year, genre, discname)
    VALUES
    (1, 'Moribund the Burgermeister', 'Peter Gabriel', 'I', 1977, 'Rock', 'Rock 19')

I don't see any error visually, and if I paste directly into the MySQL CLI and add the required semicolon, it inserts a row into the table as expected. 我在视觉上看不到任何错误,如果直接粘贴到MySQL CLI中并添加所需的分号,则会按预期在表中插入一行。

I'm completely stymied where the problem lies. 我完全陷入问题所在。

If it's any help, I'm running Python 3.6.7 and MariaDB 10.1.37 with Connector/Python 8.0.15, on Ubuntu 18.04 64-bit. 如果有任何帮助,我在64位Ubuntu 18.04上运行带有Connector / Python 8.0.15的Python 3.6.7和MariaDB 10.1.37。

Don't use %s for table name. 不要使用%s作为表名。 Use the table name directly. 直接使用表名。

Table name should not be replaced by %s . 表名不能用%s代替。 I think your error message should like: 我认为你的错误信息应该是:

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near "mp3_t"

So just use table name in your query template. 所以只需在查询模板中使用表名。

INSERT INTO mp3_t
(track_num, title, artist, album, album_year, genre, discname)
VALUES
(%s, %s, %s, %s, %s, %s, %s)

If you want to know the query will be executed, you should use these codes: 如果您想知道将执行查询,您应该使用以下代码:

conn = connect()
cur = conn.cursor()
print(query % cur._get_db().literal(params))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM