sdb.cursor.execute('''
CREATE TABLE IF NOT EXISTS survivors (
discord_id INT,
username VARCHAR(255),
`rank` ENUM('Legendary', 'Expert', 'Specialist', 'Seasoned', 'Skilled', 'Proficient', 'Settled', 'Known', 'New'),
ll_activation DATE,
pz_username VARCHAR(255),
l4d2_username VARCHAR(255),
pz_kills INT,
l4d2_kills INT,
admin_rank ENUM('SuperUser', 'Admin', 'Mod', 'ModMaker'),
PRIMARY KEY (discord_id)
) ENGINE=InnoDB
''')
This is my creation query - but I am getting an error:
Traceback (most recent call last):
File "/home/bot/venv/lib/python3.10/site-packages/discord/client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "/home/bot/KYRBot.py", line 16, in on_ready
db_manager.create_sdb_service()
File "/home/bot/db_manager.py", line 91, in create_sdb_service
sdb.cursor.execute('''
File "/home/bot/venv/lib/python3.10/site-packages/mysql/connector/cursor_cext.py", line 342, in execute
self._handle_result(result)
File "/home/bot/venv/lib/python3.10/site-packages/mysql/connector/cursor_cext.py", line 236, in _handle_result
self._handle_warnings()
File "/home/bot/venv/lib/python3.10/site-packages/mysql/connector/cursor_cext.py", line 221, in _handle_warnings
raise err
mysql.connector.errors.DatabaseError: 1050: Table 'survivors' already exists
Why is MYSQL ignoring the [IF NOT EXISTS]
Part of the statement?!
I apologise in advance for what is prob. a silly Q but I'm very new to development - I am going crazy with this :D !
I have read all the MYSQL documentation and don't see any reason why this doesn't work as intended... I have tried the statements directly into the MYSQL console and I get the same issue (orginally passed via mysql.connnector for python).
EDIT: I should add I've checked the MYSQL Version etc. and all this is fine. Is this suppose to throw an error in MYSQL as oppose to SQLite and I just need to handle gracefully?!
Nailed It!
So using https://pypi.org/project/mysql-connector-python/ there is a standard variable set in the docs for the class constructor:
'raise_on_warnings': True
This will throw Errors for what are normally warnings such as a table already existing EVEN if you use [IF NOT EXISTS]
Either turn off the warnings permanently by setting to false:
'raise_on_warnings': False
Turn warnings off for that Specific Statement:
SET sql_notes = 0; -- Temporarily disable the "Table already exists" warning
CREATE TABLE IF NOT EXISTS ...
SET sql_notes = 1; -- And then re-enable the warning again
Or Handle the Error Gracefully!