使用python脚本访问PostgresSQL
需要安装驱动程序
在windows下安装 psycopg2-2.2.2.win32-py2.6-pg9.0.1-release.exe 就可以了
在liunx 下也很简单,下载个 psycopg2-2.2.2.tar.gz
[color=blue]tar zxf psycopg2-2.2.2.tar.gz
cd psycopg2-2.2.2
python setup.py install[/color]
如果报错,就按照报错信息安装缺失的包就可以了。
编写python脚本时,找开头写上:
import psycopg2
就可以了.
举个例子:
pgconn.py
[color=blue]import psycopg2
conn = psycopg2.connect( host='10.20.134.192',database='postgres', user='postgres', password='postgres' )
cur = conn.cursor()
cur.execute( "select id ,name from test" )
for item in cur.fetchall():
print item
cur.close()
conn.close() [/color]
需要安装驱动程序
在windows下安装 psycopg2-2.2.2.win32-py2.6-pg9.0.1-release.exe 就可以了
在liunx 下也很简单,下载个 psycopg2-2.2.2.tar.gz
[color=blue]tar zxf psycopg2-2.2.2.tar.gz
cd psycopg2-2.2.2
python setup.py install[/color]
如果报错,就按照报错信息安装缺失的包就可以了。
编写python脚本时,找开头写上:
import psycopg2
就可以了.
举个例子:
pgconn.py
[color=blue]import psycopg2
conn = psycopg2.connect( host='10.20.134.192',database='postgres', user='postgres', password='postgres' )
cur = conn.cursor()
cur.execute( "select id ,name from test" )
for item in cur.fetchall():
print item
cur.close()
conn.close() [/color]