I'm trying to learn and build a program that communicates with a discord server, and a SQL server. I'm not familiar with SQL at all but know Python. I have looked through relevant Stackexchange posts, mysql.connector.errors.ProgrammingError: 1064 (4200): You have an error in your SQL syntax; is the most relevant one. I have version 3.8 Python with Conda interuptor, using the Pycharm IDE, and mysql verr 8.0.22. This is the documentation that I followed, https://dev.mysql.com/doc/connector-python/en/connector-python-example-cursor-transaction.html I get this error, I know where the error happens but have no idea why it's happening (syntax error).
File "C:\Users\Chapm\GitHub\Pew-Pew\Main.py", line 52, in <module>
cursor.execute(conn_query, profile_data)
File "C:\Users\Chapm\anaconda3\lib\site-packages\mysql\connector\cursor.py", line 569, in execute
self._handle_result(self._connection.cmd_query(stmt))
File "C:\Users\Chapm\anaconda3\lib\site-packages\mysql\connector\connection.py", line 651, in cmd_query
result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))
File "C:\Users\Chapm\anaconda3\lib\site-packages\mysql\connector\connection.py", line 538, in _handle_result
raise errors.ProgrammingError(
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 ''User', 'Value', 'Wins', 'Losses')VALUES ('DamagedTwitch', 0.1667, 1, 5)' at line 1
This is the code I have for the program, any tips or advice is helpful too. Should I use an env file for the connection to the SQL too?
import os
import discord
from dotenv import load_dotenv
import mysql.connector
from mysql.connector import errorcode
def main():
# connect to discord bot
load_dotenv()
# bot token.
TOKEN = os.getenv('BOT_TOKEN')
# discord server, insert method here to get servers; this is for future reference.
guildName = os.getenv('SERVER_TOKEN')
profiles = os.getenv('PROFILES')
client = discord.Client()
#insert mysql connections here
#this isn't empty in actually code, (obviously)
conn = None
conn = mysql.connector.connect(
host='',
user='',
password='',
database='')
if conn.is_connected():
print('Connected to MySQL database')
cursor = conn.cursor()
user = 'DamagedTwitch'
wins = 1
losses = 5
value = round(wins / (wins + losses), 4)
conn_cursor = conn.cursor()
conn_query = (
"INSERT INTO Profile ('User', 'Value', 'Wins', 'Losses')"
"VALUES (%(User)s, %(Value)s, %(Wins)s, %(Losses)s)")
profile_data = {
'User': user,
'Value': value,
'Wins': wins,
'Losses': losses,
}
cursor.execute(conn_query, profile_data)
conn.commit()
cursor.close()
conn.close()
If you need to know how I created the table, this is the code for that.
def add_table(cursor):
DB_NAME = 'PewPew'
TABLES = {}
TABLES['Profile'] = (
"CREATE TABLE `Profile` ("
" `User` varchar(37) NOT NULL,"
" `Value` float(20) NOT NULL,"
" `Wins` int(11) NOT NULL,"
" `Losses` int(11) NOT NULL,"
" PRIMARY KEY (`User`)"
") ENGINE=InnoDB")
for table_name in TABLES:
table_description = TABLES[table_name]
try:
print("Creating table {}: ".format(table_name), end='')
cursor.execute(table_description)
except mysql.connector.Error as err:
if err.errno == errorcode.ER_TABLE_EXISTS_ERROR:
print("already exists.")
else:
print(err.msg)
else:
print("OK")
try:
cursor.execute(
"CREATE DATABASE {} DEFAULT CHARACTER SET 'utf8'".format(DB_NAME))
except mysql.connector.Error as err:
print("Failed creating database: {}".format(err))
exit(1)
```