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 ...
随机推荐
- IIS 503日志文件在哪
概述 503:“服务不可用”错误是一个非自定义的错误,该错误表示服务器当前无法处理该请求. 可能原因:1.管理员可能关闭应用程序池以执行维护.2.当请求到达时应用程序池队列已满.3.应用程序池标识没 ...
- display:inline-block兼容ie6/7的写法
2.display:inline-block作用? 使用display:inline-block属性,可以使行内元素或块元素能够变成行内块元素,简单直白点讲就是不加float属性就可以定义自身的宽.高 ...
- 自制奇葩vb面试题,看你能对几道
这些题都比较奇葩,所以做出选择之前请仔细考虑. 答题过程中不要离开当前页面,不要去试代码,也不要查参考或问别人. 转载请说明作者是 Nukepayload2 Vb版本:14 默认的.net frame ...
- 关于C#读取MySql数据时,返回DataTable中某字段数据是System.Array[]形式
我在使用C#(VS2008)读取MySql数据库(5.1版本)时,返回的DataTable数据中arrivalDate字段数据显示为System.Array[]形式(程序中没有对返回的数据进行任何加工 ...
- [PE结构分析] 11.资源表结构
资源表是一个树形结构,可以设置成2的31次方的层数,Windows 使用了3级: 类型->名称->语言 其中涉及到四个结构: Data Description Resource Direc ...
- nginx配合modsecurity实现WAF功能
一.准备工作 系统:centos 7.2 64位.nginx1.10.2, modsecurity2.9.1 owasp3.0 1.nginx:http://nginx.org/download/ng ...
- 面向企业客户的制造业CRM系统的不成熟思考
CRM就是客户关系管理(Customer Relationship Management),一直一知半解,最近有涉及这方面的需求,所以稍作研究,并思考一些相关问题. CRM是什么? CRM具体如何定义 ...
- android:ellipsize实现跑马灯效果总结(转)
最近无意间看到了涉及到跑马灯效果的代码,于是在网上查阅了很多资料,在这里对自己看的一些文章进行一下总结,顺便加上自己的一些体会. 让我们一步步逐渐向下. 首先我们要实现走马灯这样一个效果,通常来说 ...
- ArrayList等常见集合的排序问题
对于ArrayList等常用的集合具体业务类,基本上都实现了Comparable接口,即可以用来比较装载的对象实体. 主要用Collections.sort方法对集合类中的对象进行排序 Collect ...
- [Xamarin.iOS] Visual Studio中Xamarin.iOS项目,无法加入PCL项目参考、NuGet组件参考
[Xamarin.iOS] Visual Studio中Xamarin.iOS项目,无法加入PCL项目参考.NuGet组件参考 解决方案 目前Visual Studio中最新版本的Xamarin.iO ...