与python交互教程

原文地址:https://www.yiibai.com/html/postgresql/2013/080998.html

1. Python psycopg2 模块APIs

  • 连接到数据库

    psycopg2.connect(database='test',user='postgres', password='520520',host='127.0.0.1',port='5432')
  • 创建光标

    connection.cursor()

  • 执行SQL语句

    cursor.execute(sql[,optional parameters])

  • 提交当前事务

    connection.commit()

  • 回滚

    connection.rollback()

  • 关闭数据库

    connection.close()

  • 提取查询结果的下一行

    cursor.fetchone()

  • 取出下一组的查询结果的行数,返回一个列表

    cursor.fetchmany()

  • 获取所有查询结果(剩余),返回一个列表

    cursor.fetchall()

2. 创建表

import psycopg2

# 连接到数据库
conn = psycopg2.connect(database='test',user='postgres',
password='520520',host='127.0.0.1',port='5432') print('连接成功') # 创建光标
cur = conn.cursor() s = '''
create table pythonTest(
e_no int primary key not null,
e_name varchar(50) not null,
e_gender char(2) not null,
dept_no int not null,
e_job varchar(100) not null,
e_salary smallint,
hireDate date
)
'''
cur.execute(s)
conn.commit()
print('建表成功') conn.close()

3. 插入操作

import psycopg2

# 连接到数据库
conn = psycopg2.connect(database='test',user='postgres',
password='520520',host='127.0.0.1',port='5432') print('连接成功') # 创建光标
cur = conn.cursor() s = '''
insert into pythonTest
values(1001,'赵1','m',20,'会计',800,'2005-11-12'),
(1002,'钱2','f',30,'推销员',1600,'2003-05-12'),
(1003,'孙3','f',30,'推销员',1250,'2005-05-12'),
(1004,'李4','m',20,'经理',2975,'1998-05-18'),
(1005,'周5','m',30,'推销员',1250,'2001-06-12'),
(1006,'吴6','f',30,'经理',2850,'1997-02-15'),
(1007,'郑7','f',10,'经理',2450,'2002-09-12'),
(1008,'王8','m',20,'分析师',3000,'2003-05-12'),
(1009,'冯9','m',10,'董事长',5000,'1995-01-01'),
(1010,'陈10','f',30,'推销员',1500,'1997-10-12'),
(1011,'褚11','f',20,'会计',1100,'1999-10-05'),
(1012,'卫12','m',30,'会计',950,'2008-06-15');
'''
cur.execute(s)
conn.commit()
print('插入成功') conn.close()

4. 查询操作

import psycopg2

# 连接到数据库
conn = psycopg2.connect(database='test',user='postgres',
password='520520',host='127.0.0.1',port='5432') print('连接成功') # 创建光标
cur = conn.cursor() s = '''
select e_name,e_job from pythonTest
'''
cur.execute(s)
conn.commit() rows = cur.fetchall()
for i in rows:
print(i) conn.close()

5. 修改操作

import psycopg2

# 连接到数据库
conn = psycopg2.connect(database='test',user='postgres',
password='520520',host='127.0.0.1',port='5432') print('连接成功') # 创建光标
cur = conn.cursor() s = '''
update pythonTest set e_job='分析师' where e_name='卫12'
'''
cur.execute(s)
conn.commit()
print('修改成功') conn.close()

6. 删除操作

import psycopg2

# 连接到数据库
conn = psycopg2.connect(database='test',user='postgres',
password='520520',host='127.0.0.1',port='5432') print('连接成功') # 创建光标
cur = conn.cursor() s = '''
delete from pythonTest where e_no=1011
'''
cur.execute(s)
conn.commit()
print('删除成功') conn.close()

PostgreSQL自学笔记:与python交互的更多相关文章

  1. PostgreSQL自学笔记:1 初识 PostgreSQL

    博主教材:李小威.清华大学出版社.<PostgreSQL 9.6 从零开始学> 博主操作系统系统:Windows10 博主PostgreSQL版本:PostgreSQL 9.6 和 Pos ...

  2. PostgreSQL自学笔记:9 索引

    9 索引 9.1 索引简介 索引是对数据库表中一列或多列值进行排序的一种结构,使用 索引可提高数据库中特定数据的查询速度 9.1.1 索引的含义和特点 索引是一种单独的.存储在磁盘上的数据库结构,他们 ...

  3. PostgreSQL自学笔记:8 查询数据

    8 查询数据 8.1 基本查询语句 select语句的基本格式是: select {* | 字段1[,字段2,...]} [ from 表1,表2... [where 表达式] [group by & ...

  4. PostgreSQL自学笔记:6 PostgreSQL函数

    6 PostgreSQL函数 6.2 数学函数 abs(x) 绝对值 pi() 圆周率π select abs(-3),pi(); cookie: MySQL中的pi()默认值3.141593, Po ...

  5. PostgreSQL自学笔记:5 数据类型和运算符

    5 数据类型和运算符 5.1 PostgreSQL 数据类型介绍 5.1.1 整数类型 整型类型 字节 取值范围 smallint 2字节 -2^15 ~ 2^15 int integer 4字节 - ...

  6. PostgreSQL自学笔记:3 数据库的基本操作

    3 数据库的基本操作 3.1 创建数据库 3.1.1 使用对象浏览器创建数据库 [Server] -> PostgreSQL 9.6 -> 数据库,右击 -> 创建 通常: 数据库: ...

  7. PostgreSQL自学笔记:7 插入、更新与删除数据

    7 插入.更新与删除数据 7.1 插入数据 先创建表person: create table person( id int not null, name char(40) not null defau ...

  8. python自学笔记

    python自学笔记 python自学笔记 1.输出 2.输入 3.零碎 4.数据结构 4.1 list 类比于java中的数组 4.2 tuple 元祖 5.条件判断和循环 5.1 条件判断 5.2 ...

  9. python自学笔记(一)

    我没学过python,通过网上和一些图书资料,自学并且记下笔记. 很多细节留作以后自己做项目时再研究,这样能更高效一些. python基础自学笔记 一.基本输入和输出 pthon3.0用input提示 ...

随机推荐

  1. 如何取消Paypal自动付款功能

    在国外在线服务消费肯定会常遇到PayPal的支付方式,有些人可能PayPal有些余额可能会用这个工具来支付,但付款后,可能服务因为不满意而退掉,但第二年却自动续约了?但明明服务已退掉,这该怎么处理呢? ...

  2. Apache Beam实战指南 | 手把手教你玩转大数据存储HdfsIO

    https://mp.weixin.qq.com/s?__biz=MzU1NDA4NjU2MA==&mid=2247494843&idx=2&sn=0dd20caec76e25 ...

  3. elasticsearch篇之mapping

    2018年05月17日 18:01:37 lyzkks 阅读数:444更多 个人分类: Elastic stack   版权声明:文章内容来自于网络和博主自身学习体会,转载请注明出处,欢迎留言大家一起 ...

  4. Linux下查看Nginx安装目录、版本号信息及当前运行的配置文件

    Linux环境下,怎么确定Nginx的安装路径 输入命令行: ps -ef | grep nginx 摁回车,将出现如下图片: master process 后面的就是的 /data/software ...

  5. redis简单命令总结

    1.连接到redis服务器:redis-cli -h 127.0.0.1 -p 6379 -a 密码 select index 切换 redis 数据库 flushdb 删除当前数据库所有的 key ...

  6. Java多线程、线程池和线程安全整理

    多线程 1.1      多线程介绍 进程指正在运行的程序.确切的来说,当一个程序进入内存运行,即变成一个进程,进程是处于运行过程中的程序,并且具有一定独立功能. 1.2      Thread类 通 ...

  7. 金融量化分析【day110】:Pandas-DataFrame索引和切片

    一.实验文档准备 1.安装 tushare pip install tushare 2.启动ipython C:\Users\Administrator>ipython Python 3.7.0 ...

  8. [再寄小读者之数学篇](2014-06-20 求极限---Jordan 不等式的应用)

    证明: 当 $\lm<1$ 时, $\dps{\lim_{R\to+\infty} R^\lm\int_0^{\pi/2} e^{-R\sin\tt}\rd \tt=0}$. 证明: 由 $$\ ...

  9. DevExpress 控件汉化方法

    Ø  简介 本文介绍下 DevExpress 控件的汉化方法,对于英文不怎么好的同学来说,还是非常有必要的.DevExpress 汉化分为运行时汉化,和设计时汉化. 1.   运行时汉化 1)   首 ...

  10. A Boring Problem UVALive - 7676 (二项式定理+前缀和)

    题目链接: I - A Boring Problem UVALive - 7676 题目大意:就是求给定的式子. 学习的网址:https://blog.csdn.net/weixin_37517391 ...