python访问postgresql
2011-04-11 16:12
323 查看
2011-04-11import pg
#!/usr/bin/python
# -*- coding: utf-8 -*
import sys
import logging
LOG_FILENAME = './log.txt'
logging.basicConfig(filename=LOG_FILENAME,level=logging.DEBUG,filemode='a')
#-1:exception,0:normal
class ConnDB():
def __init__(self):
self.connobj = None
#获取Conn对象
def getConn(self):
'''
This function opens a connection to a specified database on a given PostgreSQL server
'''
code = 0
try:
self.connobj=pg.connect(dbname="mydb",host="localhost",port=5432,user="xx",/
passwd="xx")
except TypeError,e:
code = -1
msg = "ConnDB::getConn:bad argument type, or too many arguments"
logging.debug(msg)
except SyntaxError,e:
code = -1
msg = "ConnDB::getConn:duplicate argument definition"
logging.debug(msg)
except pg.InternalError,e:
code = -1
msg = "ConnDB::getConn:some error occurred during pg connection definition"
logging.debug(msg)
return (code,self.connobj)
if __name__ == "__main__":
connobj = ConnDB()
#获取连接
code,conn = connobj.getConn()
if code == -1:
sys.exit(code)
#!/usr/bin/python
# -*- coding: utf-8 -*
import sys
import logging
LOG_FILENAME = './log.txt'
logging.basicConfig(filename=LOG_FILENAME,level=logging.DEBUG,filemode='a')
#-1:exception,0:normal
class ConnDB():
def __init__(self):
self.connobj = None
#获取Conn对象
def getConn(self):
'''
This function opens a connection to a specified database on a given PostgreSQL server
'''
code = 0
try:
self.connobj=pg.connect(dbname="mydb",host="localhost",port=5432,user="xx",/
passwd="xx")
except TypeError,e:
code = -1
msg = "ConnDB::getConn:bad argument type, or too many arguments"
logging.debug(msg)
except SyntaxError,e:
code = -1
msg = "ConnDB::getConn:duplicate argument definition"
logging.debug(msg)
except pg.InternalError,e:
code = -1
msg = "ConnDB::getConn:some error occurred during pg connection definition"
logging.debug(msg)
return (code,self.connobj)
if __name__ == "__main__":
connobj = ConnDB()
#获取连接
code,conn = connobj.getConn()
if code == -1:
sys.exit(code)
相关文章推荐
- python访问PostgreSQL数据库之连接库Psycopg2
- Python直接访问Postgresql,实践通过
- python或者postgresql图形化界面连接postgresql拒绝访问
- python访问PostgreSQL数据库之连接库Psycopg2
- python客户端访问WebService
- 使用Python连接postgresql数据库
- PostgreSQL配置远程访问
- 初试python+postgresql+linux6.4
- Python http访问学习
- PostgreSQL 允许远程访问设置方法
- Python实现登录vcloud并访问其资源
- 使用Python访问Windows的注册表
- Python3中使用代理访问网页
- python访问csv文件
- Python之访问dict
- 【转】PostgreSQL IP地址访问配置
- Python3基础 记录列表中元素访问的次数 定制不可变序列,__len__,__getitem__,下标字典
- 用RAMDISK来提高PostgreSQL访问速度
- python代码:将远程postgresql数据库表结构备份到别的主机
- python使用点操作符访问字典(dict)数据的方法