[聚合文章] Python连接SQL Server数据获取

SQL Server 2017-12-21 15 阅读

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)

注:本文内容来自互联网,旨在为开发者提供分享、交流的平台。如有涉及文章版权等事宜,请你联系站长进行处理。