1、什么是PyMySQL?
-
为了使python连接上数据库,你需要一个驱动,这个驱动是用于与数据库交互的库。
-
PyMySQL : 这是一个使Python连接到MySQL的库,它是一个纯Python库。
-
PyMySQL是一个开源项目 :https://github.com/PyMySQL/PyMySQL
-
作用:
将经常用于查询的某些字段定义索引后,数据库利用索引定位技术,能够大大加快查询速率。
特别是在当表特别大的时候,或者涉及到多表查询的时候,利用索引可以使查询加快成千倍。 -
优点:
查询速度变快,极大的降低l/O成本,降低数据库的排序成本,使用分组、排序时,能大大减少分组和排序时间 -
缺点:
会增加内存
2、简单应用
```
from pymysql import *
# fetchone 方法获取查询的数据
#fetchall 方法获取多条查询记录
def main():
#创建连接对象
conn = connect(host="localhost",database="jingdong",user="root",password="mysql",charset="utf8")
#获得cursor对象
cur = conn.cursor()
#执行select 语句,并返回受影响的行数, 查询一条数据
# count = cur.execute('select * from goods where id=5')
count = cur.execute('select id,name from goods where id>=5')
#打印受影响行数
print(count)
# for i in range(count):
# #获取查询结果
# result = cur.fetchone()
# #打印查询结果
# print(result)
# for i in range(count):
# #获取查询结果
# result = cur.fetchone()
# #打印查询结果
# print(result)
result = cur.fetchall()
print(result)
# 关闭cursor对象
cur.close()
#关闭连接对象
conn.close()
if __name__ == '__main__':
main()
```