Class to connect postgres with python in psycopg2
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的更多相关文章
- Python PostgreSQL Psycopg2
[转] http://daigong.iteye.com/blog/901160 Python如果要操作Postgresql,需要一个API,这就需要Psycopg2 1. 链接PostgreSQL并 ...
- python安装psycopg2
vim ~/.bash_profile export PATH=/Applications/Postgres.app/Contents/Versions/9.4/bin/:$PATH pip inst ...
- ubuntu 14.04 安装python包psycopg2
http://stackoverflow.com/questions/28253681/you-need-to-install-postgresql-server-dev-x-y-for-buildi ...
- Python使用psycopg2模块操作PostgreSQL
https://blog.csdn.net/pcent/article/details/78643611
- 如何使用Python操纵Postgres数据库
pip install psycopg2 psycopg2-binary #!/usr/bin/python import psycopg2conn = psycopg2.connect(databa ...
- PostgreSQL连接python,postgresql在python 连接,创建表,创建表内容,插入操作,选择操作,更新操作,删除操作。
安装 PostgreSQL可以用Python psycopg2模块集成. sycopg2是Python编程语言的PostgreSQL数据库的适配器. 其程序代码少,速度快,稳定.不需要单独安装这个模块 ...
- PostgreSQL连接Python
安装 PostgreSQL可以用Python psycopg2模块集成. sycopg2是Python编程语言的PostgreSQL数据库的适配器. 其程序代码少,速度快,稳定.不需要单独安装这个模块 ...
- python 操作PostgreSQL
pip install psycopg Python psycopg2 模块APIs 以下是psycopg2的重要的的模块例程可以满足Python程序与PostgreSQL数据库的工作. S.N. A ...
- Python 操作 PostgreSQL 数据库
我使用的是 Python 3.7.0 PostgreSQL可以使用psycopg2模块与Python集成. sycopg2是用于Python编程语言的PostgreSQL数据库适配器. psycopg ...
随机推荐
- ASP.Net中无刷新执行Session身份验证
在写一个客户的B/S结构应用程序时,突然发现一个技巧,不知道是否是MS的一个BUG,给相关的有研究的朋友原先考虑写一个检查Session的类,Session失效后,必须转向登陆页面,可每一个调用该类的 ...
- [moka同学笔记]yii2.0的下拉菜单与bootstrap下拉菜单
1.yii2下拉菜单 <li class="dropdown"><a href="#" class="dropdown-toggle ...
- saltstack学习笔记1 --安装
salt官网:http://docs.saltstack.cn/zh_CN/latest/ 安装教程: - http://docs.saltstack.cn/zh_CN/latest/topics/i ...
- 一个Chrome拓展——HttpPost
周末花了点时间做了一个chrome拓展,叫HttpPost,顾名思义是用来测试http的post请求. 先直接看效果 插件与拓展 在说这个做的过程前,先说明什么是Chrome插件.Chrome拓展 1 ...
- python基础之基本算法和装饰器
1.冒泡排序 关于冒泡排序实现大小比较,大索引会向后移动,这次循环将最大数值直接移动至最后. li = [,,,,] ): ]: temp = li[i] li[i] = li[i + ] li[i ...
- OAUTH 协议介绍
OAUTH 产生背景 随着互联网的深入发展,一些互联网巨头积累了海量的用户和数据.对于平台级软件厂商来说,用户的需求多种多样,变化万千 以一己之力予以充分满足,难免疲于本命.因此将数据以接口的形式开放 ...
- SharePoint 2013 设置自定义布局页
在SharePoint中,我们经常需要自定义登陆页面.错误页面.拒绝访问等:不知道大家如何操作,以前自己经常在原来页面改或者跳转,其实SharePoint为我们提供了PowerShell命令,来修改这 ...
- SharePoint 2010 文档管理之点击次数
前言:很多场景下,我们都需要对一篇文章或者文档的点击次数进行统计,然而SharePoint本身并没有给我们设计这样一个字段,所以我们需要通过简单的字段开发来实现这样一个功能. 一.创建项目: 1. 创 ...
- Android 之 Intent(意图)
Intent是 Android中重要的桥梁之一,它分为显式意图和隐式意图.接下来分别针对这两种意图进行讲解. 显式意图:通过指定一组数据或动作,激活应用内部的 activity:(相比隐式意图,此做法 ...
- C迷途指针
在计算机编程领域中,迷途指针,或称悬空指针.野指针,指的是不指向任何合法的对象的指针. 当所指向的对象被释放或者收回,但是对该指针没有作任何的修改,以至于该指针仍旧指向已经回收的内存地址,此情况下该指 ...