欢迎大家赞助一杯啤酒🍺 我们准备了下酒菜:Formal mathematics/Isabelle/ML, Formal verification/Coq/Agda, C++/Erlang/Lisp
Psycopg
来自开放百科 - 灰狐
(版本间的差异)
第10行: | 第10行: | ||
python setup.py install | python setup.py install | ||
+ | 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() | ||
*http://initd.org/projects/psycopg1 | *http://initd.org/projects/psycopg1 | ||
*http://initd.org/tracker/psycopg | *http://initd.org/tracker/psycopg |
2007年3月31日 (六) 11:31的版本
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
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()
分享您的观点