增删改数据必须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. C++ GUI Qt4编程(07)-3.1menu

    1. C++ GUI Qt4编程第三章,添加menu菜单. 2. mainwindow.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include < ...

  2. RNN(recurrent neural network)学习笔记

    参考:https://www.jianshu.com/p/9dc9f41f0b29  以及<白话深度学习与TensorFlow> 与前馈神经网络.卷积神经网络等不同之处在于,RNN具有一定 ...

  3. GreenPlum 大数据平台--安装

    1. 环境准备 01, 安装包准备: Greenplum :  >>>>链接地址 Pgadmin客户端 :  >>>链接地址 greenplum-cc-web ...

  4. 这真的该用try-catch吗?

    前言 我有个技能,就是把“我”说的听起来特别像“老子”. 以前是小喽啰的时候,会跟领导说“我!不加班.”,听起来就像“老子不加班!”一样.到最后发现,我确实没有把计划内的工作拖到需要加班才能完成,这个 ...

  5. IDEA 14.0 (默认模式) 快捷键

    IDEA 14.0 (默认模式) 快捷键 1.Alt+Shift+R:重命名变量名.类名.方法名(使用已经使用过的) 2.Ctrl+O :重写方法 3.Alt+Shift+P :实现接口 4.Alt+ ...

  6. pat1012. The Best Rank (25)

    1012. The Best Rank (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue To eval ...

  7. 【卷土重来之C#学习笔记】(三) 类型 存储和变量

    .c#程序是一组类型声明 ※C#程序或DLL的源代码是一组一种或多种类型声明 ※对于可执行程序,类型声明必须有一个包含Main方法类 ※命名空间是一种把相关的类型声明分组并命名的方法.既然程序是一组相 ...

  8. fabric省略输出

    fab -f test_fabric.py start --hide status,running,stdout,user,aborts,warnings,stderr 省略所有输出--hide st ...

  9. 深入理解JavaScript系列(6):S.O.L.I.D五大原则之单一职责SRP

    前言 Bob大叔提出并发扬了S.O.L.I.D五大原则,用来更好地进行面向对象编程,五大原则分别是: The Single Responsibility Principle(单一职责SRP) The ...

  10. maven 基本配置

    <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...