安装:http://www.pygresql.org/contents/install.html

PyGreSQL入门

——简单整理翻译自官方文档:http://www.pygresql.org/contents/tutorial.html

  • 创建数据库连接

只需import DB类并创建一个实例,填入相应的连接信息,例:

 
1
2
>>> from pg import DB
>>> db = DB(dbname='testdb', host='pgserver', port=5432, user='scott', passwd='tiger')

如果省略参数,则会使用默认值:

dbname,user默认为当前系统用户,host为localhost,port为5432。

  • 执行SQL语句

DB.query()

 
1
>>> db.query("create table fruits(id serial primary key, name varchar)")
  • 获取所有表名

DB.get_tables(),类似psql中\d:

 
1
2
>>> db.get_tables()
['public.fruits']
  • 获取表属性

DB.get_attnames(),类似psql中\d table:

 
1
2
>>> db.get_attnames('fruits')
{'id': 'int', 'name': 'text'}
  • 检查权限

DB.has_table_privilege()

 
1
2
>>> db.has_table_privilege('fruits', 'insert')
True
  • 插入数据

DB.insert()   –注:GP不支持

 
1
2
>>> db.insert('fruits', name='apple')
{'name': 'apple', 'id': 1}

该方法将完整的行作为字典返回,包括自增列。可以将字典赋值给变量:

 
1
>>> banana = db.insert('fruits', name='banana')
  • 批量插入数据

Connection.inserttable()

在插入大量数据时,批量插入性能比单条插入快很多

 
1
2
3
>>> more_fruits = 'cherimaya durian eggfruit fig grapefruit'.split()
>>> data = list(enumerate(more_fruits, start=3))
>>> db.inserttable('fruits', data)
  • 查询数据

DB.query()

 
1
2
3
4
5
6
7
8
9
10
11
>>> print(db.query('select * from fruits'))
id|   name
--+----------
1|apple
2|banana
3|cherimaya
4|durian
5|eggfruit
6|fig
7|grapefruit
(7 rows)

将查询结果放入元组:

 
1
2
3
>>> q = db.query('select * from fruits')
>>> q.getresult()
... [(1, 'apple'), ..., (7, 'grapefruit')]

或字典:

 
1
2
>>> q.dictresult()
[{'id': 1, 'name': 'apple'}, ..., {'id': 7, 'name': 'grapefruit'}]

或named tuple:

 
1
2
3
>>> rows = q.namedresult()
>>> rows[3].name
'durian'

使用DB.get_as_dict()可以轻松的将整张表数据加载到Python 字典中:

 
1
2
3
4
5
6
7
8
>>> db.get_as_dict('fruits', scalar=True)
OrderedDict([(1, 'apple'),
(2, 'banana'),
(3, 'cherimaya'),
(4, 'durian'),
(5, 'eggfruit'),
(6, 'fig'),
(7, 'grapefruit')])
  • 修改数据

DB.update()

 
1
2
3
4
5
6
7
8
9
>>> db.update('fruits', banana, name=banana['name'].capitalize())
{'id': 2, 'name': 'Banana'}
>>> print(db.query('select * from fruits where id between 1 and 3'))
id|  name
--+---------
1|apple
2|Banana
3|cherimaya
(3 rows)

也可使用DB.query()

 
1
2
>>> db.query('update fruits set name=initcap(name)')
'7'

返回值:‘7’表示更新的行数。

  • 删除数据

DB.delete()

 
1
2
>>> db.delete('fruits', banana)
1

1表示删除的行数,再次执行就会显示0行被删除:

 
1
2
>>> db.delete('fruits', banana)
0
  • 删除表
 
1
>>> db.query("drop table fruits")
  • 关闭连接
 
1
>>> db.close()

更高级的特性和详细信息,参阅:http://www.pygresql.org/contents/pg/index.html

接口:

The Classic PyGreSQL Interface

Contents

pgdb — The DB-API Compliant Interface

Contents

A PostgreSQL Primer

The examples in this chapter of the documentation have been taken from the PostgreSQL manual. They demonstrate some PostgreSQL features using the classic PyGreSQL interface. They can serve as an introduction to PostgreSQL, but not so much as examples for the use of PyGreSQL.

最新文章

  1. ListView 使用 LiveBindings 显示超过 200 条记录
  2. PHP Apache 配置伪静态
  3. Python之函数之路
  4. 【转】Spring jar包详解
  5. Hibernate逍遥游记-第13章 映射实体关联关系-003单向多对多
  6. 如何选择一个 Linux Tracer (2015)
  7. jquery easyui form load
  8. dba_dependencies查询结果视图
  9. mac上搭建python+selenium2的环境
  10. Java并发之需要了解但不能太依赖的东东
  11. rsync命令解释
  12. Linux-Centon7安装以及配置
  13. Python:a,*args,**kwargs的理解
  14. DALSA相机开发--修改参数
  15. nmon监控数据分析
  16. 如何在js中使用递归
  17. jquery表格展示
  18. Extjs Gridpanel 动态加载
  19. angular学习笔记(七)-迭代1
  20. Pygame:编写一个小游戏 标签: pythonpygame游戏 2017-06-20 15:06 103人阅读 评论(0)

热门文章

  1. Amazon电商数据分析——数据获取
  2. linux系统下创建lvm挂载到指定目录
  3. Hive的安装和使用
  4. 黑苹果 之 神舟战神Z7M-SL7D2
  5. MySQL数据库之视图
  6. 多线程学习笔记三之ReentrantLock与AQS实现分析
  7. Servlet接口、GenericServlet类、HttpServlet类
  8. bzoj 3285 离散对数解指数方程
  9. 【原】Maven解决jar冲突调试步骤:第三方组件引用不符合要求的javassit导致的相关异常
  10. j.u.c系列(07)---之读写锁:ReentrantReadWriteLock