HowTo:  Connect and run queries to a SQL Server database from Python

相关信息
Article ID: 40599
Software:
ArcSDE 10, 10.1
ArcGIS - ArcEditor 10
ArcGIS - ArcInfo 10
ArcGIS - ArcView 10
ArcGIS for Desktop Advanced 10.1
ArcGIS for Desktop Standard 10.1
ArcGIS for Desktop Basic 10.1
Platforms:
Windows Server 2008, Windows 7
RHEL 5, 6

问题描述
Connecting to SQL Server and making SQL queries can be incorporated into Python to aid common GIS geoprocessing tasks.
已邀请:

易智瑞技术支持

赞同来自:

解决方案
The procedures below describe how to connect to a SQL Server database and make SQL queries from Python.
  1. Download and install the appropriate pyodbc module for the installed Python release (for example, Python 2.6, 2.7) from the following URL:
    http://code.google.com/p/pyodbc/ 
    Since ArcGIS Desktop by default installs the 32-bit version of Python, install the 32-bit version of pyodbc.
  2. Make a connection to the SQL Server database using database authentication or Windows authentication by passing in the appropriate parameters such as the server name, user ID (UID) and password (PWD): Database authentication string:
    con = pyodbc.connect('DRIVER={SQL Server};SERVER=Prod1\SQL2008R2;DATABASE=SDE;UID=sa;PWD=sa')
    Windows authentication string: 
    con = pyodbc.connect('Trusted_Connection=yes', driver = '{SQL Server}',server = ‘Prod1\SQL2008R2 ‘, database = ‘SDE')
  3. Define a parameter to access the cursor method: 
    cur = con.cursor()
  4. Create a query string: 
    querystring = "select * into ParcelsA from ParcelsB"
  5. Pass the query string into the cursor method:
     cur.execute(querystring)con.commit()


其它相关参考
  1. Python ODBC Library


创建及修改时间
Created: 10/5/2012

Last Modified: 7/12/2013
原文链接
http://support.esri.com/en/kno ... 40599

要回复问题请先登录注册