安装模块
pip install pymysql
默认安装目录
c:\users\xxx\appdata\local\programs\python\python37\lib\site-packages
成功则提示
Installing collected packages: pymysql
Successfully installed pymysql-1.0.2
编写python
import pymysql
conn = pymysql.Connect(
host='127.0.0.1',
port=3306,
user='root',
passwd='root',
db='test',
charset='utf8'
)
cursor=conn.cursor()
sql='select * from test'
print(sql)
rows=cursor.execute(sql)
print(cursor.fetchall())
cursor.close()
conn.close()
打印结果:
select * from test
((5, 'b', 'a1'), (6, 'nameb', 'typeb'))