So I have a Python script that is for all intents and purposes going to sync Discord with my website. I am saving the messages to a database or trying to anyway. I keep getting an error saying there is an error with my syntax. I have been through all the documentation on the matter multiple times and still haven't found a reason or solution. Below is the part of the script I am working with right now. Any ideas are appreciated! The print yes and print no are just for diagnostics until I have it working. Ultimately, there will be a lot more script in each space.
@client.event
async def on_message(message):
channel = message.channel
guild = message.guild
mycursor = mydb.cursor()
mycursor.execute("SHOW DATABASES")
author=message.author
headers=(guild,author)
if guild in mycursor:
print("yes")
else:
print("no")
mycursor.execute("CREATE DATABASE %s",guild)
mycursor.execute("CREATE TABLE %s (message VARCHAR(255) %s (author VARCHAR(255)",headers)
Here is the error I get.
root@viktor:~# python3 bot2.py
Bot Loaded
no # <---- (This is the print no if there is not a database with the same name as the guild.)
Ignoring exception in on_message
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/discord/client.py", line 312, in _run_event
await coro(*args, **kwargs)
File "bot2.py", line 95, in on_message
mycursor.execute("CREATE DATABASE %s",guild)
File "/usr/local/lib/python3.6/dist-packages/mysql/connector/cursor.py", line 551, in execute
self._handle_result(self._connection.cmd_query(stmt))
File "/usr/local/lib/python3.6/dist-packages/mysql/connector/connection.py", line 490, in cmd_query
result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))
File "/usr/local/lib/python3.6/dist-packages/mysql/connector/connection.py", line 395, in _handle_result
raise errors.get_exception(packet)
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 '%s' at line 1
Okay, so still not sure what the issue is but I did find a resolution.
I simply changed the way I setup the command to execute.
cmd="CREATE DATABASE " + guild
mycursor.execute(cmd)