日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python入门23 pymssql模块(python连接sql server增删改数据 )

發(fā)布時間:2023/11/30 python 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python入门23 pymssql模块(python连接sql server增删改数据 ) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

增刪改數(shù)據(jù)必須connect.commit()才會生效?

回滾函數(shù) connect.rollback()

?

連接數(shù)據(jù)庫

''' dinghanhua sql server增刪改 '''import pymssqlserver = '192.168.1.1' user = 'user' password = '111111' database = 'test'dbconnect = pymssql.connect(server = server,user = user,password=password,database = database) #連接到數(shù)據(jù)庫

?

修改數(shù)據(jù)

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() #增刪改數(shù)據(jù)后必須commit

?

刪除數(shù)據(jù)

dbcursor.execute("delete from test_student where sno =1",'peter pan') dbconnect.commit() #增刪改數(shù)據(jù)后必須commit

?

新增數(shù)據(jù)

dbcursor.execute("insert into test_teacher values(%d,%s,%s)",(2,'xingxing','111111')) dbconnect.commit() #增刪改數(shù)據(jù)后必須commit

?

commit多個

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

commit之前,游標再執(zhí)行select取出的都是未提交的數(shù)據(jù)

?

rollback()

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

?

最后關閉連接

dbcursor.close() dbconnect.close()

?

commit之后數(shù)據(jù)庫數(shù)據(jù)已變更,回滾是無效的,必須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") #假設忘記加deletedbcursor.execute("select count(1) from test_teacher") #查詢下刪除后數(shù)據(jù)個數(shù)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','1'),(8,'yy','2'),(9,'zz','3')])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','111111'))dbconnect.rollback() #回滾dbcursor.execute("update test_student set name =%s where sno =2",'peter234')dbcursor.execute("select * from test_student")print(dbcursor.fetchall()) #取出的是未提交的數(shù)據(jù) dbconnect.commit()

?

the end!

?

轉載于:https://www.cnblogs.com/dinghanhua/p/9996693.html

總結

以上是生活随笔為你收集整理的python入门23 pymssql模块(python连接sql server增删改数据 )的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。