安装psycopg2模块:

postgresql client ---Navicat Premium

怎么验证是否已经安装过psycopy2?

编写上面代码,运行看是否抛出缺少psycopg2模块。

安装方法1:

1)使用psycopg2-2.4.2.win-amd64-py2.7-pg9.0.4-release.exe安装,下载地址:http://vdisk.weibo.com/s/Cd8pPaw56Ozys

直接运行exe,不出错误,运行上边代码验证代码无错误,基本算是安装完成了。

2)怎么卸载?

2.1)找到安装目录:C:\Python27,发现下边包含文件:Removepsycopg2.exe,运行,来删除;

2.2)如果运行失败的话,进入目录:C:\Python27\Lib\site-packages下,找到psycopg2文件夹和psycopg2-2.4.2-py2.7.egg-info文件,右键删除。

2.3)运行上边的代码,确认是否删除成功。

安装方法2:

使用.whl安装,下载地址:https://pypi.python.org/pypi/psycopg2/

下载文件:psycopg2-2.6.2-cp27-none-win_amd64.whl

我这里把psycopg2-2.6.2-cp27-none-win_amd64.whl拷贝到安装目录下Scripts文件夹中。

cmd中运行代码:pip install C:\Python27\Scripts\psycopg2-2.6.2-cp27-none-win_amd64.whl

运行上边的代码,确认是否删除成功。

通过psycopg2操作数据库:

使用账户postgres,创建测试数据库testdb。

参考yiibai.comAPI:

S.N. API & 描述

1psycopg2.connect(database="testdb", user="postgres", password="cohondob", host="127.0.0.1", port="5432")

这个API打开一个连接到PostgreSQL数据库。如果成功打开数据库时,它返回一个连接对象。

2connection.cursor()

该程序创建一个光标将用于整个数据库使用Python编程。

3cursor.execute(sql [, optional parameters])

此例程执行SQL语句。可被参数化的SQL语句(即占位符,而不是SQL文字)。 psycopg2的模块支持占位符用%s标志

例如:cursor.execute("insert into people values (%s, %s)", (who, age))

4curosr.executemany(sql, seq_of_parameters)

该程序执行SQL命令对所有参数序列或序列中的sql映射。

5curosr.callproc(procname[, parameters])

这个程序执行的存储数据库程序给定的名称。该程序预计为每一个参数,参数的顺序必须包含一个条目。

6cursor.rowcount

这个只读属性,它返回数据库中的行的总数已修改,插入或删除最后 execute*().

7connection.commit()

此方法提交当前事务。如果不调用这个方法,无论做了什么修改,自从上次调用commit()是不可见的,从其他的数据库连接。

8connection.rollback()

此方法会回滚任何更改数据库自上次调用commit()方法。

9connection.close()

此方法关闭数据库连接。请注意,这并不自动调用commit()。如果你只是关闭数据库连接而不调用commit()方法首先,那么所有更改将会丢失!

10cursor.fetchone()

这种方法提取的查询结果集的下一行,返回一个序列,或者无当没有更多的数据是可用的。

11cursor.fetchmany([size=cursor.arraysize])

这个例程中取出下一个组的查询结果的行数,返回一个列表。当没有找到记录,返回空列表。该方法试图获取尽可能多的行所显示的大小参数。

12cursor.fetchall()

这个例程获取所有查询结果(剩余)行,返回一个列表。空行时则返回空列表。

打开数据库连接:

1 importos2 importsys3 importpsycopg24

5 defconnectPostgreSQL():6 conn = psycopg2.connect(database="testdb", user="postgres", password="new.1234", host="127.0.0.1", port="5432")7 print 'connect successful!'

8

9 if __name__=='__main__':10 connectPostgreSQL()11

创建表操作:

1 importos2 importsys3 importpsycopg24

5 defconnectPostgreSQL():6 conn = psycopg2.connect(database="testdb", user="postgres", password="new.1234", host="127.0.0.1", port="5432")7 print 'connect successful!'

8 cursor=conn.cursor()9 cursor.execute('''create table public.member(10 id integer not null primary key,11 name varchar(32) not null,12 password varchar(32) not null,13 singal varchar(128)14 )''')15 conn.commit()16 conn.close()17 print 'table public.member is created!'

18

19 if __name__=='__main__':20 connectPostgreSQL()21

Insert 操作:

1 importos2 importsys3 importpsycopg24

5 defconnectPostgreSQL():6 conn = psycopg2.connect(database="testdb", user="postgres", password="new.1234", host="127.0.0.1", port="5432")7 print 'connect successful!'

8 cursor=conn.cursor()9 cursor.execute('''create table public.member(10 id integer not null primary key,11 name varchar(32) not null,12 password varchar(32) not null,13 singal varchar(128)14 )''')15 conn.commit()16 conn.close()17 print 'table public.member is created!'

18

19 definsertOperate():20 conn = psycopg2.connect(database="testdb", user="postgres", password="new.1234", host="127.0.0.1", port="5432")21 cursor=conn.cursor()22 cursor.execute("insert into public.member(id,name,password,singal)\23 values(1,'member0','password0','signal0')")24 cursor.execute("insert into public.member(id,name,password,singal)\25 values(2,'member1','password1','signal1')")26 cursor.execute("insert into public.member(id,name,password,singal)\27 values(3,'member2','password2','signal2')")28 cursor.execute("insert into public.member(id,name,password,singal)\29 values(4,'member3','password3','signal3')")30 conn.commit()31 conn.close()32

33 print 'insert records into public.memmber successfully'

34

35 if __name__=='__main__':36 #connectPostgreSQL()

37 insertOperate()38

Select 操作:

1 importos2 importsys3 importpsycopg24

5 defconnectPostgreSQL():6 conn = psycopg2.connect(database="testdb", user="postgres", password="new.1234", host="127.0.0.1", port="5432")7 print 'connect successful!'

8 cursor=conn.cursor()9 cursor.execute('''create table public.member(10 id integer not null primary key,11 name varchar(32) not null,12 password varchar(32) not null,13 singal varchar(128)14 )''')15 conn.commit()16 conn.close()17 print 'table public.member is created!'

18

19 definsertOperate():20 conn = psycopg2.connect(database="testdb", user="postgres", password="new.1234", host="127.0.0.1", port="5432")21 cursor=conn.cursor()22 cursor.execute("insert into public.member(id,name,password,singal)\23 values(1,'member0','password0','signal0')")24 cursor.execute("insert into public.member(id,name,password,singal)\25 values(2,'member1','password1','signal1')")26 cursor.execute("insert into public.member(id,name,password,singal)\27 values(3,'member2','password2','signal2')")28 cursor.execute("insert into public.member(id,name,password,singal)\29 values(4,'member3','password3','signal3')")30 conn.commit()31 conn.close()32

33 print 'insert records into public.memmber successfully'

34

35 defselectOperate():36 conn = psycopg2.connect(database="testdb", user="postgres", password="new.1234", host="127.0.0.1", port="5432")37 cursor=conn.cursor()38 cursor.execute("select id,name,password,singal from public.member where id>2")39 rows=cursor.fetchall()40 for row inrows:41 print 'id=',row[0], ',name=',row[1],',pwd=',row[2],',singal=',row[3],'\n'

42 conn.close()43

44 if __name__=='__main__':45 #connectPostgreSQL()

46 #insertOperate()

47 selectOperate()48

结果:

Python 2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:24:40) [MSC v.1500 64bit (AMD64)] on win32

Type"copyright", "credits" or "license()" formore information.>>>

========== RESTART: C:\Users\Administrator\Desktop\mutilpleTest.py ==========id= 3 ,name= member2 ,pwd= password2 ,singal=signal2

id= 4 ,name= member3 ,pwd= password3 ,singal=signal3>>>

update操作:

1 importos2 importsys3 importpsycopg24

5 defconnectPostgreSQL():6 conn = psycopg2.connect(database="testdb", user="postgres", password="new.1234", host="127.0.0.1", port="5432")7 print 'connect successful!'

8 cursor=conn.cursor()9 cursor.execute('''create table public.member(10 id integer not null primary key,11 name varchar(32) not null,12 password varchar(32) not null,13 singal varchar(128)14 )''')15 conn.commit()16 conn.close()17 print 'table public.member is created!'

18

19 definsertOperate():20 conn = psycopg2.connect(database="testdb", user="postgres", password="new.1234", host="127.0.0.1", port="5432")21 cursor=conn.cursor()22 cursor.execute("insert into public.member(id,name,password,singal)\23 values(1,'member0','password0','signal0')")24 cursor.execute("insert into public.member(id,name,password,singal)\25 values(2,'member1','password1','signal1')")26 cursor.execute("insert into public.member(id,name,password,singal)\27 values(3,'member2','password2','signal2')")28 cursor.execute("insert into public.member(id,name,password,singal)\29 values(4,'member3','password3','signal3')")30 conn.commit()31 conn.close()32

33 print 'insert records into public.memmber successfully'

34

35 defselectOperate():36 conn = psycopg2.connect(database="testdb", user="postgres", password="new.1234", host="127.0.0.1", port="5432")37 cursor=conn.cursor()38 cursor.execute("select id,name,password,singal from public.member where id>2")39 rows=cursor.fetchall()40 for row inrows:41 print 'id=',row[0], ',name=',row[1],',pwd=',row[2],',singal=',row[3],'\n'

42 conn.close()43

44 defupdateOperate():45 conn = psycopg2.connect(database="testdb", user="postgres", password="new.1234", host="127.0.0.1", port="5432")46 cursor=conn.cursor()47 cursor.execute("update public.member set name='update ...' where id=2")48 conn.commit()49 print "Total number of rows updated :", cursor.rowcount50

51 cursor.execute("select id,name,password,singal from public.member")52 rows=cursor.fetchall()53 for row inrows:54 print 'id=',row[0], ',name=',row[1],',pwd=',row[2],',singal=',row[3],'\n'

55 conn.close()56

57 if __name__=='__main__':58 #connectPostgreSQL()

59 #insertOperate()

60 #selectOperate()

61 updateOperate()

结果:

Python 2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:24:40) [MSC v.1500 64bit (AMD64)] on win32

Type"copyright", "credits" or "license()" formore information.>>>

========== RESTART: C:\Users\Administrator\Desktop\mutilpleTest.py ==========Total number of rows updated :1id= 1 ,name= member0 ,pwd= password0 ,singal=signal0

id= 3 ,name= member2 ,pwd= password2 ,singal=signal2

id= 4 ,name= member3 ,pwd= password3 ,singal=signal3

id= 2 ,name= update ... ,pwd= password1 ,singal=signal1>>>

Delete操作:

1 importos2 importsys3 importpsycopg24

5 defconnectPostgreSQL():6 conn = psycopg2.connect(database="testdb", user="postgres", password="new.1234", host="127.0.0.1", port="5432")7 print 'connect successful!'

8 cursor=conn.cursor()9 cursor.execute('''create table public.member(10 id integer not null primary key,11 name varchar(32) not null,12 password varchar(32) not null,13 singal varchar(128)14 )''')15 conn.commit()16 conn.close()17 print 'table public.member is created!'

18

19 definsertOperate():20 conn = psycopg2.connect(database="testdb", user="postgres", password="new.1234", host="127.0.0.1", port="5432")21 cursor=conn.cursor()22 cursor.execute("insert into public.member(id,name,password,singal)\23 values(1,'member0','password0','signal0')")24 cursor.execute("insert into public.member(id,name,password,singal)\25 values(2,'member1','password1','signal1')")26 cursor.execute("insert into public.member(id,name,password,singal)\27 values(3,'member2','password2','signal2')")28 cursor.execute("insert into public.member(id,name,password,singal)\29 values(4,'member3','password3','signal3')")30 conn.commit()31 conn.close()32

33 print 'insert records into public.memmber successfully'

34

35 defselectOperate():36 conn = psycopg2.connect(database="testdb", user="postgres", password="new.1234", host="127.0.0.1", port="5432")37 cursor=conn.cursor()38 cursor.execute("select id,name,password,singal from public.member where id>2")39 rows=cursor.fetchall()40 for row inrows:41 print 'id=',row[0], ',name=',row[1],',pwd=',row[2],',singal=',row[3],'\n'

42 conn.close()43

44 defupdateOperate():45 conn = psycopg2.connect(database="testdb", user="postgres", password="new.1234", host="127.0.0.1", port="5432")46 cursor=conn.cursor()47 cursor.execute("update public.member set name='update ...' where id=2")48 conn.commit()49 print "Total number of rows updated :", cursor.rowcount50

51 cursor.execute("select id,name,password,singal from public.member")52 rows=cursor.fetchall()53 for row inrows:54 print 'id=',row[0], ',name=',row[1],',pwd=',row[2],',singal=',row[3],'\n'

55 conn.close()56

57 defdeleteOperate():58 conn = psycopg2.connect(database="testdb", user="postgres", password="new.1234", host="127.0.0.1", port="5432")59 cursor=conn.cursor()60

61 cursor.execute("select id,name,password,singal from public.member")62 rows=cursor.fetchall()63 for row inrows:64 print 'id=',row[0], ',name=',row[1],',pwd=',row[2],',singal=',row[3],'\n'

65

66 print 'begin delete'

67 cursor.execute("delete from public.member where id=2")68 conn.commit()69 print 'end delete'

70 print "Total number of rows deleted :", cursor.rowcount71

72 cursor.execute("select id,name,password,singal from public.member")73 rows=cursor.fetchall()74 for row inrows:75 print 'id=',row[0], ',name=',row[1],',pwd=',row[2],',singal=',row[3],'\n'

76 conn.close()77

78 if __name__=='__main__':79 #connectPostgreSQL()

80 #insertOperate()

81 #selectOperate()

82 #updateOperate()

83 deleteOperate()

结果:

Python 2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:24:40) [MSC v.1500 64bit (AMD64)] on win32

Type"copyright", "credits" or "license()" formore information.>>>

========== RESTART: C:\Users\Administrator\Desktop\mutilpleTest.py ==========id= 1 ,name= member0 ,pwd= password0 ,singal=signal0

id= 3 ,name= member2 ,pwd= password2 ,singal=signal2

id= 4 ,name= member3 ,pwd= password3 ,singal=signal3

id= 2 ,name= update ... ,pwd= password1 ,singal=signal1

begin delete

end delete

Total number of rows deleted :1id= 1 ,name= member0 ,pwd= password0 ,singal=signal0

id= 3 ,name= member2 ,pwd= password2 ,singal=signal2

id= 4 ,name= member3 ,pwd= password3 ,singal=signal3>>>

参考文章:

http://www.cnblogs.com/qiongmiaoer/archive/2013/09/30/3346984.html

http://www.yiibai.com/html/postgresql/2013/080998.html

python psycopg2_Python:使用psycopg2模块操作PostgreSQL相关推荐

  1. python psycopg2使用_Python中用psycopg2模块操作PostgreSQL方法

    其实在Python中可以用来连接PostgreSQL的模块很多,这里比较推荐psycopg2.psycopg2安装起来非常的简单(pip install psycopg2),这里主要重点介绍下如何使用 ...

  2. python psycopg2使用_安装python依赖包psycopg2来调用postgresql的操作

    1.先安装psycopg2的依赖组件 本案例的操作系统为linux red hat 在安装python依赖包psycopg之前,你必须需要先安装postgresql数据库的相关组件: postgres ...

  3. python psycopg2_Python和psycopg2在大数据下的多处理性能

    我有一个相当标准的多处理脚本,可以处理一个数据库表中的200万条记录.在我把工作放入worker_队列之前,内存使用量就膨胀到12GB以上并崩溃.有更好的设计方法吗?在import math impo ...

  4. 【Python使用psycopg2三方库操作PostgreSQL的数据】

    psycopg2,是Python语言的PostgreSQL数据库接口,它的主要优势在于完全支持Python DB API 2.0,以及安全的多线程支持.它适用于随时创建.销毁大量游标的.和产生大量并发 ...

  5. python psycopg2_python库之psycopg2

    psycopg2 库是 python 用来操作 postgreSQL 数据库的第三方库.使用时需要先进行安装.pip install psycopg2. python 部分准备就绪,接下来我们先来看看 ...

  6. python xlrd读取excel所有数据_python读取excel进行遍历/xlrd模块操作

    我就废话不多说了,大家还是直接看代码吧~ #!/usr/bin/env python # -*- coding: utf-8 -*- import csv import xlrd import xlw ...

  7. python怎么用excel-Python使用xlwt模块操作Excel的方法详解

    本文实例讲述了Python使用xlwt模块操作Excel的方法.分享给大家供大家参考,具体如下: 部分摘自官网文档. 该模块安装很简单 $ pip install xlwt 先来个简单的例子: #!/ ...

  8. psycopg2 mysql_使用psycopg2操作PostgreSQL数据库之二

    使用psycopg2操作PostgreSQL数据库之二 佣工7001 1. 连接数据库: import psycopg2 import psycopg2.extras conn = psycopg2. ...

  9. 使用psycopg2操作PostgreSQL数据库之二

    使用psycopg2操作PostgreSQL数据库之二 佣工7001 1. 连接数据库: import psycopg2import psycopg2.extrasconn = psycopg2.co ...

最新文章

  1. 浏览是不是计算机网络功能,什么是因特网——计算机网络是怎么为你服务的?...
  2. mysql 同步日志文件_mysql 5.5 中对SLAVE relay-log相关日志文件同步的强化
  3. 数据结构:堆排序一(heap sort)
  4. 函数模板案例_利用函数模板封装一个排序的函数,可以对不同数据类型数组进行排序 排序规则从大到小,排序算法为选择排序 分别利用char数组和int数组进行测试
  5. 直方图均衡 视觉显著_视觉图像:对比度受限直方图均衡化CLAHE
  6. LUOGU P4016 负载平衡问题
  7. 中国网建java发送短信_短信验证登陆-中国网建提供的SMS短信平台
  8. RailsCasts中文版,#16 Virtual Attributes 虚拟属性
  9. visio2013复制到word有多余白边_Visio虚线复制到word中变为实线的解决办法
  10. 再论DataSet与DataFrame的区别
  11. php基础之常量(系统常量,自定义常量)
  12. 乘法口诀表 java_利用java 实现一个九九乘法口诀表
  13. 博文视点架构师成长书单,5本书助你少走弯路
  14. MQ7.0 在WAS中配置队列链接工厂并测试连接报2035错误
  15. 如何理解t检验、t分布、t值?
  16. Qt Quick简单教程
  17. 使用DOM4J解析XML文件的两种方法
  18. 在kubernetes集群用helm离线安装harbor
  19. 博士申请 | 澳大利亚麦考瑞大学王岩教授招收机器学习方向全奖博士生
  20. Centos7安装并使用gcc编译器

热门文章

  1. python并行计算(完结篇):并行方法总结
  2. mysql php教程视频教程下载地址_最全138节Mysql数据库+PHP零基础到精通,视频教程下载...
  3. 蛋蛋读UFS之六:UFS设备初始化和启动
  4. MySql计算两个日期的时间差
  5. (转)神经网络和深度学习简史(第一部分):从感知机到BP算法
  6. java中scheduler_Spring 中的Scheduler
  7. Pycharm添加镜像源
  8. 防止arp攻击怎么做?ARP攻击防范的解决办法
  9. 香港服务器有显示器吗?
  10. (8)多表查询【Oracle】