使用pymssql模塊操作SQL Server數(shù)據(jù)庫(kù)
一,連接數(shù)據(jù)庫(kù)
使用pymssql連接SQL Server數(shù)據(jù)庫(kù),首先創(chuàng)建連接和游標(biāo):
import pymssql conn = pymssql.connect(host= ' host ' ,user= ' user ' ,password= ' pwd ' ,database= ' db_name ' ) cursor = conn.cursor()
1,行的格式
當(dāng)執(zhí)行select語(yǔ)句獲取數(shù)據(jù)時(shí),返回的數(shù)據(jù)行有兩種格式:元組和字典,行的默認(rèn)格式是元組。pymssql返回的數(shù)據(jù)集的格式是在創(chuàng)建游標(biāo)時(shí)設(shè)置的,當(dāng)參數(shù) as_dict為True時(shí),返回的行是字典格式,該參數(shù)的默認(rèn)值是False,因此,默認(rèn)的行格式是元組。
cursor = conn.cursor(as_dict=True)
2,執(zhí)行查詢
使用游標(biāo)執(zhí)行SQL語(yǔ)句
cursor.execute( " sql statement " )
3,提交事務(wù)
當(dāng)執(zhí)行更新操作時(shí),需要顯式調(diào)用 commit()函數(shù)來(lái)提交事務(wù):
conn.commit()
如果事務(wù)執(zhí)行失敗,可以回滾事務(wù):
conn.rollback()
4,關(guān)閉連接,釋放資源
conn.close()
二,更新數(shù)據(jù)
對(duì)數(shù)據(jù)庫(kù)有查詢,更新和新增操作,在更新和插入數(shù)據(jù)時(shí),需要顯式提交事務(wù)。當(dāng)需要更新數(shù)據(jù)時(shí),調(diào)用游標(biāo)的execute()函數(shù)執(zhí)行SQL命令來(lái)實(shí)現(xiàn):
Cursor.execute(operation)
Cursor.execute(operation, params)
如果要在一個(gè)事務(wù)中執(zhí)行多條SQL命令,可以調(diào)用游標(biāo)的executemany()函數(shù):
Cursor.executemany(operation, params_seq)
1,執(zhí)行數(shù)據(jù)更新和刪除
通過(guò)游標(biāo)的execute()函數(shù)來(lái)執(zhí)行TSQL語(yǔ)句,調(diào)用 commit() 來(lái)提交事務(wù)
cursor.execute( """ sql statement """ ) conn.commit()
2,執(zhí)行數(shù)據(jù)的多行插入
如果需要插入多條記錄,可以使用游標(biāo)的executemany()函數(shù),該函數(shù)包含模板SQL 命令和一個(gè)格式化的參數(shù)列表,用于在一條事務(wù)中插入多條記錄:
args=[(1, ' John Smith ' , ' John Doe ' ), ( 2, ' Jane Doe ' , ' Joe Dog ' ), ( 3, ' Mike T. ' , ' Sarah H. ' )] cursor.executemany( " INSERT INTO persons VALUES (%d, %s, %s) " , args ) conn.commit()
三,遍歷數(shù)據(jù)
當(dāng)從SQL Server數(shù)據(jù)庫(kù)中獲取數(shù)據(jù)時(shí),可以使用fetchone()從結(jié)果集中提取一條記錄,使用fetchmany()從結(jié)果集中提取多條記錄,或使用fetchall()提取全部結(jié)果集:
cursor.fetchone() # return a row (a tuple or a dict) if as_dict was passed to pymssql.connect() cursor.fetchmany(size=None) # return a list of tuples or dicts if as_dict was passed to pymssql.connect() cursor.fetchall() # return a list of tuples or dicts if as_dict was passed to pymssql.connect()
由于游標(biāo)是一個(gè)迭代器,因此,可以使用for語(yǔ)句以迭代方式逐行處理查詢的結(jié)果集。
for row in cursor:
1,以元組方式遍歷數(shù)據(jù)
返回的結(jié)果集中,每一行都是一個(gè)元組:
cursor= connect.cursor() cursor.execute( ' SELECT * FROM persons WHERE salesrep=%s ' , ' John Doe ' ) for row in cursor: print ( ' row = %r ' % (row,))
2,以字典方式遍歷數(shù)據(jù)
返回的結(jié)果集,每一行是一個(gè)字典結(jié)構(gòu):
cursor = conn.cursor(as_dict= True) cursor.execute( ' SELECT * FROM persons WHERE salesrep=%s ' , ' John Doe ' ) for row in cursor: print ( " ID=%d, Name=%s " % (row[ ' id ' ], row[ ' name ' ]))
四,關(guān)閉連接
在一個(gè)連接中,可以提交多個(gè)事務(wù),當(dāng)查詢完成之后,一定要關(guān)閉連接:
conn.close()
通常情況下,使用with來(lái)自動(dòng)關(guān)閉連接:
import pymssql with pymssql.connect(server, user, password, " db_name " ) as conn: with conn.cursor(as_dict = True) as cursor: cursor.execute( ' SELECT * FROM persons WHERE salesrep=%s ' , ' John Doe ' ) for row in cursor: print ( " ID=%d, Name=%s " % (row[ ' id ' ], row[ ' name ' ]))
?
參考文檔:
pymssql reference
Python中從SQL型數(shù)據(jù)庫(kù)讀寫dataframe型數(shù)據(jù)
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
