- 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 - 限制
- 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 - 限制
- 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 - 限制
- 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 - 限制
- Python 数据访问资源
- Python 数据访问 - 快速指南
- Python 数据访问 - 有用的资源
- Python 数据访问 - 讨论
Python SQLite -Where 子句
如果要在 SQLite 中获取、删除或更新表中的特定行,则需要使用 where 子句指定条件来过滤操作表中的行。
例如,如果您有一个带有 where 子句的 SELECT 语句,则只会检索满足指定条件的行。
句法
以下是 SQLite 中 WHERE 子句的语法 -
SELECT column1, column2, columnN FROM table_name WHERE [search_condition]
您可以使用比较或逻辑运算符指定 search_condition。例如 >、<、=、LIKE、NOT 等。下面的示例将使这个概念变得清晰。
例子
假设我们使用以下查询创建了一个名为 CRICKETERS 的表 -
sqlite> CREATE TABLE CRICKETERS ( First_Name VARCHAR(255), Last_Name VARCHAR(255), Age int, Place_Of_Birth VARCHAR(255), Country VARCHAR(255) ); sqlite>
如果我们使用 INSERT 语句插入 5 条记录:
sqlite> insert into CRICKETERS values('Shikhar', 'Dhawan', 33, 'Delhi', 'India');
sqlite> insert into CRICKETERS values('Jonathan', 'Trott', 38, 'CapeTown', 'SouthAfrica');
sqlite> insert into CRICKETERS values('Kumara', 'Sangakkara', 41, 'Matale', 'Srilanka');
sqlite> insert into CRICKETERS values('Virat', 'Kohli', 30, 'Delhi', 'India');
sqlite> insert into CRICKETERS values('Rohit', 'Sharma', 32, 'Nagpur', 'India');
sqlite>
以下 SELECT 语句检索年龄大于 35 的记录 -
sqlite> SELECT * FROM CRICKETERS WHERE AGE > 35; First_Name Last_Name Age Place_Of_B Country ---------- ---------- ---- ---------- ------------- Jonathan Trott 38 CapeTown SouthAfrica Kumara Sangakkara 41 Matale Srilanka sqlite>
使用 python 的 where 子句
Cursor对象/类包含执行查询和获取数据等的所有方法。连接类的cursor方法返回一个游标对象。
因此,要使用 python 在 SQLite 数据库中创建表 -
使用 connect() 方法与数据库建立连接。
通过在上面创建的连接对象上调用cursor()方法来创建游标对象。
现在使用 Cursor 类的execute() 方法执行CREATE TABLE 语句。
例子
以下示例创建一个名为 Employee 的表并填充它。然后使用 where 子句检索年龄值小于 23 的记录。
import sqlite3
#Connecting to sqlite
conn = sqlite3.connect('example.db')
#Creating a cursor object using the cursor() method
cursor = conn.cursor()
#Doping EMPLOYEE table if already exists.
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)
#Populating the table
cursor.execute('''INSERT INTO EMPLOYEE(
FIRST_NAME, LAST_NAME, AGE, SEX, INCOME) VALUES
('Ramya', 'Rama priya', 27, 'F', 9000)''')
cursor.execute('''INSERT INTO EMPLOYEE
(FIRST_NAME, LAST_NAME, AGE, SEX, INCOME) VALUES
('Vinay', 'Battacharya', 20, 'M', 6000)''')
cursor.execute('''INSERT INTO EMPLOYEE(
FIRST_NAME, LAST_NAME, AGE, SEX, INCOME) VALUES
('Sharukh', 'Sheik', 25, 'M', 8300)''')
cursor.execute('''INSERT INTO EMPLOYEE(
FIRST_NAME, LAST_NAME, AGE, SEX, INCOME) VALUES
('Sarmista', 'Sharma', 26, 'F', 10000)''')
cursor.execute('''INSERT INTO EMPLOYEE(
FIRST_NAME, LAST_NAME, AGE, SEX, INCOME) VALUES
('Tripthi', 'Mishra', 24, 'F', 6000)''')
#Retrieving specific records using the where clause
cursor.execute("SELECT * from EMPLOYEE WHERE AGE <23")
print(cursor.fetchall())
#Commit your changes in the database
conn.commit()
#Closing the connection
conn.close()
输出
[('Vinay', 'Battacharya', 20, 'M', 6000.0)]