1.安装
    pip install MySQLdb
    pip install mysqlclient

    2.连接

    1. import MySQLdb
    2. #连接数据库
    3. conn=MySQLdb.connect(host='localhost',user='使用者',password='密码',port=3306,db='数据库名字')
    4. cur=conn.cursor() #创建游标
    5. sql="写入要执行的mysql语句"
    6. cur.execute(sql) #用游标执行mysql语句
    7. conn.commit() #一定要提交,数据库才会更改!
    8. cur.close() #关闭游标
    9. conn.close() #关闭数据库