简介

Psycopg 是Python语言的PostgreSQL数据库接口。 它的主要优势在于完全支持Python DB API 2.0,以及安全的多线程支持。它适用于随时创建、销毁大量游标的、和产生大量并发INSERT、UPDATE操作的多线程数据库应用。Psycopg包内含 ZPsycopgDA,一个Zope数据库接口。

示例1:新建表、插入、修改

#-*- coding: utf- -*-
import psycopg2 def main(user,pwd,ip,port,dbname):
connection = "dbname=%s user=%s password=%s host=%s port=%s" % (dbname, user,pwd, ip, port) db = psycopg2.connect(connection)
cur = db.cursor()
#创建表
sql_stat = "CREATE TABLE test_class_id(id INT PRIMARY kEY NOT NULL, test_class TEXT, test_id CHAR(10))";
cur.execute(sql_stat)
#插入表
sql_stat = "insert into test_class_id(id, test_class, test_id) values (1, 'a', '3')"
cur.execute(sql_stat) sql_stat = "insert into test_class_id(id, test_class, test_id) values (2, 'a', '3')"
cur.execute(sql_stat)
#更改字段值
sql_stat = "update test_class_id set test_class='b' where id=1"
cur.execute(sql_stat) db.commit() if __name__ == "__main__":
user = '****'
pwd = '****'
ip = '***'
port = ''
dbname = '****'
main(user, pwd, ip, port, dbname)
print "Done~~"

结果

:这里有多次提交execute  最后一次提交commit,如果没有最后的一次commit,你们前面的提交也是不执行的。

示例2:批量表操作

 -*- coding: utf-8 -*-
import psycopg2 class_ids = []
class_ids.append({"test_id": 1, "test_class":"A", "test_id":""})
class_ids.append({"test_id": 2, "test_class":"A", "test_id":""})
class_ids.append({"test_id": 3, "test_class":"B", "test_id":""}) def main(user,pwd,ip,port,dbname):
connection = "dbname=%s user=%s password=%s host=%s port=%s" % (dbname, user,pwd, ip, port)
db = psycopg2.connect(connection)
cur = db.cursor() #sql_del = "delete from cdb_chk_group"
#cur.execute(sql_del)
#批量操作
sql_stat = 'insert into test_class_id(id, test_class, test_id) values (%(test_id)s, %(test_class)s,%(test_id)s)'
cur.executemany(sql_stat, class_ids) db.commit() if __name__ == "__main__":
user = '****'
pwd = '****'
ip = '****'
port = '5432'
dbname = '****'
main(user, pwd, ip, port, dbname)
print "Done~~"

验证

用法补充

1. 查询数据,并输出结果

查询一条

>>> cur.execute("select * from test;")
>>> cur.fetchone()
(, , "abc'def")

查询所有条

>>> cur.execute("select * from test;")
>>> cur.fetchall()
[(1, 100, "abc'def"),(2, 100, "abc'def")]

psycopg2的更多相关文章

  1. 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: impor ...

  2. Python:使用psycopg2模块操作PostgreSQL

    安装psycopg2模块: 怎么验证是否已经安装过psycopy2? 编写上面代码,运行看是否抛出缺少psycopg2模块. 安装方法1: 1)使用psycopg2-2.4.2.win-amd64-p ...

  3. python安装psycopg2

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

  4. psycopg2.pool – Connections pooling / psycopg2.pool – 连接池 / postgresql 连接池

    创建新的PostgreSQL连接可以是一个昂贵的操作.这个模块提供了一些纯Python类直接在客户端应用程序实现简单的连接池.      class psycopg2.pool.AbstractCon ...

  5. psycopg2关于undefined symbol: lo_truncate64解决方法

    今天,在centos6.5下安装psycopg2,利用Python连接PostgreSQL数据库的时候,出现了一个undefined symbol: lo_truncate6的错误: django.c ...

  6. psycopg2接口的基本用法

    转载自:http://zhiwei.li/text/2012/02/05/psycopg2接口的基本用法/ 与其他实现了DB API 2.0协议的其他数据库用户基本一致. import psycopg ...

  7. python2 使用pip安装psycopg2出现错误:Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-install-mvzdNj/psycopg2/

    公司业务需求,开发语言python2,需要使用数据库:postgresql,需要安装模块psycopg2这个模块, 使用pip install psycopg2 报错: Command "p ...

  8. pip install psycopg2出现python setup.py egg_info failed with error code 1 in /tmp/pip-build-YtLeN3/psycopg2错误处理

    折腾了一上午flask部署,到最后访问域名还是出现Application Error错误提示.估计是程序还有问题,想着直接clone书中作者的代码先试试能不能部署成功.结果在执行pip install ...

  9. psycopg2+postgis+pgAdmin4

    基于docker的postgres 部署见这篇 https://www.cnblogs.com/xuanmanstein/p/7742647.html 连接数据库 import psycopg2cla ...

随机推荐

  1. [kuangbin带你飞]专题六 最小生成树

    学习最小生成树已经有一段时间了 做一些比较简单的题还算得心应手..花了三天的时间做完了kuangbin的专题 写一个题解出来记录一下(虽然几乎都是模板题) 做完的感想:有很多地方都要注意 n == 1 ...

  2. PHP 文件系统管理函数与 preg_replace() 函数过滤代码

    案例:在带行号的代码至文件 crop.js 中.用两种方法去掉代码前面的行号,带行号的代码片段: 1.$(function(){ 2. //初始化图片区域 3. var myimg = new Ima ...

  3. linux 防火墙设置

    防火墙的基本操作命令: 查询防火墙状态:[root@localhost ~]# service iptables status<回车> 停止防火墙:[root@localhost ~]# ...

  4. 页面静态化3 --- 伪静态技术之Apache的rewrite机制

      Apache的rewrite机制: 意思就是,你发送的地址,比如:http://localhost/news-id67.html会被Apache改写成http://localhost/news.p ...

  5. word size

    Computer Systems A Programmer's Perspective Second Edition Running throughout the system is a collec ...

  6. Behavior-Based Intelligence

    Computer Science An Overview _J. Glenn Brookshear _11th Edition Early work in artificial intelligenc ...

  7. nginx proxy_pass

    在nginx中配置proxy_pass时,如果是按照^~匹配路径时,要注意proxy_pass后的url最后的/,当加上了/,相当于是绝对根路径,则nginx不会把location中匹配的路径部分代理 ...

  8. Oracle EBS 术语解释

    SRS:是否在标准窗口进行提交请求 fnd_flex_value_sets.validation_type含义如下: D:从属I:独立N:无P:对U:特定F:表X:可转换独立Y:可转换从属

  9. Android 的上下文菜单: Context Menu,registerForContextMenu(getListView())

    概述: Android 的上下文菜单类似于 PC 上的右键菜单.当为一个视图注册了上下文菜单之后,长按(2 秒左右)这个视图对象就会弹出一个浮动菜单,即上下文菜单.任何视图都可以注册上下文菜单,不过, ...

  10. 关于HBase的概述

    1.hbase的特点 ->数据存储量可以达到亿级别数据维持在秒级 ->按列存储的数据库 ->能够存储上百万列 ->hbase的底层存储依赖于HDFS ->如何扩展hbas ...