与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. 深入理解PHP的运行模式

    PHP运行模式有4钟:1)cgi 通用网关接口(Common Gateway Interface))2) fast-cgi 常驻 (long-live) 型的 CGI3) cli  命令行运行   ( ...

  2. EXCEL(1)级联下拉框

    EXCEL级联下拉框 http://jingyan.baidu.com/article/3c343ff756e0cf0d377963f9.html 在输入一些多级项目时,如果输入前一级内容后,能够自动 ...

  3. Python终极coding

    作为一名程序员,除了需要具备解决问题的思路以外,代码的质量和简洁性也很关键.因为从一个人的代码可以直接看出你的基本功.对于Python而言,这就意味着你需要对Python的内置功能和库有很深入的了解. ...

  4. CMDB服务器管理系统【s5day89】:深入理解Java的接口和抽象类

    对于面向对象编程来说,抽象是它的一大特征之一.在Java中,可以通过两种形式来体现OOP的抽象:接口和抽象类.这两者有太多相似的地方,又有太多不同的地方.很多人在初学的时候会以为它们可以随意互换使用, ...

  5. Linux记录-open-falcon开源监控系统部署

    参考https://book.open-falcon.org/zh_0_2/quick_install/prepare.html一.安装后端1.环境准备yum -y install redisyum ...

  6. js常用数据类型(Number,String,undefined,boolean) 引用类型( function,object,null ),其他数据类型( 数组Array,时间Date,正则RegExp ),数组与对象的使用

    js常用数据类型 数字类型 | 字符串类型 | 未定义类型 | 布尔类型 typeof()函数查看变量类型 数字类型  Number var a1 = 10; var a2 = 3.66; conso ...

  7. Centos7下安装Docker

    1.首先卸载旧版: yum remove docker \ docker-client \ docker-client-latest \ docker-common \ docker-latest \ ...

  8. @JsonView的使用,entity中指定向前台返回哪些字段

    使用步骤: 1.使用接口来声明多个视图 2.在值对象的get方法上指定视图 3.在Controller方法上指定视图

  9. 第29月第14天 evpp

    1.evpp https://github.com/Qihoo360/evpp/tree/master/examples/recipes/self_control_timer https://blog ...

  10. AB PLC与西门子S7-1200以太网通信

    前言:在项目实际应用中,经常会遇到两个不同厂家的PLC需要互联进行通信交换数据,由于各自的通信协议有所不同,实现起来的难度较大,通常的做法是借助第三方的网关.本文介绍的是AB PLC与西门子S7-12 ...