Python 数据访问教程

Python 数据访问主页

Python MySQL

Python MySQL 简介 Python MySQL 数据库连接 Python MySQL 创建数据库 Python MySQL 创建表 Python MySQL 插入数据 Python MySQL 选择数据 Python MySQL Where 子句 Python MySQL 排序 Python MySQL 更新表 Python MySQL 删除数据 Python MySQL 删除表 Python MySQL Limit 子句 Python MySQL 连接 Python MySQL 游标对象

Python PostgreSQL

Python PostgreSQL 简介 Python PostgreSQL 数据库连接 Python PostgreSQL 创建数据库 Python PostgreSQL 创建表 Python PostgreSQL 插入数据 Python PostgreSQL 选择数据 Python PostgreSQL Where 子句 Python PostgreSQL 排序 Python PostgreSQL 更新表 Python PostgreSQL 删除数据 Python PostgreSQL 删除表 Python PostgreSQL Limit 子句 Python PostgreSQL 连接 Python PostgreSQL 游标对象

Python SQLite

Python SQLite 简介 Python SQLite 建立连接 Python SQLite 创建表 Python SQLite 插入数据 Python SQLite 选择数据 Python SQLite Where 子句 Python SQLite 排序 Python SQLite 更新表 Python SQLite 删除数据 Python SQLite 删除表 Python SQLite Limit 子句 Python SQLite 连接 Python SQLite 游标对象

Python MongoDB

Python MongoDB 简介 Python MongoDB 创建数据库 Python MongoDB 创建集合 Python MongoDB 插入文档 Python MongoDB 查找 Python MongoDB 查询 Python MongoDB 排序 Python MongoDB 删除文档 Python MongoDB 删除集合 Python MongoDB 更新 Python MongoDB Limit 子句

Python 数据访问资源

Python 数据访问 快速指南 Python 数据访问 有用资源 Python 数据访问 讨论


Python PostgreSQL - Select 选择数据

您可以使用 SELECT 语句检索 PostgreSQL 中现有表的内容。在此语句中,您需要指定表的名称,它会以表格格式返回其内容,这被称为结果集。

语法

以下是 PostgreSQL 中 SELECT 语句的语法 −

SELECT column1, column2, columnN FROM table_name;

示例

假设我们使用以下查询创建了一个名为 CRICKETERS 的表 −

postgres=# CREATE TABLE CRICKETERS (
   First_Name VARCHAR(255), Last_Name VARCHAR(255), 
   Age int, Place_Of_Birth VARCHAR(255), Country VARCHAR(255)
);
CREATE TABLE
postgres=#

如果我们使用 INSERT 语句向其中插入 5 条记录,如下所示 −

postgres=# insert into CRICKETERS values('Shikhar', 'Dhawan', 33, 'Delhi', 'India');
INSERT 0 1
postgres=# insert into CRICKETERS values('Jonathan', 'Trott', 38, 'CapeTown', 'SouthAfrica');
INSERT 0 1
postgres=# insert into CRICKETERS values('Kumara', 'Sangakkara', 41, 'Matale', 'Srilanka');
INSERT 0 1
postgres=# insert into CRICKETERS values('Virat', 'Kohli', 30, 'Delhi', 'India');
INSERT 0 1
postgres=# insert into CRICKETERS values('Rohit', 'Sharma', 32, 'Nagpur', 'India');
INSERT 0 1

以下 SELECT 查询从 CRICKETERS 表中检索 FIRST_NAME、LAST_NAME 和 COUNTRY 列的值。

postgres=# SELECT FIRST_NAME, LAST_NAME, COUNTRY FROM CRICKETERS;
first_name  | last_name  | country
------------+------------+-------------
Shikhar     | Dhawan     | India
Jonathan    | Trott      | SouthAfrica
Kumara      | Sangakkara | Srilanka
Virat       | Kohli      | India
Rohit       | Sharma     | India
(5 rows)

如果要检索每条记录的所有列,则需要用"*"替换列名称,如下所示 −

postgres=# SELECT * FROM CRICKETERS;
first_name  | last_name  | age | place_of_birth | country
------------+------------+-----+----------------+-------------
Shikhar     | Dhawan     | 33  | Delhi          | India
Jonathan    | Trott      | 38  | CapeTown       | SouthAfrica
Kumara      | Sangakkara | 41  | Matale         | Srilanka
Virat       | Kohli      | 30  | Delhi          | India
Rohit       | Sharma     | 32  | Nagpur         | India
(5 rows)
postgres=#

使用 python 检索数据

对任何数据库执行 READ 操作意味着从数据库获取一些有用的信息。您可以使用 psycopg2 提供的 fetch() 方法从 PostgreSQL 获取数据。

Cursor 类提供了三种方法,即 fetchall()、fetchmany() 和 fetchone(),

  • fetchall() 方法检索查询结果集中的所有行并将它们作为元组列表返回。 (如果我们在检索几行后执行此操作,它将返回剩余的行)。

  • fetchone() 方法获取查询结果中的下一行并将其作为元组返回。

  • fetchmany() 方法类似于 fetchone(),但它检索查询结果集中的下一组行,而不是单个行。

注意 − 结果集是使用游标对象查询表时返回的对象。

示例

以下 Python 程序连接到 PostgreSQL 的名为 mydb 的数据库并从名为 EMPLOYEE 的表中检索所有记录。

import psycopg2

#建立连接
conn = psycopg2.connect(
   database="mydb", user='postgres', password='password', host='127.0.0.1', port= '5432'
)

#设置自动提交为 false
conn.autocommit = True

#使用 cursor() 方法创建游标对象
cursor = conn.cursor()

#检索数据
cursor.execute('''SELECT * from EMPLOYEE''')

#从表中获取第一行
result = cursor.fetchone();
print(result)

#从表中获取第一行
result = cursor.fetchall();
print(result)

#在数据库中提交你的更改
conn.commit()

#关闭连接
conn.close()

输出

('Ramya', 'Rama priya', 27, 'F', 9000.0)
[('Vinay', 'Battacharya', 20, 'M', 6000.0),
('Sharukh', 'Sheik', 25, 'M', 8300.0),
('Sarmista', 'Sharma', 26, 'F', 10000.0),
('Tripthi', 'Mishra', 24, 'F', 6000.0)]