广告位联系
返回顶部
分享到

Python中PyMySQL的基本操作介绍

python 来源:互联网 作者:佚名 发布时间:2022-11-09 11:14:36 人浏览
摘要

PyMySQL 是在 Python3.x 版本中用于连接 MySQL 服务器的一个库 PyMySQL 遵循 Python 数据库 API v2.0 规范,并包含了 pure-Python MySQL 客户端库。 如果还未安装,我们可以使用以下命令安装最新版的

PyMySQL 是在 Python3.x 版本中用于连接 MySQL 服务器的一个库

PyMySQL 遵循 Python 数据库 API v2.0 规范,并包含了 pure-Python MySQL 客户端库。

如果还未安装,我们可以使用以下命令安装最新版的 PyMySQL:

1

pip install PyMySQL

pymysql github地址

下面看下PyMySQL的基本操作,

1、查找数据

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

import pymysql

# 简单的查找

# 连接数据库

    conn = pymysql.connect(host='127.0.0.1', user='root', password='******', database='authoritydb')

# cursor=pymysql.cursors.DictCursor,是为了将数据作为一个字典返回

    cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)

    sql = 'select id,name from userinfo where id = %s'

# row返回获取到数据的行数

# 不能将查询的某个表作为一个参数传入到SQL语句中,否则会报错

# eg:sql = 'select id,name from %s'

# eg:row = cursor.excute(sql, 'userinfo') # 备注:userinfo是一个表名

# 像上面这样做就会报SQL语法错误,正确做法如下:

    row = cursor.execute(sql, 1)

# fetchall()(获取所有的数据),fetchmany(size)(size指定获取多少条数据),fetchone()(获取一条数据)

    result = cursor.fetchall()

    cursor.close()

    conn.close()

# 最后打印获取到的数据

    print(result)

 

# 补充

# 传入多个数据时

    sql = 'select id,name from userinfo where id>=%s and id<=%s'

    row = cursor.executemany(sql, [(1, 3)])

# 以字典方式传值

    sql = 'select id,name from userinfo where id>=%(id)s or name=%(name)s'

    rows = cursor.execute(sql, {'id': id, 'name': name})

     

# -------------------------------------------------

    import pymysql

# 复杂一点的查找,与MySQL的语句格式一样

    connect = pymysql.connect(host='localhost', user='root', password='****', database='authoritydb')

    cursor = connect.cursor(cursor=pymysql.cursors.DictCursor)

    username = input('username: ')

    sql = 'select A.name,authorityName from (select name,aid from userauth left join userinfo on' \

          ' uid=userinfo.id where name=%s) as A left join authority on authority.id=A.aid'

    row = cursor.execute(sql, username)

    result = cursor.fetchmany(row)

    cursor.close()

    connect.close()

    print(result)

 

# 调用函数

import pymysql

# 函数已经在mysql数据库中创建,这里只调用

# 函数的创建请访问后面的网址(https://blog.csdn.net/qq_43102443/article/details/107349451).

# in (指在创建函数时指定的参数只能输入)

    connect = pymysql.connect(host='localhost', user='root', password='******', database='schooldb')

    cursor = connect.cursor(cursor=pymysql.cursors.DictCursor)

    r = cursor.callproc('p2', (10, 5))

    result_1 = cursor.fetchall()

# 这个函数返回两个结果集

# 换到另一个结果集,在进行获取值

    cursor.nextset()

    result_2 = cursor.fetchall()

    cursor.close()

    connect.close()

     

    print('学生:', result_1, '\n老师:', result_2)

 

# out (指在创建函数时指定的参数只能输出)

    connect = pymysql.connect(host='localhost', user='root', password='*****', database='schooldb')

    cursor = connect.cursor(cursor=pymysql.cursors.DictCursor)

# 调用函数

    r = cursor.callproc('p3', (8, 0))

    result_1 = cursor.fetchall()

# 查询函数的第二个参数的值(从零开始计数)

    cursor.execute('select @_p3_1')

    result_2 = cursor.fetchall()

    cursor.close()

    connect.close()

     

    print(result_1, '\n', result_2)

 

# inout(指在创建函数时指定的参数既能输入,又能输出)

    connect = pymysql.connect(host='localhost', user='root', password='l@l19981019', database='schooldb')

    cursor = connect.cursor(cursor=pymysql.cursors.DictCursor)

    r = cursor.callproc('p4', (10, 0, 2))

    result_1 = cursor.fetchall()

    cursor.execute('select @_p4_1,@_p4_2')

    result_2 = cursor.fetchall()

    cursor.close()

    connect.close()

    print(result_1, '\n', result_2)

2、添加数据

用PyMySQL进行数据的增、删、改时,记得最后要commit()进行提交,这样才能保存到数据库,否则不能

1

2

3

4

5

6

7

8

9

    connect = pymysql.Connect(host='localhost', user='root', password='******', database='schooldb')

    cursor = connect.cursor(cursor=pymysql.cursors.DictCursor)

    student_id, course_id, number = input('student_id,course_id,number[eg:1 2 43]: ').split()

    sql = 'insert into score(student_id,course_id,number) values(%(student_id)s,%(course_id)s,%(number)s)'

    rows = cursor.execute(sql,{'student_id': student_id, 'course_id': course_id, 'number': number})

# 这里一定要提交

    cursor.commit()

    cursor.close()

    connect.close()

3、删除数据

1

2

3

4

5

6

7

8

9

connect = pymysql.Connect(host='localhost', user='root', password='******', database='schooldb')

    cursor = connect.cursor(cursor=pymysql.cursors.DictCursor)

    student_id = input('student_id: ')

    sql = 'delete from student where sid=%s'

    rows = cursor.execute(sql, student_id)

# 这里一定要提交

    cursor.commit()

    cursor.close()

    connect.close()

4、更改数据

1

2

3

4

5

6

7

8

9

connect = pymysql.Connect(host='localhost', user='root', password='******', database='schooldb')

    cursor = connect.cursor(cursor=pymysql.cursors.DictCursor)

    id, name = input('id, name: ').split()

    sql = "update userinfo set name=%s where id=%s"

    rows = cursor.executemany(sql, [(name, id)])

# 这里一定要提交

    cursor.commit()

    cursor.close()

    connect.close()


版权声明 : 本文内容来源于互联网或用户自行发布贡献,该文观点仅代表原作者本人。本站仅提供信息存储空间服务和不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权, 违法违规的内容, 请发送邮件至2530232025#qq.cn(#换@)举报,一经查实,本站将立刻删除。
原文链接 : https://blog.csdn.net/qq_43102443/article/details/107394794
相关文章
  • python实现华氏温度和摄氏温度转换的方法

    python实现华氏温度和摄氏温度转换的方法
    华氏温度和摄氏温度转换 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 TempStr=input(请输入带有符号的温度值,C/c表示摄氏度、F/f表示华氏温度:) if Te
  • Python中PyMySQL的基本操作介绍
    PyMySQL 是在 Python3.x 版本中用于连接 MySQL 服务器的一个库 PyMySQL 遵循 Python 数据库 API v2.0 规范,并包含了 pure-Python MySQL 客户端库。 如果还未
  • 四种Python机器学习超参数搜索方法总结
    在建模时模型的超参数对精度有一定的影响,而设置和调整超参数的取值,往往称为调参。 在实践中调参往往依赖人工来进行设置调整范围
  • python yield迭代器介绍
    在python深度学习模型读取数据时,经常遇到yield,互联网搜索后,将比较容易理解的说明记录一下。 二、使用步骤 1.引入库 代码如下(示例
  • python实现一个围棋小游戏

    python实现一个围棋小游戏
    一道Python课作业题,大致如下: 编写一个类: 该类Building应具有以下方法: ●一个构造函数,它根本不接受任何参数(除了通常的`self`) ●s
  • Pycharm配置anaconda环境图文教程

    Pycharm配置anaconda环境图文教程
    1.配置anaconda环境 以下内容在下载完anaconda后实现 快捷键win+r,打出命令行cmd,进入黑色 界面 输入conda info --env,跳出以下内容: 如果没有 则
  • Python设置环境字体方法教程

    Python设置环境字体方法教程
    Python设置环境字体方法 第一步:首先,打开Python,然后点击Options。 第二步:然后,我们就能点击下拉菜单中的Settings。 第三步:此时,我
  • Python Django教程之实现新闻应用程序

    Python Django教程之实现新闻应用程序
    Django是一个用Python编写的高级框架,它允许我们创建服务器端Web应用程序。在本文中,我们将了解如何使用Django创建新闻应用程序。 我们将
  • 书写Python代码的一种更优雅方式(推荐!)

    书写Python代码的一种更优雅方式(推荐!)
    一些比较熟悉pandas的读者朋友应该经常会使用query()、eval()、pipe()、assign()等pandas的常用方法,书写可读性很高的「链式」数据分析处理代码
  • Python灰度变换中伽马变换分析实现

    Python灰度变换中伽马变换分析实现
    1. 介绍 伽马变换主要目的是对比度拉伸,将图像灰度较低的部分进行修正 伽马变换针对的是对单个像素点的变换,也就是点对点的映射 形
  • 本站所有内容来源于互联网或用户自行发布,本站仅提供信息存储空间服务,不拥有版权,不承担法律责任。如有侵犯您的权益,请您联系站长处理!
  • Copyright © 2017-2022 F11.CN All Rights Reserved. F11站长开发者网 版权所有 | 苏ICP备2022031554号-1 | 51LA统计