数据库
首页 > 数据库> > 10.python-5.3.2数据库的连接

10.python-5.3.2数据库的连接

作者:互联网

import pymysql


connection = pymysql.connect(host = 'localhost', user = 'root', db = 'student', charset = 'utf8', passwd = 'as123=as123')

# 获取cursor,数据库执行对象
cursor = connection.cursor()

#定义sql
sql = 'select*from student'

#执行sql语句
students = cursor.execute(sql)

#通过fetchall获得所有的数据
result = cursor.fetchall()
print(result)
# 关闭链接
connection.close()

标签:10,5.3,as123,fetchall,python,pymysql,cursor,connection,sql
来源: https://blog.csdn.net/weixin_37706204/article/details/120477392