增删改数据必须connect.commit()才会生效

回滚函数 connect.rollback()

连接数据库

'''
dinghanhua
sql server增删改
''' import pymssql server = '192.168.1.1'
user = 'user'
password = ''
database = 'test' dbconnect = pymssql.connect(server = server,user = user,password=password,database = database) #连接到数据库

修改数据

dbcursor.execute("update test_student set name =%s where sno =1",'peter pan')
dbcursor.execute("update test_student set name =%s where sno =2",'silina smith')
dbconnect.commit() #增删改数据后必须commit

删除数据

dbcursor.execute("delete from test_student  where sno =1",'peter pan')
dbconnect.commit() #增删改数据后必须commit

新增数据

dbcursor.execute("insert into test_teacher values(%d,%s,%s)",(2,'xingxing',''))
dbconnect.commit() #增删改数据后必须commit

commit多个

dbcursor.execute("insert into test_teacher values(%d,%s,%s)",(6,'xingxing6',''))
dbcursor.execute("update test_student set name =%s where sno =2",'peter Panpan')
dbconnect.commit() #提交多个

commit之前,游标再执行select取出的都是未提交的数据

rollback()

dbcursor.execute("insert into test_teacher values(%d,%s,%s)",(7,'xingxing7',''))
dbconnect.rollback() #回滚
dbcursor.execute("update test_student set name =%s where sno =2",'peter')
dbconnect.commit() #提交

最后关闭连接

dbcursor.close()
dbconnect.close()

commit之后数据库数据已变更,回滚是无效的,必须commit之前回滚。commit之前可以做下判断。

#避免delete或update未加where语句
with pymssql.connect(server = server,user = user,password=password,database = database) as dbconnect:
with dbconnect.cursor() as dbcursor:
dbcursor.execute("delete from test_teacher") #假设忘记加delete dbcursor.execute("select count(1) from test_teacher") #查询下删除后数据个数
if dbcursor.fetchone()[0] == 0:
dbconnect.rollback() #回滚
else:
dbconnect.commit()

cursor.executemany()

with  pymssql.connect(server = server,user = user,password=password,database = database)  as dbconnect:
with dbconnect.cursor() as dbcursor:
dbcursor.executemany("insert into test_teacher values (%s,%s,%s)",
[(7,'xx',''),(8,'yy',''),(9,'zz','')])
dbconnect.commit()

with as  替代手工关闭

with  pymssql.connect(server = server,user = user,password=password,database = database)  as dbconnect:
with dbconnect.cursor(as_dict=True) as dbcursor: dbcursor.execute("insert into test_teacher values(%d,%s,%s)",(7,'xingxing7',''))
dbconnect.rollback() #回滚
dbcursor.execute("update test_student set name =%s where sno =2",'peter234') dbcursor.execute("select * from test_student")
print(dbcursor.fetchall()) #取出的是未提交的数据 dbconnect.commit()

the end!

python入门23 pymssql模块(python连接sql server增删改数据 )的更多相关文章

  1. Python基于Pymssql模块实现连接SQL Server数据库的方法

    首先,安装pymssql第三方库pip install pymssql 其次,导入pymssql库 最后们就可以连接数据库了 import pymssql server = "10.10.9 ...

  2. Python 学习 第17篇:从SQL Server数据库读写数据

    在Python语言中,从SQL Server数据库读写数据,通常情况下,都是使用sqlalchemy 包和 pymssql 包的组合,这是因为大多数数据处理程序都需要用到DataFrame对象,它内置 ...

  3. SQL Server 增删改

    --use用来设置当前使用哪个数据库use StudentDb--go批处理go --T-SQL中不区分大小写,数据库表中的数据是区分大小写的--例如:insert与INSERT不区分大小写,数据库表 ...

  4. MS SQL Server 增删改查

    数据插入 语法:INSERT INTO Table_name(field1,field2……fieldN) values(value1,vlaue2,…valueN) 单行插入用户类型 INSERT ...

  5. python入门22 pymssql模块(python连接sql server查询)

    安装 pip install pymssql 连接数据库 pymssql.connect() # coding:utf-8 import pymssql server = '192.168.8.1' ...

  6. 连接sql server、插入数据、从数据库获取时间(C#)

    using System; using System.Data.SqlClient; namespace Test { //连接数据库 public class Connection { privat ...

  7. sql server 增删改(查太多了)

    表: 学生(*学号,姓名,性别,年龄,专业) create table student( sno ) primary key, sname ) not null, ssex ), sage small ...

  8. python 使用pymssql连接sql server数据库

    python 使用pymssql连接sql server数据库   #coding=utf-8 #!/usr/bin/env python#------------------------------ ...

  9. python 连接sql server

    linux 下pymssql模块的安装 所需压缩包:pymssql-2.1.0.tar.bz2freetds-patched.tar.gz 安装: tar -xvf pymssql-2.1.0.tar ...

随机推荐

  1. Oracle的pipelined函数实现高性能大数据处理

    从Oracle 8开始,我们就可以从一个collection类型的数据集合中查询出数据,这个集合称之为"虚拟表".它的方法是"SELECT FROM TABLE(CAST ...

  2. SPOJ - AMR11

    A Thanks a lot for helping Harry Potter in finding the Sorcerer's Stone of Immortality in October. D ...

  3. linux 入门测验

    cd . 当前目录.. 返回上一级目录 ../../../返回多级目录 grep "目标信息" 目标地址 -v :显示没有被匹配的信息 mkdir -p:创建多级目录 mkdir ...

  4. pipline --学习 (-)

    一,语法: 编写位置: pipline 启动docker pipeline { agent { docker 'maven:3.3.3' } stages { stage('build') { ste ...

  5. 更改CMD默认的初始路径

    一直用CMD开启本地服务,每一次都得切换路径,有点尴尬.记录一下,修改CMD默认路径 1.打开注册表编辑器(WIN+R打开运行.输入regedit,或者直接找到路径,双击打开C:\Windows\re ...

  6. 深入浅出理解linux inode结构

    一.inode是什么? 参考文档:http://tech.diannaodian.com/dw/lin/2012/0112/154629.html 做Android底层驱动或者嵌入式Linux的程序猿 ...

  7. HikariCP配置使用spring结合--Java数据库连接池

    我的个人德州扑克项目https://github.com/mingzijian/pokers,欢迎给星星.maven引入: Java 8 maven artifact: <dependency& ...

  8. unity项目架构

    Unity 游戏框架搭建 (一) 概述Unity 游戏框架搭建 (二) 单例的模板Unity 游戏框架搭建 (三) MonoBehaviour单例的模板Unity 游戏框架搭建 (四) 简易有限状态机 ...

  9. 深入理解JavaScript系列(20):《你真懂JavaScript吗?》答案详解

    介绍 昨天发的<大叔手记(19):你真懂JavaScript吗?>里面的5个题目,有很多回答,发现强人还是很多的,很多人都全部答对了. 今天我们来对这5个题目详细分析一下,希望对大家有所帮 ...

  10. Unity 基础

    Unity 基础是unity入门的关键.他将讲解Unity的界面, 菜单项,使用资源,创设场景,并发布版本. 当你读完这段,你将理解unity是怎么工作的,如何有效地使用它,并且完成一个基本的游戏. ...