连接不上mysql数据库

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值