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

相关信息
Article ID: 40634
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 2003, Vista, Server 2008, Windows 7
RHEL 5, 6

问题描述
Connecting to PostgreSQL databases from Python provides the ability to run essential SQL queries that can be utilized in geoprocessing tasks.
已邀请:

EsriSupport

赞同来自:

解决方案
Instructions provided describe how to connect to a PostgreSQL database and run SQL queries from a Python script.


  1. Download and install the appropriate psycopg2 module for the installed Python release (for example, Python 2.6, 2.7, etc.):http://www.stickpeople.com/pro ... copg/
  2. Import the module in the Python script: import psycopg2
  3. Make a connection to a PostgreSQL database by passing in the appropriate user/password to the following connection string: connection = psycopg2.connect(host='prod', database='sde', user='sde', password='sde')
  4. Define a parameter to access the cursor method: cursor = connection.cursor()
  5. Create a query string and pass to cursor method: cursor.execute('select * from PARCELS WHERE OBJECTID < 70000')
  6. Create a for loop and print results for query in cursor: print str(query)


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

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

要回复问题请先登录注册