简体   繁体   中英

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

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 . 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):

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:

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:

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.

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.

Don't use %s for table name. Use the table name directly.

Table name should not be replaced by %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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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