MySql教程

Python 连接 mysql 并查询表数据

本文主要是介绍Python 连接 mysql 并查询表数据,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
import pymysql as mysql
con = mysql.connect(host="127.0.0.1",port=3306,user="root",passwd="root",db="aaa",charset="utf8")
mycursor = con.cursor()
print("连接成功")

# 查询
sql = "select * from account"

mycursor.execute(sql)
result=mycursor.fetchall()
for row in result:
    print(row)
    
pip install pymysql
C:\Users\admin>pip install pymysql
Collecting pymysql
Downloading PyMySQL-1.0.2-py3-none-any.whl (43 kB)
|████████████████████████████████| 43 kB 409 kB/s
Installing collected packages: pymysql
Successfully installed pymysql-1.0.2

C:\Users\admin>python

 

这篇关于Python 连接 mysql 并查询表数据的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!