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 - Where 子句

在执行 SELECT、UPDATE 或 DELETE 操作时,您可以使用 WHERE 子句指定条件来过滤记录。操作将对满足给定条件的记录执行。

语法

以下是 PostgreSQL 中 WHERE 子句的语法 −

SELECT column1, column2, columnN
FROM table_name
WHERE [search_condition]

您可以使用比较或逻辑运算符指定 search_condition。如 >、<、=、LIKE、NOT 等。以下示例将使这个概念更加清晰。

示例

假设我们使用以下查询创建了一个名为 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 语句检索年龄大于 35 的记录 −

postgres=# SELECT * FROM CRICKETERS WHERE AGE > 35;
first_name  | last_name  | age | place_of_birth | country
------------+------------+-----+----------------+-------------
Jonathan    | Trott      | 38  | CapeTown       | SouthAfrica
Kumara      | Sangakkara | 41  | Matale         | Srilanka
(2 rows)
postgres=#

使用 Python 的 Where 子句

要使用 Python 程序从表中获取特定记录,请执行带有 WHERE 子句的 SELECT 语句,并将其作为参数传递给 execute() 方法。

示例

以下 Python 示例演示了如何使用 Python 来使用 WHERE 命令。

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()

#如果 EMPLOYEE 表已经存在则添加注释。
cursor.execute("DROP TABLE IF EXISTS EMPLOYEE")
sql = '''CREATE TABLE EMPLOYEE(
   FIRST_NAME CHAR(20) NOT NULL,
   LAST_NAME CHAR(20),
   AGE INT,
   SEX CHAR(1),
   INCOME FLOAT
)'''
cursor.execute(sql)

#填充表
insert_stmt = "INSERT INTO EMPLOYEE (FIRST_NAME, LAST_NAME, AGE, SEX, INCOME) 
   VALUES (%s, %s, %s, %s, %s)"
data = [('Krishna', 'Sharma', 19, 'M', 2000), 
   ('Raj', 'Kandukuri', 20, 'M', 7000),
   ('Ramya', 'Ramapriya', 25, 'M', 5000),
   ('Mac', 'Mohan', 26, 'M', 2000)]
cursor.executemany(insert_stmt, data)

#使用 where 子句检索特定记录
cursor.execute("SELECT * from EMPLOYEE WHERE AGE <23")
print(cursor.fetchall())

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

#关闭连接
conn.close()

输出

[('Krishna', 'Sharma', 19, 'M', 2000.0), ('Raj', 'Kandukuri', 20, 'M', 7000.0)]