Python连接SQL Server数据获取

2017-12-22 11:24:12来源:http://www.cnblogs.com/jun-xi/p/8080490.html作者:Python_博客园人点击

分享

import pyodbc
conn=pyodbc.connect(r'DRIVER={SQL Server Native Client 10.0};SERVER=10.10.1.87;DATABASE=test_1;UID=qcm;PWD=qcm123')
cursor = conn.cursor()
cursor.execute("exec test")
row=cursor.fetchone()
if row:
print(row)
rows = cursor.fetchall()
for row in rows:
print(row.F1, row.F4)
for row in cursor:
print(row.F1, row.F4)
cursor.execute("delete from A20170816 where F4=?", '513902198511158151')
conn.commit()
cursor.close()
conn.close()
import pyodbc<br /><br />

不同的SQL server版本对应的DRIVER字段不同。对应关系如下:


{SQL Server} - released with SQL Server 2000


{SQL Native Client} - released with SQL Server 2005 (also known as version 9.0)
{SQL Server Native Client 10.0} - released with SQL Server 2008
{SQL Server Native Client 11.0} - released with SQL Server 2012
conn=pyodbc.connect(r'DRIVER={SQL Server Native Client 10.0};SERVER=10.10.1.87;DATABASE=test_1;UID=qcm;PWD=qcm123')<br />cursor = conn.cursor()<br />cursor.execute("select * from A20170816")
row=cursor.fetchone()<br />if row:<br />print(row)
<br /><br />rows = cursor.fetchall()<br />for row in rows:<br />print(row.F1, row.F4)

最新文章

123

最新摄影

闪念基因

微信扫一扫

第七城市微信公众平台