简体   繁体   中英

when i try to create a db using python getting the following error

enter image description here

import mysql.connector

mydb = mysql.connector.connect(

    host = "localhost",
    user = "root",
    passwd= "root",

)

mycursor = mydb.cursor()

mycursor.execute("CREATE DATABASE mydatabase")

---------------------------------------------------------------------------
MySQLInterfaceError                       Traceback (most recent call last)
C:\ANACONDA3\lib\site-packages\mysql\connector\connection_cext.py in cmd_query(self, query, raw, buffered, raw_as_string)
    471                                raw=raw, buffered=buffered,
--> 472                                raw_as_string=raw_as_string)
    473         except MySQLInterfaceError as exc:

MySQLInterfaceError: Can't create database 'mydatabase'; database exists

During handling of the above exception, another exception occurred:

DatabaseError                             Traceback (most recent call last)
<ipython-input-21-a74bf2818939> in <module>
     11 mycursor = mydb.cursor()
     12 
---> 13 mycursor.execute("CREATE DATABASE mydatabase")

C:\ANACONDA3\lib\site-packages\mysql\connector\cursor_cext.py in execute(self, operation, params, multi)
    264             result = self._cnx.cmd_query(stmt, raw=self._raw,
    265                                          buffered=self._buffered,
--> 266                                          raw_as_string=self._raw_as_string)
    267         except MySQLInterfaceError as exc:
    268             raise errors.get_mysql_exception(msg=exc.msg, errno=exc.errno,

C:\ANACONDA3\lib\site-packages\mysql\connector\connection_cext.py in cmd_query(self, query, raw, buffered, raw_as_string)
    473         except MySQLInterfaceError as exc:
    474             raise errors.get_mysql_exception(exc.errno, msg=exc.msg,
--> 475                                              sqlstate=exc.sqlstate)
    476         except AttributeError:
    477             if self._unix_socket:

DatabaseError: 1007 (HY000): Can't create database 'mydatabase'; database exists

The error message is self-descriptive. The database you're trying to create already exists. Create another database or delete the existing one beforehand.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM