首页 > 数据库 >pymysql 模块演练代码

pymysql 模块演练代码

时间:2024-05-31 14:24:20浏览次数:17  
标签:execute sname student pymysql cursor 模块 sid 演练 conn

import pymysql
from pymysql.cursors import DictCursor

conn = pymysql.connect(host='localhost', user='root', password='123456', database='day1', port=3306,
cursorclass=DictCursor, connect_timeout=3)
cursor = conn.cursor()

【直接用sql语句进行增删改查】

增加

sql2 ='insert into student(gender,sname,class_id) values("男","abc",1)'

cursor.execute(sql2)

conn.commit()

修改

sql3 ='update student set sname="bcd" where sid=1'

cursor.execute(sql3)

conn.commit()

删除

sql4 ='delete from student where sid=17'

cursor.execute(sql4)

conn.commit()

【用sql语句占位符进行增删改查】

增加

sql2 ='insert into student(gender,sname,class_id) values(%s,%s,%s)'

cursor.execute(sql2,('男','edf',2))

conn.commit()

修改

sql3 ='update student set sname=%s where sid=%s'

cursor.execute(sql3,('abd',1))

conn.commit()

删除

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

cursor.execute(sql4,'18')

conn.commit()

【用sql语句关键字占位符进行增删改查】

增加

sql2 = 'insert into student(gender,sname,class_id) values(%(gender)s,%(sname)s,%(class_id)s)'

cursor.execute(sql2, {'gender': '男', "sname": 'edf', "class_id": 2})

conn.commit()

修改

sql3 ='update student set sname=%(sname)s where sid=%(sid)s'

cursor.execute(sql3,{'sname':'亲爱',"sid":19})

conn.commit()

删除

sql4 ='delete from student where sid=%(sid)s'

cursor.execute(sql4,{"sid":'19'})

conn.commit()

sql = 'select * from student'
cursor.execute(sql)
result = cursor.fetchall()

print(conn)
print(result)

标签:execute,sname,student,pymysql,cursor,模块,sid,演练,conn
From: https://www.cnblogs.com/zenopan101861/p/18224476

相关文章

  • BOSHIDA AC/DC电源模块:应用于工业自动化领域
    BOSHIDAAC/DC电源模块:应用于工业自动化领域AC/DC电源模块是一种用来将交流电转换为直流电的电源模块。它在工业自动化领域有着广泛的应用,可以为各种设备和系统提供稳定可靠的电力供应。 一,AC/DC电源模块在工业自动化领域中起到了稳定电源供应的作用。在工业自动化过程中,许多......
  • Pymysql
    Pymysql安装:pipinstallpymysql导入模块:importpymysql创建连接对象:conn=pymysql.connect(user=root,password='',host='localhost',database='',port=3306,charset='',cursorclass=DictCursor)#cursorclass不指定返回结果就是元组,Dictcu......
  • 低代码开发平台(Low-code Development Platform)的模块组成部分
    低代码开发平台(Low-codeDevelopmentPlatform)的模块组成部分主要包括以下几个方面:低代码开发平台的模块组成部分可以按照包含系统、模块、菜单组织操作行为等维度进行详细阐述。以下是从这些方面对平台模块组成部分的说明:包含系统低代码开发平台本身作为一个完整的系统,包含......
  • QShop商城-添加新模块/页面(CodeSmith代码生成)
    QShop商城-添加新模块/页面(CodeSmith代码生成)工具准备CodeSmith当前使用的CodeSmith71,如找不到我已分享至:https://pan.baidu.com/s/1O2Tqg3gnbToAzjH0T5ETcg?pwd=2wcv下载后按照文档CodeSmith7激活教程.doc内容激活CodeSmith 代码生成下载激活后,双击TemplateEd......
  • 【Python快速上手(三十四)】- Python math 模块
    目录Python快速上手(三十四)-Pythonmath模块Pythonmath模块详解1.导入math模块2.基本数学运算3.三角函数4.双曲函数5.特殊函数6.浮点运算辅助函数7.常量8.实际应用案例9.小结Python快速上手(三十四)-Pythonmath模块Pythonmath模块详解math模块......
  • java单元测试:spring测试模块
    Spring测试模块为开发者提供了一套强大的工具,用于在Spring应用中进行单元测试、集成测试和端到端测试。1.测试框架集成Spring测试模块与多个测试框架集成,最常用的是JUnit5和TestNG。JUnit5集成使用@ExtendWith(SpringExtension.class)注解来启用Spring测......
  • BEM规范 电商订单模块UI设计实战-- CSS篇(一)
    BEM规范|电商订单模块UI设计实战--CSS篇(一)引言我最近专注于JavaScript的学习,暂时搁置了CSS。然而,我偶然发现了BEM(BlockElementModifier)命名规范,并且注意到我们日常使用的微信界面UI遵循了这一规范。虽然看起来有些杂乱,但这是WeUI的一部分,展示了众多组件。这让我思......
  • Lua封装函数模块并由其他模块调用
    在Lua中,封装函数通常意味着将一组相关的功能组织在一起,然后通过模块的形式提供给其他脚本调用。Lua的模块可以是简单的脚本文件,也可以是返回一个表的脚本,表中包含了模块的所有公共函数和变量。以下是创建和使用Lua模块的基本步骤:1.创建模块创建一个Lua文件,比如mymodule.lua,并......
  • Verilog HDL中如何控制模块的调用与否
    VerilogHDL中如何控制模块的调用与否(实用)语言:VerilgHDLEDA工具:ISE、Vivado、QuartusIIVerilogHDL中如何控制模块的调用与否(实用)一、引言二、模块调用与否的几种方法1.注释2.使用预处理指令`ifdef3.使用generate语句三、结尾关键词:调用,VerilogHDL......
  • Nginx反向代理之 upstream 模块
    upstream模块的内容应放于nginx.conf配置的http{}标签内,其默认的调度算法是rr(轮循round-robin)ngx_http_upstream_module模块官方文档upstream模块内部server标签参数说明#提示:以上的参数和专业的haproxy参数类似,但不如haproxy的参数易懂。upstream模块调度算......