Psycopg

来自开放百科 - 灰狐
跳转到: 导航, 搜索

A DB-API 2.0 compliant driver designed to support heavily multithreaded applications with many cursors. Cursors can be very short-lived since the driver has an intelligent system for reusing db connections at libpq level. Supports thread level 2.

Install

Debian

#apt-get install python2.3-psycopg // Python 2.3 module for PostgreSQL
or 
wget http://initd.org/pub/software/psycopg/psycopg2-2.0.2.tar.gz
tar zxvf  psycopg2-2.0.2.tar.gz
cd psycopg2-2.0.2
python setup.py install

可能出现的错误: ./psycopg/connection.h:27:22: error: libpq-fe.h: No such file or directory. postgresql-dev 没有安装

wget ftp://ftp.relline.ru/pub/unix/centos-5/os/x86_64/CentOS/postgresql-devel-8.1.9-1.el5.i386.rpm
# rpm -ivh postgresql-devel-8.1.9-1.el5.i386.rpm 

问题解决 :)

import psycopg2
import psycopg2.extensions
import logging
class LoggingCursor(psycopg2.extensions.cursor):
    def execute(self, sql, args=None):
        logger = logging.getLogger('sql_debug')
        logger.info(self.mogrify(sql, args))
        try:
            psycopg2.extensions.cursor.execute(self, sql, args)
        except Exception, exc:
            logger.error("%s: %s" % (exc.__class__.__name__, exc))
            raise
DSN = "dbname='mydb' user='postgres' host='localhost' password='postgres'"
conn = psycopg2.connect(DSN)
curs = conn.cursor(cursor_factory=LoggingCursor)
curs.execute("select * from products")
curs.fetchone()

链接

Comment-32x32.png

<discussion>characters_max=300</discussion>

分享您的观点
个人工具
名字空间

变换
操作
导航
工具箱