pycharm中连接数据库

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/Feng_xiaowu/article/details/100151256

python操作mysql数据库

#	需要先安装pymysql库
import pymysql
#	连接数据库
db = pymysql.connect(host='localhost',user='root',password='123456')
#	选择数据库
db.select_db('school')
#	设置字符集
db.set_charset('urf8)
#	创建游标对象
cursor=db.cursor
#	编写sql语句
sql='select * from student'
#	执行sql语句
data = cursor.excute(sql)
#	结果集中的数量
print(data)
#	获取所有数据,默认以元组的形式返回
print(cursor.fetchall())
#	获取单条数据
print(cursor.fetchone())
print(cursor.fetchmany(1))
#	关闭游标对象,断开连接               
cursor.close()
db.close()

猜你喜欢

转载自blog.csdn.net/Feng_xiaowu/article/details/100151256