一、安装pymysql

  1. 下载模块
  2. 1.命令行
  3. pip3 install pymysql
  4. pip3 install pymysql -i 源地址
  5. 2.借助于pycharm
  6. 3.python解释器配置文件

二、模块使用

1、基本介绍

  1. """
  2. 最基础为:
  3. 1、创建连接
  4. 2、创建游标对象
  5. 3、定义SQL语句
  6. 4、执行SQL语句
  7. 5、获取返回结果
  8. """
  9. import pymysql
  10. # 创建连接
  11. conn = pymysql.connect(
  12. host="localhost",
  13. port=3306,
  14. user="yly",
  15. password="123",
  16. database="db_7",
  17. charset="utf8",
  18. # autocommit=True #这个
  19. )
  20. # 创建游标对象(就相当于进入了cmd中mysql客户端) cursor : n.(计算机荧光屏上的)光标,游标
  21. cursor = conn.cursor() #执行完毕返回的结果集默认以元组显示
  22. # 定义SQL语句
  23. sql = "select * from userinfo"
  24. # 执行SQL语句
  25. cursor.execute(sql,args)
  26. # 获取返回结果
  27. res = cursor.fetchall()
  28. print(res)
  29. # 在后面不需要使用数据的时候,主动关闭链接 释放资源
  30. cursor.close()
  31. conn.close() # 也可不写,python会在程序运行结束之后自动关

2、指定cursor游标对象的输出格式

  1. cursor = conn.cursor() #执行完毕返回的结果集默认以元组显示,不显示字段名
  2. cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) # 指定字典输出,为 "字段名":value
  3. #注意是cursors

3、cursor方法一:execute执行

3.1 sql注入问题

  1. """
  2. 结论:别自己传参,使用pymysql的execute
  3. """
  4. import pymysql
  5. # 创建链接
  6. conn = pymysql.connect(
  7. host='127.0.0.1',
  8. port=3306,
  9. user='root',
  10. password='123',
  11. database='db_5',
  12. charset='utf8'
  13. )
  14. # 生成一个游标对象
  15. cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) # 让数据自动组织成字典
  16. # 获取用户名和密码
  17. username = input('username>>>:').strip()
  18. password = input('password>>>:').strip()
  19. # 构造SQL语句
  20. # sql = "select * from userinfo where name='%s' and password='%s'"%(username,password)
  21. # 针对核心数据 不要自己拼接 交由execute方法帮你筛选再拼接
  22. sql = "select * from userinfo where name=%s and password=%s"
  23. print(sql)
  24. # 执行SQL语句
  25. cursor.execute(sql,(username,password))
  26. res = cursor.fetchall()
  27. if res:
  28. print(res)
  29. print('登录成功')
  30. else:
  31. print('用户名或密码错误')
  32. """
  33. 登录功能
  34. 1.获取用户名和密码
  35. 2.基于用户名和密码直接精准查找
  36. """
  37. """
  38. 问题1
  39. 用户名正确 不需要密码也能登录
  40. 问题2
  41. 用户名和密码都不需要也能登录
  42. SQL注入问题的产生
  43. 就是通过一些特殊符号的组合 达到某些特定的效果从而避免常规的逻辑
  44. SQL注入问题如何解决
  45. execute方法自动帮你解决
  46. """

3.2 execute用法

  1. cursor.execute(query,args) # query为sql语句,args为传给sql语句的参数
  2. --------------------------------"案例"-------------------------------------
  3. # 情况1、执行sql语句,不需传参:
  4. sql="select * from db_7;"
  5. cursor.execute(sql)
  6. # 情况2、执行sql语句,需传一个参:
  7. sql="select * from db_7 where id = %s;"
  8. cursor.execute(sql,(1,)) # args只传一个参,需要加逗号
  9. # 情况3、执行sql语句,需传多个参:
  10. sql="select * from db_7 where name='%s' and password='%s';"
  11. cursor.execute(sql,("yly","123"))
  12. --------------------------------------------------------------------------
  13. """
  14. execute的返回值:
  15. print(cursor.execute(sql)) # 返回值是执行SQL语句之后,受影响的行数
  16. """

4、conn的方法一:commit 确认操作数据库

  1. """
  2. 查找
  3. """
  4. import pymysql
  5. # 创建链接
  6. conn = pymysql.connect(
  7. host='127.0.0.1',
  8. port=3306,
  9. user='root',
  10. password='123',
  11. database='db_5',
  12. charset='utf8',
  13. autocommit=True # 涉及到增删改 自动二次确认
  14. )
  15. # 生成一个游标对象
  16. cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) # 让数据自动组织成字典
  17. sql1 = 'select * from userinfo'
  18. sql2 = 'insert into userinfo(name,password) values(%s,%s)'
  19. sql3 = 'update userinfo set name="jasonNB" where id=1'
  20. sql4 = 'delete from userinfo where id=2'
  21. # 1.查询语句可以正常执行并获取结果
  22. # cursor.execute(sql1)
  23. # 2.插入语句能够执行 但是并没有影响表数据
  24. # cursor.execute(sql2,('jackson',666))
  25. # 3.更新语句能够执行 但是并没有影响表数据
  26. # res = cursor.execute(sql3)
  27. # print(res)
  28. # 4.删除语句能够执行 但是并没有影响表数据
  29. # res = cursor.execute(sql4)
  30. # print(res)
  31. '''针对增删改操作 需要二次确认才可生效'''
  32. # cursor.execute(sql2,('jackson',666))
  33. # conn.commit()
  34. # cursor.execute(sql3)
  35. # conn.commit()
  36. # cursor.execute(sql4)
  37. # conn.commit()
  38. cursor.executemany(sql2,[('jason111',123),('jason222',321),('jason333',222)])
  39. # 主动关闭链接 释放资源
  40. # conn.close()

5、cursor方法二:fetch批量查询

  1. '''
  2. 分为三种:
  3. cursor.fetchall()
  4. cursor.fetchone()
  5. cursor.fetchmany()
  6. '''
  7. 1fetchone
  8. # 以光标所在位置,往后取出一个结果
  9. res=cursor.fetchone()
  10. print(res)
  11. 2fetchmany
  12. # 以光标所在位置,往后取出多个结果
  13. res=cursor.fetchmany(2)
  14. print(res)
  15. 3fetchall
  16. # 以光标所在位置,往后取出剩余所有结果
  17. res=cursor.fetchall()
  18. print(res)

6、cursor方法三:scroll 移动光标

  1. cursor.scroll(3,mode='absolute') # 相对绝对位置移动
  2. cursor.scroll(3,mode='relative') # 相对当前位置移动