pymssql數(shù)據(jù)庫(kù)操作MSSQL2005實(shí)例分析
本文實(shí)例講述了pymssql數(shù)據(jù)庫(kù)操作MSSQL2005的方法。分享給大家供大家參考。具體如下:
使用的MSSQL2005,通過(guò)pymssql來(lái)連接的。把可能用到的數(shù)據(jù)庫(kù)操作方式都總結(jié)如下,如果要用的時(shí)候就備查啦。
#!/usr/bin/env python #coding=utf-8 from __future__ import with_statement from contextlib import closing import inspect import pymssql import uuid import datetime #查詢操作 with closing(pymssql.connect(host='localhost',user='sa',password='pppp',database='blogs')) as conn : cur = conn.cursor() #SELECT 長(zhǎng)連接查詢操作(逐條方式獲取數(shù)據(jù)) sql = "select * from pcontent" cur.execute(sql) for i in range(cur.rowcount): print cur.fetchone() #SELECT 短鏈接查詢操作(一次查詢將所有數(shù)據(jù)取出) sql = "select * from pcontent" cur.execute(sql) print cur.fetchall() #INSERT sql = "INSERT INTO pcontent(title)VAlUES(%s)" uuidstr = str(uuid.uuid1()) cur.execute(sql,(uuidstr,)) conn.commit() print cur._result #INSERT 獲取IDENTITY(在插入一個(gè)值,希望獲得主鍵的時(shí)候經(jīng)常用到,很不優(yōu)雅的方式) sql = "INSERT INTO pcontent(title)VAlUES(%s);SELECT @@IDENTITY" uuidstr = str(uuid.uuid1()) cur.execute(sql,(uuidstr,)) print "arraysite:",cur.arraysize print cur._result[1][2][0][0]#不知道具體的做法,目前暫時(shí)這樣使用 conn.commit() #Update vl = '中國(guó)' sql = 'update pcontent set title = %s where id=1' cur.execute(sql,(vl,)) conn.commit() #參數(shù)化查詢這個(gè)是為了避免SQL攻擊的 sql = "select * from pcontent where id=%d" cur.execute(sql,(1,)) print cur.fetchall() # 調(diào)用存儲(chǔ)過(guò)程SP_GetALLContent 無(wú)參數(shù) sql = "Exec SP_GetALLContent" cur.execute(sql) print cur.fetchall() # 調(diào)用存儲(chǔ)過(guò)程SP_GetContentByID 有參數(shù)的 sql = "Exec SP_GetContentByID %d" cur.execute(sql,(3,)) print cur.fetchall() #調(diào)用存儲(chǔ)過(guò)程SP_AddContent 有output參數(shù)的(很不優(yōu)雅的方式) sql = "DECLARE @ID INT;EXEC SP_AddContent 'ddddd',@ID OUTPUT;SELECT @ID" cur.execute(sql) print cur._result
希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
pandas中std和numpy的np.std區(qū)別及說(shuō)明
這篇文章主要介紹了pandas中std和numpy的np.std區(qū)別及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08Python plt 利用subplot 實(shí)現(xiàn)在一張畫(huà)布同時(shí)畫(huà)多張圖
這篇文章主要介紹了Python plt 利用subplot 實(shí)現(xiàn)在一張畫(huà)布同時(shí)畫(huà)多張圖,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02python使用信號(hào)量動(dòng)態(tài)更新配置文件的操作
這篇文章主要介紹了python使用信號(hào)量動(dòng)態(tài)更新配置文件的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04關(guān)于python的bottle框架跨域請(qǐng)求報(bào)錯(cuò)問(wèn)題的處理方法
這篇文章主要介紹了關(guān)于python的bottle框架跨域請(qǐng)求報(bào)錯(cuò)問(wèn)題的處理方法,需要的朋友可以參考下2017-03-03用Python實(shí)現(xiàn)校園通知更新提醒功能
今天小編就為大家分享一篇用Python實(shí)現(xiàn)校園通知更新提醒功能,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11