使用python代码重命名phpmyadmin数据库名称



我有一个数据库,我给它命名为"test"然后我尝试使用python代码更改数据库名称,但是当我运行该程序时,结果给我一个错误警告。下面是我的代码:

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()

下面是错误警告:

Traceback (most recent call last):
File "c:UsersNBUSERDocumentsARTOFWARPITON2-pitonaldochgdbname.py", line 18, in <module>
viewdb1()
File "c:UsersNBUSERDocumentsARTOFWARPITON2-pitonaldochgdbname.py", line 15, in viewdb1       
mycursor.execute('RENAME DATABASE test to test1')
File "C:UsersNBUSERAppDataLocalProgramsPythonPython39libsite-packagesmysqlconnectorcursor.py", line 568, in execute
self._handle_result(self._connection.cmd_query(stmt))
File "C:UsersNBUSERAppDataLocalProgramsPythonPython39libsite-packagesmysqlconnectorconnection.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:UsersNBUSERDocumentsARTOFWARPITON2-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:UsersNBUSERDocumentsARTOFWARPITON2-pitonaldochgdbname.py", line 18, in <module>
viewdb1()
File "c:UsersNBUSERDocumentsARTOFWARPITON2-pitonaldochgdbname.py", line 15, in viewdb1       
mycursor.execute('ALTER DATABASE test RENAME to test1')
File "C:UsersNBUSERAppDataLocalProgramsPythonPython39libsite-packagesmysqlconnectorcursor.py", line 568, in execute
self._handle_result(self._connection.cmd_query(stmt))
File "C:UsersNBUSERAppDataLocalProgramsPythonPython39libsite-packagesmysqlconnectorconnection.py", line 846, in cmd_query
result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))
File "C:UsersNBUSERAppDataLocalProgramsPythonPython39libsite-packagesmysqlconnectorconnection.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

从哪里找到的SQL语法到ALTER DATABASE...RENAME...?在我看来,你的推荐信有缺陷;这不是我熟悉的MySQL语法。我相信这种能力只存在了一眨眼的时间。稍微搜索一下互联网历史,可以确认它已经超过10年没有成为MySQL (pdf)语法的一部分了(向@zloctb致敬)。

不能在纯SQL中重命名数据库。通常的解决方法是导出到SQL,然后重新导入到新数据库。