如何使用Python从 SQLite 表中删除特定行?
在本文中,我们将讨论如何使用Python从 SQLite 表中删除特定行。
为了从 SQL 中的表中删除特定行,我们使用DELETE 查询,SQL 中的 DELETE 语句用于从表中删除现有记录。根据我们在 WHERE 子句中指定的条件,我们可以删除单个记录或多个记录。
句法:
DELETE FROM table_name
WHERE condition;
我们将创建一个表,然后在其中执行删除操作。
Python3
# importing sqlite module
import sqlite3
# create connection to the database
# my_database
connection = sqlite3.connect('my_database.db')
# create table named address of customers
# with 4 columns id,name age and address
connection.execute('''CREATE TABLE ship (ship_id INT, ship_name \
TEXT NOT NULL, ship_destination CHAR(50) NOT NULL); ''')
print("Ship table created successfully")
# close the connection
connection.close()
Python3
# import sqlite module database
import sqlite3
# create connection to the database
# my_database
connection = sqlite3.connect('my_database.db')
# insert query to insert values
connection.execute("INSERT INTO ship VALUES (1, 'tata-hitachi','noida' )")
connection.execute("INSERT INTO ship VALUES (2, 'tata-mumbai','mumbai' )")
connection.execute("INSERT INTO ship VALUES (3, 'tata-express','hyderabad' )")
# query to display all data in the table
cursor = connection.execute("SELECT * from ship")
print("Actual data")
# display row by row
for row in cursor:
print(row)
# query to delete all data where ship_id = 2
connection.execute("DELETE from ship where ship_id=2")
print("After deleting ship id = 2 row")
# display row by row
cursor = connection.execute("SELECT * from ship")
for row in cursor:
print(row)
# close the connection
connection.close()
Python3
# import sqlite module database
import sqlite3
# create connection to the database
# my_database
connection = sqlite3.connect('my_database.db')
# insert query to insert values
connection.execute("INSERT INTO ship VALUES (1, 'tata-hitachi','noida' )")
connection.execute("INSERT INTO ship VALUES (2, 'tata-mumbai','mumbai' )")
connection.execute("INSERT INTO ship VALUES (3, 'tata-express','hyderabad' )")
# query to display all data in the table
cursor = connection.execute("SELECT * from ship")
print("Actual data")
# display row by row
for row in cursor:
print(row)
# query to delete all data where ship_id = 2
connection.execute("DELETE from ship where ship_destination='hyderabad'")
print("After deleting ship address = hyderabad row")
# display row by row
cursor = connection.execute("SELECT * from ship")
for row in cursor:
print(row)
# close the connection
connection.close()
输出:
Ship table created successfully
示例 1:
用于插入数据和删除数据的Python程序,其中 2 是船的 id。
蟒蛇3
# import sqlite module database
import sqlite3
# create connection to the database
# my_database
connection = sqlite3.connect('my_database.db')
# insert query to insert values
connection.execute("INSERT INTO ship VALUES (1, 'tata-hitachi','noida' )")
connection.execute("INSERT INTO ship VALUES (2, 'tata-mumbai','mumbai' )")
connection.execute("INSERT INTO ship VALUES (3, 'tata-express','hyderabad' )")
# query to display all data in the table
cursor = connection.execute("SELECT * from ship")
print("Actual data")
# display row by row
for row in cursor:
print(row)
# query to delete all data where ship_id = 2
connection.execute("DELETE from ship where ship_id=2")
print("After deleting ship id = 2 row")
# display row by row
cursor = connection.execute("SELECT * from ship")
for row in cursor:
print(row)
# close the connection
connection.close()
输出:
示例 2:
在此示例中,删除同一表中船舶地址为 hyderabad 的数据。
蟒蛇3
# import sqlite module database
import sqlite3
# create connection to the database
# my_database
connection = sqlite3.connect('my_database.db')
# insert query to insert values
connection.execute("INSERT INTO ship VALUES (1, 'tata-hitachi','noida' )")
connection.execute("INSERT INTO ship VALUES (2, 'tata-mumbai','mumbai' )")
connection.execute("INSERT INTO ship VALUES (3, 'tata-express','hyderabad' )")
# query to display all data in the table
cursor = connection.execute("SELECT * from ship")
print("Actual data")
# display row by row
for row in cursor:
print(row)
# query to delete all data where ship_id = 2
connection.execute("DELETE from ship where ship_destination='hyderabad'")
print("After deleting ship address = hyderabad row")
# display row by row
cursor = connection.execute("SELECT * from ship")
for row in cursor:
print(row)
# close the connection
connection.close()
输出: