python3连接mysql

时间:2022-10-02 07:07:32

用到模块pymysql,用pip安装

sudo pip3 install pymysql3

编写程序

import pymysql

conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='',db='girl')

cur = conn.cursor()

cur.execute("SELECT * FROM student")

for r in cur.fetchall():

           print(r)

cur.close()

conn.close()