简介

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. Servlet编程-步步为营

    [环境]eclipse j2ee;Tomcat 7.0; [模型1] package com.zhiqi; import ...; public class TestServlet extends H ...

  2. FastDFS常见命令

    1: 启动FastDFS tracker: ./fdfs_trackered  .../tracker.conf storage: ./fdfs_storaged  .../storage.conf ...

  3. ThinkPHP 3.2.2 在 volist 多重循环嵌套中使用 if 判断标签

    今天在 ThinkPHP 3.2.2 的试图模板中使用多重循环,用来把相应类别下对应的文章都依次循环出来,但是无论如何只能循环出类别,类别下的文章无法循环出,( 错误 ) 代码如下: <voli ...

  4. oracle 多表查询

    1.注意点 在查询过程中,不确定数据库表中的数据量,先查询数据量,数据量较大,则不能直接查询(select * from emp),如果数据量较大,直接查询容易造成死机或者数据读取较慢,如果较小可以查 ...

  5. Stationary point

    https://en.wikipedia.org/wiki/Stationary_point https://zh.wikipedia.org/wiki/驻点

  6. Delphi的并行计算

    有如下循环体: hits:=; do begin {perform some calculations dependent on random number generation to determi ...

  7. TabHost详解

    [转]http://blog.csdn.net/harvic880925/article/details/17120325 前言:今天仔细研究了下TabHost,主要是为了实现微信底部导航栏的功能,最 ...

  8. uiwebview的基本使用

    http://blog.csdn.net/daiyelang/article/details/40989389

  9. Photoshop:通过图片理解通道原理

    电脑上的图像通过色光三原色RGB表现,不同颜色存储在不同的通道,所以RGB模式下有3条通道,CMYK模式有4条通道等等 打开一张照片,查看通道 可以看出,显示越白的地方,对应的原色越深. 默认phot ...

  10. php动态读取数据清除最右边距

    需求效果一行3栏: 场景模拟:同事给了我这么一段静态代码如下: <!DOCTYPE html> <html lang="en"> <head> ...