import mysql.connector
def create_db_sampledb():
global conn
config_root ={
"host":"localhost",
"user":"root",
"password":"*******"}
sql = "Create Database If Not Exists sampledb CHARSET =utf8 COLLATE=utf8_bin"
try:
conn = mysql.connector.connect(**config_root)
cursor = conn.cursor()
cursor.execute(sql)
conn.commit()
finally:
conn.close()
create_db_sampledb()
出现下面这个报错结果,有没有哪个大神知道怎么解决的? --------------------------------------------------------------------------- NameError Traceback (most recent call last) ~\AppData\Local\Temp/ipykernel_27744/4191062787.py in create_db_sampledb() 8 try: ----> 9 conn = mysql.connector.connect(**config_root) 10 cursor = conn.cursor() NameError: name 'mysql' is not defined During handling of the above exception, another exception occurred: NameError Traceback (most recent call last) ~\AppData\Local\Temp/ipykernel_27744/4191062787.py in <module> 13 finally: 14 conn.close() ---> 15 create_db_sampledb() ~\AppData\Local\Temp/ipykernel_27744/4191062787.py in create_db_sampledb() 12 conn.commit() 13 finally: ---> 14 conn.close() 15 create_db_sampledb() NameError: name 'conn' is not defined