0 安装模块,pip install pymysql
1 pymql的作用是在python程序中操作mysql,同样是一个客户端,需要链接服务端后进行数据操作
1 import pymysql 2 #链接需要指定所链接的ip地址和端口号,本地客户端可以直接写localhost, 3 #建立链接需要指定字符集编码,此时为utf8,不是utf-8 4 conn=pymysql.connect(host='localhost', 5 port=3306, 6 user='root', 7 password='407509', 8 #database='test', 9 charset='utf8') 10 cursor = conn.cursor() 11 12 sql = 'show databases;' 13 res = cursor.execute(sql) 14 print(cursor.fetchall())