0

i have a database and i give it name "test" then i try to change the database name using python code, but when i run the program the result give me an error warning. here is my code :

def changedb_name():
    import mysql.connector
    mydb = mysql.connector.connect(host = 'localhost', user='root', password ='')
    mycursor = mydb.cursor()
    mycursor.execute('ALTER DATABASE test RENAME to test1')

changedb_name()

here is the error warning :

Traceback (most recent call last):
  File "c:\Users\NBUSER\Documents\ARTOFWAR\PITON\02-piton\aldo\chgdbname.py", line 18, in <module>
    viewdb1()
  File "c:\Users\NBUSER\Documents\ARTOFWAR\PITON\02-piton\aldo\chgdbname.py", line 15, in viewdb1       
    mycursor.execute('RENAME DATABASE test to test1')
  File "C:\Users\NBUSER\AppData\Local\Programs\Python\Python39\lib\site-packages\mysql\connector\cursor.py", line 568, in execute
    self._handle_result(self._connection.cmd_query(stmt))
  File "C:\Users\NBUSER\AppData\Local\Programs\Python\Python39\lib\site-packages\mysql\connector\connection.py", line 846, in cmd_query
    result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))
ion.py", line 656, 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 MariaDB server version for the right syntax to use near 'DATABASE test to test1' at line 1
PS C:\Users\NBUSER\Documents\ARTOFWAR\PITON\02-piton> & C:/Users/NBUSER/AppData/Local/Programs/Python/Python39/python.exe c:/Users/NBUSER/Documents/ARTOFWAR/PITON/02-piton/aldo/chgdbname.py
Traceback (most recent call last):
  File "c:\Users\NBUSER\Documents\ARTOFWAR\PITON\02-piton\aldo\chgdbname.py", line 18, in <module>
    viewdb1()
  File "c:\Users\NBUSER\Documents\ARTOFWAR\PITON\02-piton\aldo\chgdbname.py", line 15, in viewdb1       
    mycursor.execute('ALTER DATABASE test RENAME to test1')
  File "C:\Users\NBUSER\AppData\Local\Programs\Python\Python39\lib\site-packages\mysql\connector\cursor.py", line 568, in execute
    self._handle_result(self._connection.cmd_query(stmt))
  File "C:\Users\NBUSER\AppData\Local\Programs\Python\Python39\lib\site-packages\mysql\connector\connection.py", line 846, in cmd_query
    result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))
  File "C:\Users\NBUSER\AppData\Local\Programs\Python\Python39\lib\site-packages\mysql\connector\connection.py", line 656, 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 MariaDB server version for the right syntax to use near 'RENAME to test1' at line 1
Isaac Bennetch
  • 11,830
  • 2
  • 32
  • 43
golead rich
  • 19
  • 1
  • 5
  • Does this answer your question? [How do I quickly rename a MySQL database (change schema name)?](https://stackoverflow.com/questions/67093/how-do-i-quickly-rename-a-mysql-database-change-schema-name) – Isaac Bennetch Nov 25 '21 at 16:34

1 Answers1

0

From where did you find the SQL syntax to ALTER DATABASE...RENAME... ? It seems to me your reference is flawed; that isn't any MySQL syntax I'm familiar with. I believe the ability briefly existed for the blink of an eye. A bit of an internet history search confirms that it hasn't been part of MySQL (pdf) syntax in over 10 years (hat tip to @zloctb).

You can't rename a database in pure SQL. The usual workaround is to export to SQL and re-import to the new database.

Isaac Bennetch
  • 11,830
  • 2
  • 32
  • 43