python pymysql 基本使用

 1 from pymysql import *
 2 
 3 # 1.创建连接数据库
 4 conn = connect(host="localhost", port=3306, user="root", password="root", database="jiang_test", charset="utf8")
 5 # 2.获取游标
 6 cur = conn.cursor()
 7 # 数据库操作
 8 count = cur.execute("select *from students")
 9 res = cur.fetchall()  # fetchone()获取一条数据    fetchmany(n)获取指定n条数据量
10 print(type(res))  # tulpe
11 print(count)  # 打印出查询数据数量
12 print(res)  # 打印出查询数据
13 # 3.关闭游标
14 cur.close()
15 # 4.关闭连接
16 conn.close()

步骤

1.创建连接数据库

2.获取游标
3.数据库操作(增删改查)

4.关闭游标

5.关闭连接

猜你喜欢

转载自www.cnblogs.com/jum-bolg/p/11235455.html
今日推荐