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 ...
随机推荐
- HTML5 Wijmo:控制 Wijmo Grid 插件的编辑模式
Wijmo jQuery 插件经常应用于在财务类网站中创建平滑和良好用户体验的交互表格.WijGrid 插件用于显示.排序.分组和编辑数据.今天我们来分享下如何控件WijGrid插件的编辑模式. 在本 ...
- CRC16校验码生成
/// <summary> /// 计算CRC-16 /// </summary> /// <param name="data"></pa ...
- JPA(7) spring-data-jpa
对于不是使用spring管理的项目,我们就自己创建对象使用:大概的思路就是①创建dao接口②实现该接口,并且编写逻辑: Dao: public interface StudentDao { publi ...
- Python 学习之进制与编码
进制 日常生活中,我们最熟悉的数据就是十进制计数.它的数值部分由十个不同的数字符号0.1.2.3.4.5.6.7.8.9来表示,我们把这些数字符号叫做数码,表示十种不同的状态.数码处于不同的位置(或数 ...
- Hibernate框架之关联映射入门
关联映射就是将关联关系映射到数据库里,在对象模型中就是一个或多个引用. 一:配置单向多对一关联 在Emp类中定义一个Dept属性,而在Dept类中无须定义用于存放Emp对象的集合属性 01.Dept. ...
- bootstrap 弹出框点击其他区域时弹出框不消失选项设置
默认情况下,bootstrap 弹出框点击其他区域时,弹出框会自动关闭,在很多时候,我们可能会希望达到和原生弹出框一样的效果,避免不小心点击其他区域时弹框自动隐藏,尤其是对于一些复杂的表单,重复填写可 ...
- PlayFramework 1 自定义标签 -- FastTags http://unmi.cc/category/javajee/playframework/
最早是用 HTML 来自定义标签,现在觉得 HTML 写有关逻辑的代码就有点不伦不类了,HTML 里着重是显示代码.前有一篇 PlayFramework 1 模板应用 -- Java 对象扩展 学习 ...
- js用8421码实现10进制转2进制
今天早上突然心血来潮决定用 ''和js来撸一个进制转换.(纯属心血来潮,有兴趣的可以看看.) 我们知道,通过8421码.可以快速的得到一个10进制的2进制.如下图: 如上图所示:我们将10进制的 '1 ...
- 解决maven项目update project会更改jdk版本问题
一.问题描述 在Eclipse中新建了一个Maven工程, 然后更改JDK版本为1.6, 结果每次使用Maven > Update project的时候JDK版本都恢复成 ...
- .NET破解之轻量万能自定义信息管理系统
一般敢说万能的莫非真有两把刷子.今天来破解试试,看效果好用不. 下载:http://down.chinaz.com/soft/36780.htm 补丁: http://www.t00y.com/fil ...