0
点赞
收藏
分享

微信扫一扫

Python连接数据库

Resin_Wu 2023-11-08 阅读 41

"""测试连接数据库"""

1.第一步导包

from pymysql import Connection

2.获取连接对象

connection = Connection( host="localhost", #主机名(IP) port=3306, #端口号 user="root", #用户名 password="123456", #密码 autocommit=True #(自动确认,用于增删改) ) #3.获取游标 cursor = connection.cursor()

4.使用数据库

connection.select_db("test")

5.执行sql语句

cursor.execute("select *from user")

6.获取数据(数据为元组类型)

fetchall = cursor.fetchall()

7.循环遍历数据

for e in fetchall: print(e)

8.关闭对象

connection.close()

举报

相关推荐

0 条评论