一开始是完全正常的,也成功与python进行了连接
然后我写了如下代码,第一次运行时完全正常
import mysql.connector
my_db=mysql.connector.connect(
host="localhost", #如果服务器是本机用loacalhost,如果服务器是远程,用IP地址。
user="root", #用户名
passwd="1395st2022" #密码
)
mycursor=my_db.cursor()
mycursor.execute("create database mydb3")
mycursor.execute("show databases")
for x in mycursor:
print(x)
'''mycursor.execute("drop database if exists mydb3")
mycursor.execute("use mydb3")
mycursor.execute("create table users(userID varchar(10) primary key,userName varchar(10))")
mycursor.execute("show tables")'''
第二次运行就开始产生问题
运行结果及报错内容
"D:\Program Files\Python39\python.exe" D:/PycharmProjects/web_practice/connect_database_mysql.py
Traceback (most recent call last):
File "D:\Program Files\Python39\lib\site-packages\mysql\connector\connection_cext.py", line 516, in cmd_query
self._cmysql.query(query,
_mysql_connector.MySQLInterfaceError: Can't create database 'mydb3'; database exists
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:\PycharmProjects\web_practice\connect_database_mysql.py", line 8, in <module>
mycursor.execute("create database mydb3")
File "D:\Program Files\Python39\lib\site-packages\mysql\connector\cursor_cext.py", line 269, in execute
result = self._cnx.cmd_query(stmt, raw=self._raw,
File "D:\Program Files\Python39\lib\site-packages\mysql\connector\connection_cext.py", line 521, in cmd_query
raise errors.get_mysql_exception(exc.errno, msg=exc.msg,
mysql.connector.errors.DatabaseError: 1007 (HY000): Can't create database 'mydb3'; database exists
Process finished with exit code 1
我的解答思路和尝试过的方法
我打开cmd命令窗口
![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/140771154146140.png
真的懵了,不知道啥情况,请众位解答