Modern_Mo
Modern_Mo

Reputation: 97

MySQL Python Error 1064, Error in MySQL Sytax

Im currently having some issues when im trying to insert a line into a database, this also worked before though it is only now that I have added the "key" column it fails to work and gives me an error.

add = ("INSERT INTO server_stats_servers "
            "(Owner, ServerID, MessageID, Channel, ServerIP, ServerPort, ServerName, DiscordName, Key)"
            "VALUES (%(Owner)s, %(ServerID)s, %(MessageID)s, %(Channel)s, %(ServerIP)s, %(ServerPort)s, %(ServerName)s, %(DiscordName)s, %(Key)s)")
    values = {"Owner": owner,
            "ServerID": serverid,
            "MessageID": messageid,
            "Channel": channel,
            "ServerIP": ip,
            "ServerPort": port,
            "ServerName": servername,
            "DiscordName": discordname,
            "Key": key,}
    cursor.execute(add, values)
    cnx.commit()

Error:

mysql.connector.errors.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 ''Key')VALUES ('Modern_Mo', '349692657221500938', '505375472646094868', '48783440' at line 1

Any help is great! (All of these inputs are Strings, aswell the "key" is specified as "modern")

Upvotes: 0

Views: 36

Answers (1)

Eugene Yarmash
Eugene Yarmash

Reputation: 149823

KEY is a reserved word in MySQL. You must quote it in your query when used as an identifier:

add = ("INSERT INTO server_stats_servers (Owner, ServerID, MessageID, "
       "Channel, ServerIP, ServerPort, ServerName, DiscordName, `Key`) "
       "VALUES (%(Owner)s, %(ServerID)s, %(MessageID)s, %(Channel)s, "             
       "%(ServerIP)s, %(ServerPort)s, %(ServerName)s, %(DiscordName)s, %(Key)s)")

Upvotes: 1

Related Questions