python 操作 postgreSQL 数据库 Demo ( 基于PyGreSQL 包)

安装

安装命令 sudo pip install PyGreSQL

NOTE: 可能报如下错误

Traceback (most recent call last):
      File "", line 1, in 
      File "/tmp/pip-build-f61ovl/PyGreSQL/setup.py", line 88, in 
        pg_version = pg_version()
      File "/tmp/pip-build-f61ovl/PyGreSQL/setup.py", line 82, in pg_version
        match = re.search(r'(\d+)\.(\d+)', pg_config('version'))
      File "/tmp/pip-build-f61ovl/PyGreSQL/setup.py", line 74, in pg_config
        raise Exception("pg_config tool is not available.")
    Exception: pg_config tool is not available.

Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-f61ovl/PyGreSQL/

解决办法 sudo apt-get install libpq-dev

使用

见官方文档 www.pygresql.org

DEMO

from pg import DB
db = DB(dbname='test', host='localhost', port=5432, user='user1', passwd='123456')
sql = "select * from test_table limit 10"
q = db.query(sql)
result_list = q.getresult()  

NOTE:这里的 SQL 语句是 postgreSQL, 参考 www.postgresql.org

你可能感兴趣的:(PostgreSQL,Python,计算机语言集)