PostgreSQL自学笔记:与python交互
与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交互的更多相关文章
- PostgreSQL自学笔记:1 初识 PostgreSQL
博主教材:李小威.清华大学出版社.<PostgreSQL 9.6 从零开始学> 博主操作系统系统:Windows10 博主PostgreSQL版本:PostgreSQL 9.6 和 Pos ...
- PostgreSQL自学笔记:9 索引
9 索引 9.1 索引简介 索引是对数据库表中一列或多列值进行排序的一种结构,使用 索引可提高数据库中特定数据的查询速度 9.1.1 索引的含义和特点 索引是一种单独的.存储在磁盘上的数据库结构,他们 ...
- PostgreSQL自学笔记:8 查询数据
8 查询数据 8.1 基本查询语句 select语句的基本格式是: select {* | 字段1[,字段2,...]} [ from 表1,表2... [where 表达式] [group by & ...
- PostgreSQL自学笔记:6 PostgreSQL函数
6 PostgreSQL函数 6.2 数学函数 abs(x) 绝对值 pi() 圆周率π select abs(-3),pi(); cookie: MySQL中的pi()默认值3.141593, Po ...
- PostgreSQL自学笔记:5 数据类型和运算符
5 数据类型和运算符 5.1 PostgreSQL 数据类型介绍 5.1.1 整数类型 整型类型 字节 取值范围 smallint 2字节 -2^15 ~ 2^15 int integer 4字节 - ...
- PostgreSQL自学笔记:3 数据库的基本操作
3 数据库的基本操作 3.1 创建数据库 3.1.1 使用对象浏览器创建数据库 [Server] -> PostgreSQL 9.6 -> 数据库,右击 -> 创建 通常: 数据库: ...
- PostgreSQL自学笔记:7 插入、更新与删除数据
7 插入.更新与删除数据 7.1 插入数据 先创建表person: create table person( id int not null, name char(40) not null defau ...
- python自学笔记
python自学笔记 python自学笔记 1.输出 2.输入 3.零碎 4.数据结构 4.1 list 类比于java中的数组 4.2 tuple 元祖 5.条件判断和循环 5.1 条件判断 5.2 ...
- python自学笔记(一)
我没学过python,通过网上和一些图书资料,自学并且记下笔记. 很多细节留作以后自己做项目时再研究,这样能更高效一些. python基础自学笔记 一.基本输入和输出 pthon3.0用input提示 ...
随机推荐
- Day038--Python--Gevent , IO多路复用
1. 协程: gevent (遇到IO自动切换) import gevent import time from gevent import monkey; monkey.patch_all() # ...
- CopyOnWriteArrayList真的完全线程安全吗
我之前书上看到的说法是:Vector是相对线程安全,CopyOnWriteArrayList是绝对线程安全 这种说法其实有些问题,CopyOnWriteArrayList在某些场景下还是会报错的 Co ...
- [Android] Android 类似今日头条顶部的TabLayout 滑动标签栏 效果
APP市场中大多数新闻App都有导航菜单,导航菜单是一组标签的集合,在新闻客户端中,每个标签标示一个新闻类别,对应下面ViewPager控件的一个分页面,今日头条, 网易新闻等. 本文主要讲的是用:T ...
- Elasticsearch High Level Rest Client 发起请求的过程分析
本文讨论的是JAVA High Level Rest Client向ElasticSearch6.3.2发送请求(index操作.update.delete--)的一个详细过程的理解,主要涉及到Res ...
- [再寄小读者之数学篇](2015-06-24 Series)
(AMM. Problems and Solutions. 2015. 03) Let $\sed{a_n}$ be a monotone decreasing sequence of real nu ...
- [物理学与PDEs]第5章第2节 变形的描述, 应变张量 2.2 Cauchy - Green 应变张量
1. 引理 (极分解): 设 $|{\bf F}|\neq 0$, 则存在正交阵 ${\bf R}$ 及对称正定阵 ${\bf U},{\bf V}$ 使得 $$\bex {\bf F}={\bf ...
- [物理学与PDEs]第4章第1节 引言
1. 本章讨论可燃流体在流动过程中同时伴随着燃烧现象的情况. 2. 燃烧有两种, 一种是爆燃 (deflagration): 火焰低速向前传播, 此时流体微元通常是未燃气体.已燃气体的混合物; 一 ...
- python中的__len__,__getitem__ __setitem__ __delitem__ __contains__
可变集合需要实现: __len__ __getitem__ __setitem__ __delitem__不可变集合需要实现: __len__ __getitem__ __len__:返回 ...
- CDH5.16.1启动报错:Invalid value set for db.setupType, the valid values are EMBEDDED or EXTERNAL
1 自己的配置文件已经添加了配置,但是还是报错,由于是自建数据库 com.cloudera.cmf.db.setupType=EXTERNAL 2,索性注释掉这个参数,启动就ok了
- python 列表 元组 字典 集合
列表 lst = [i for i in range(10)] 切片 # 把下标小于2的显示出来 print(lst[:2]) # 把10个数有大到小输出 print(lst[::-1]) # 把下标 ...