📜  Python SQLite – WHERE 子句

📅  最后修改于: 2022-05-13 01:54:50.330000             🧑  作者: Mango

Python SQLite – WHERE 子句

使用 Where 子句是为了使我们的搜索结果更加具体,使用 SQL/SQLite 中的 where 子句,我们可以继续指定从数据库检索数据时必须满足的特定条件。

如果我们想检索、更新或删除一组特定的数据,我们可以使用 where 子句。如果我们在您的数据库表中没有条件匹配值,我们可能没有得到任何返回。

SQL 中的 WHERE 子句:

使用Python的 SQLite 中的 WHERE 子句:

在Python SQLite Cursor 对象/类中包含执行 SQL 查询以执行操作等的所有方法。 Cursor 是返回游标对象的连接类的方法。

因此,要在Python中执行 SQLite 命令,我们需要完成 3 件基本的事情 -

  • 使用 connect() 方法建立与数据库的连接。
  • 使用 cursor() 方法创建一个游标对象。
  • 现在可以使用 Cursor 类的 execute() 方法执行 SQLite 查询/语句。

我们将创建一个数据库来管理有关学生的数据。我们将存储每个学生的信息,然后我们还将创建一种使用 where 子句检索、更新和删除学生数据的方法。

让我们创建数据库(geekforgeeks_student.db)和一个表(STUDENT)。

Python3
# import the sqlite3 module
import sqlite3
  
# Define connection and cursor
connection = sqlite3.connect('geekforgeeks_student.db')
cursor = connection.cursor()
  
# create table
cursor.execute("DROP TABLE IF EXISTS STUDENT")
createTable = '''CREATE TABLE STUDENT(
   Student_ID int, First_Name VARCHAR(100),
   Last_Name VARCHAR(100), Age int,
   Department VARCHAR(100)
)'''
cursor.execute(createTable)
  
# check the database creation data
if cursor:
    print("Database Created Successfully !")
else:
    print("Database Creation Failed !")
  
# Commit the changes in database and Close the connection
connection.commit()
connection.close()


Python3
# import the sqlite3 module
import sqlite3
  
# Define connection and cursor
connection = sqlite3.connect('geekforgeeks_student.db')
cursor = connection.cursor()
  
# Insert data into the table
cursor.execute("INSERT INTO STUDENT VALUES (1,'Rohit', 'Pathak', 21, 'IT')")
cursor.execute("INSERT INTO STUDENT VALUES (2,'Nitin', 'Biradar', 21, 'IT')")
cursor.execute("INSERT INTO STUDENT VALUES (3,'Virat', 'Kohli', 30, 'CIVIL')")
cursor.execute("INSERT INTO STUDENT VALUES (4,'Rohit', 'Sharma', 32, 'COMP')")
  
# printing the cursor data
if cursor:
    print("Data Inserted !")
else:
    print("Data Insertion Failed !")
  
# Commit the changes in database and Close the connection
connection.commit()
connection.close()


Python3
import sqlite3
  
connection = sqlite3.connect('geekforgeeks_student.db')
cursor = connection.cursor()
  
# WHERE CLAUSE TO RETRIEVE DATA
cursor.execute("SELECT * FROM STUDENT WHERE Department = 'IT'")
  
# printing the cursor data
print(cursor.fetchall())
  
connection.commit()
connection.close()


Python3
import sqlite3
  
connection = sqlite3.connect('geekforgeeks_student.db')
cursor = connection.cursor()
  
# WHERE CLAUSE TO RETRIEVE DATA
cursor.execute("SELECT * from STUDENT WHERE First_name Like'R%'")
  
# printing the cursor data
print(cursor.fetchall())
  
connection.commit()
connection.close()


Python3
import sqlite3
  
connection = sqlite3.connect('geekforgeeks_student.db')
cursor = connection.cursor()
  
# WHERE CLAUSE TO UPDATE DATA
cursor.execute("UPDATE STUDENT SET Department ='E&TC' WHERE Student_ID = 2")
  
# printing the cursor data
cursor.execute("SELECT * from STUDENT")
print(cursor.fetchall())
  
connection.commit()
connection.close()


Python3
import sqlite3
  
connection = sqlite3.connect('geekforgeeks_student.db')
cursor = connection.cursor()
  
# WHERE CLAUSE TO DELETE DATA
cursor.execute("DELETE from STUDENT WHERE Age = 32")
  
#printing the cursor data
cursor.execute("SELECT * from STUDENT")
print(cursor.fetchall())
  
connection.commit()
connection.close()


输出:

Database Created Successfully!

上面的代码将在本地创建“geekforgeeks_student.db”文件。

要查看“geekforgeeks_student.db”的内容,我们可以使用它并加载我们的 .db 文件,如下所示 –

在上图中,我们可以看到已经在数据库中创建了表和表架构。

现在我们将数据插入到 STUDENT 表中。

蟒蛇3

# import the sqlite3 module
import sqlite3
  
# Define connection and cursor
connection = sqlite3.connect('geekforgeeks_student.db')
cursor = connection.cursor()
  
# Insert data into the table
cursor.execute("INSERT INTO STUDENT VALUES (1,'Rohit', 'Pathak', 21, 'IT')")
cursor.execute("INSERT INTO STUDENT VALUES (2,'Nitin', 'Biradar', 21, 'IT')")
cursor.execute("INSERT INTO STUDENT VALUES (3,'Virat', 'Kohli', 30, 'CIVIL')")
cursor.execute("INSERT INTO STUDENT VALUES (4,'Rohit', 'Sharma', 32, 'COMP')")
  
# printing the cursor data
if cursor:
    print("Data Inserted !")
else:
    print("Data Insertion Failed !")
  
# Commit the changes in database and Close the connection
connection.commit()
connection.close()

输出:

Database Inserted!

上面的代码将数据插入到 STUDENT 表中

以下代码展示了 Where 子句的使用

示例1:检索IT系学生的数据

蟒蛇3

import sqlite3
  
connection = sqlite3.connect('geekforgeeks_student.db')
cursor = connection.cursor()
  
# WHERE CLAUSE TO RETRIEVE DATA
cursor.execute("SELECT * FROM STUDENT WHERE Department = 'IT'")
  
# printing the cursor data
print(cursor.fetchall())
  
connection.commit()
connection.close()

输出:

我们在 STUDENT 表中有 2 条记录,其部门是 IT。

示例 2:检索名字以“R”开头的学生的数据。我们还可以在 where 子句中使用字符,如下所示

蟒蛇3

import sqlite3
  
connection = sqlite3.connect('geekforgeeks_student.db')
cursor = connection.cursor()
  
# WHERE CLAUSE TO RETRIEVE DATA
cursor.execute("SELECT * from STUDENT WHERE First_name Like'R%'")
  
# printing the cursor data
print(cursor.fetchall())
  
connection.commit()
connection.close()

输出:

我们在 STUDENT 表中有 2 条记录,其名字以字母“R”开头。

例3:更新Student ID为4的学生数据

蟒蛇3

import sqlite3
  
connection = sqlite3.connect('geekforgeeks_student.db')
cursor = connection.cursor()
  
# WHERE CLAUSE TO UPDATE DATA
cursor.execute("UPDATE STUDENT SET Department ='E&TC' WHERE Student_ID = 2")
  
# printing the cursor data
cursor.execute("SELECT * from STUDENT")
print(cursor.fetchall())
  
connection.commit()
connection.close()

输出:

检查数据库内容

系为学生 ID 2 更新。

例4:删除年龄ID为30的学生数据

蟒蛇3

import sqlite3
  
connection = sqlite3.connect('geekforgeeks_student.db')
cursor = connection.cursor()
  
# WHERE CLAUSE TO DELETE DATA
cursor.execute("DELETE from STUDENT WHERE Age = 32")
  
#printing the cursor data
cursor.execute("SELECT * from STUDENT")
print(cursor.fetchall())
  
connection.commit()
connection.close()

输出:

检查数据库内容

已删除年龄为 32 岁学生的数据。