Reputation: 2797
In my python script, when I try to DROP
a table that does not exist, using DROP ... IF EXISTS
, I still get an error.
import mysql.connector
cnx = mysql.connector.connect(**my_database_config)
cursor = cnx.cursor()
cursor.execute("DROP TABLE IF EXISTS nonexistent_table;")
Why is this so?
mysql.connector.errors.DatabaseError: 1051: Unknown table 'mydb.nonexistent_table'
The full error:
File "myfile.py", line 199, in myfunction
cursor.execute("DROP TABLE IF EXISTS nonexistent_table;")
File "\venv\lib\site-packages\mysql\connector\cursor.py", line 551, in execute
self._handle_result(self._connection.cmd_query(stmt))
File "\venv\lib\site-packages\mysql\connector\cursor.py", line 470, in _handle_result
self._handle_noresultset(result)
File "\venv\lib\site-packages\mysql\connector\cursor.py", line 440, in _handle_noresultset
self._warnings[0][1], self._warnings[0][2])
mysql.connector.errors.DatabaseError: 1051: Unknown table 'mydb.nonexistent_table'
In this related question the user seems to be getting a Warning instead of an Error for similar code.
Upvotes: 0
Views: 3481
Reputation: 1
I think 'warning' is not standing for 'mistake'. It's just a kind of tip. And actually, if you are going to create 'nonexistent_table', the warning will not hinder this process. Your 'nonexistent_table' will be created without stopping. Clearly, the 'warning' makes us feel uneasy, although it doesn't block our process, we still want to make it disappear. So, you can try this to achieve it.
import warnings
warnings.filterwarnings('ignore')
Upvotes: 0
Reputation: 98398
You say it shows as a warning in phpmyadmin. So your client configuration is set to treat warnings as errors.
See https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlconnection-raise-on-warnings.html and then either disable that or catch the error and disregard it.
Upvotes: 1
Reputation: 872
This seems like an MySQL related issue. So, your SQL statement:
DROP TABLE IF EXISTS nonexistent_table;
is in correct Syntax Drop Table From MySQL:
DROP [TEMPORARY] TABLE [IF EXISTS]
tbl_name [, tbl_name]
[RESTRICT | CASCADE]
but you need to sure on some subjects:
Better to check MySQL Docs corresponding to your version.
Upvotes: 0
Reputation: 483
The warning is supposed to be there to let you handle what should happen if it does not exist. It's not an error.
Upvotes: 1