For we need to connect the postgres db in project very frequently, so write the follows class:

 import psycopg2
#finish the work with task schedule
class dbwork:
def __init__(self,work_content,dbname='taskschedule',user='rl_dev',password='', host='10.0.39.46',port=5432):
self.dbname=dbname
self.user=user
self.password=password
self.host=host
self.port=port
self.work_content=work_content
def dowork(self):
conn=psycopg2.connect(database=self.dbname , user=self.user, password=self.password,host=self.host,port=self.port)
# Open a cursor to perform database operations
cur=conn.cursor()
# Execute a command: this creates a new table
sqlstr=self.work_content
#print 'sqlstr:'+sqlstr
if sqlstr.__contains__("update") or sqlstr.__contains__("UPDATE") or sqlstr.__contains__("delete") or sqlstr.__contains__("DELETE") or sqlstr.__contains__("insert") or sqlstr.__contains__("INSERT"):
cur.execute(sqlstr)
#if the sql action is a transaction, need to do commit
conn.commit()
cur.close()
conn.close()
else:
#if the sql action is not a transaction , return the result derectly
cur.execute(self.work_content)
result = cur.fetchall()
cur.close()
conn.close() return result #cur.execute("CREATE TABLE test (id serial PRIMARY KEY, num integer, data varchar);")

Class to connect postgres with python in psycopg2的更多相关文章

  1. Python PostgreSQL Psycopg2

    [转] http://daigong.iteye.com/blog/901160 Python如果要操作Postgresql,需要一个API,这就需要Psycopg2 1. 链接PostgreSQL并 ...

  2. python安装psycopg2

    vim ~/.bash_profile export PATH=/Applications/Postgres.app/Contents/Versions/9.4/bin/:$PATH pip inst ...

  3. ubuntu 14.04 安装python包psycopg2

    http://stackoverflow.com/questions/28253681/you-need-to-install-postgresql-server-dev-x-y-for-buildi ...

  4. Python使用psycopg2模块操作PostgreSQL

    https://blog.csdn.net/pcent/article/details/78643611

  5. 如何使用Python操纵Postgres数据库

    pip install psycopg2 psycopg2-binary #!/usr/bin/python import psycopg2conn = psycopg2.connect(databa ...

  6. PostgreSQL连接python,postgresql在python 连接,创建表,创建表内容,插入操作,选择操作,更新操作,删除操作。

    安装 PostgreSQL可以用Python psycopg2模块集成. sycopg2是Python编程语言的PostgreSQL数据库的适配器. 其程序代码少,速度快,稳定.不需要单独安装这个模块 ...

  7. PostgreSQL连接Python

    安装 PostgreSQL可以用Python psycopg2模块集成. sycopg2是Python编程语言的PostgreSQL数据库的适配器. 其程序代码少,速度快,稳定.不需要单独安装这个模块 ...

  8. python 操作PostgreSQL

    pip install psycopg Python psycopg2 模块APIs 以下是psycopg2的重要的的模块例程可以满足Python程序与PostgreSQL数据库的工作. S.N. A ...

  9. Python 操作 PostgreSQL 数据库

    我使用的是 Python 3.7.0 PostgreSQL可以使用psycopg2模块与Python集成. sycopg2是用于Python编程语言的PostgreSQL数据库适配器. psycopg ...

随机推荐

  1. Winform开发框架重构总结

    最近一直致力于Winform开发框架的重构工作,因为发现要维护传统Winform开发框架.WCF开发框架.混合式开发框架,以及相关的模块,包括权限管理.字典管理模块.附件管理.人员管理等一些辅助模块, ...

  2. C#全角转换成半角并检查

    新建一个项目quanbanjiao,在此项目下新建一个类Class1 using System; using System.Web; using System.Text; namespace quan ...

  3. [CLR via C#]8. 方法

    一.实例构造器和类(引用类型) 类实例构造器是允许将类型的实例初始化为良好状态的一种特殊的方法. 类实例构造器方法在"方法定义元数据表"中始终叫.ctor(代表constructo ...

  4. 返璞归真 asp.net mvc (13) - asp.net mvc 5.0 新特性

    [索引页][源码下载] 返璞归真 asp.net mvc (13) - asp.net mvc 5.0 新特性 作者:webabcd 介绍asp.net mvc 之 asp.net mvc 5.0 新 ...

  5. HDU 5667 Sequence 矩阵快速幂+费马小定理

    题目不难懂.式子是一个递推式,并且不难发现f[n]都是a的整数次幂.(f[1]=a0;f[2]=ab;f[3]=ab*f[2]c*f[1]...) 我们先只看指数部分,设h[n]. 则 h[1]=0; ...

  6. mfc110.dll丢失,解决方法

    mfc110.dll下载_附文件使用方法 mfc110.dll是存放在windows系统中的一个重要dll文件,缺少它可能会造成部分软件或游戏无法正常运行.当系统提示“没有找到mfc110.dll”或 ...

  7. [小北De编程手记] : Lesson 06 - Selenium For C# 之 流程控制

    无论你是用哪一种自动化测试的驱动框架,当我们构建一个复杂应用程序的自动化测试的时候.都希望构建一个测试流程稳定,维护成本较低的自动化测试.但是,现实往往没有理想丰满.而这一篇,我会为大家讲解我们在使用 ...

  8. bootstrap 弹出框点击其他区域时弹出框不消失选项设置

    默认情况下,bootstrap 弹出框点击其他区域时,弹出框会自动关闭,在很多时候,我们可能会希望达到和原生弹出框一样的效果,避免不小心点击其他区域时弹框自动隐藏,尤其是对于一些复杂的表单,重复填写可 ...

  9. weblogic 12c下jxls导出excel报错Could not initialize class org.apache.poi.xssf.usermodel.XSSFVMLDrawing

    周一,开发反馈weblogic 12c下jxls导出excel报错,公司环境和UAT环境均报错,看日志如下: 2016-06-08 09:16:55,825 ERROR org.jxls.util.T ...

  10. vh属性-- 一个永远垂直居中的弹出框

    下面的demo,无论浏览器大小如何改变,滚动条是否滚动,弹出框永远水平垂直居中 <html> <head> <title></title> </h ...