如何删除表中的多行?使用单个代码,不需要在内部键入execute和conditions两次


import sqlite3
conn = sqlite3.connect('example.db') # connectiong to database or creating new if table does 
not exist
cursor = conn.cursor() # allows python to use sql commands
cursor.execute('''CREATE TABLE IF NOT EXISTS Fruits( #creating table
code TEXT PRIMARY KEY, # text data-type, primary key -constraint
name TEXT,
price FLOAT);
''')
conn.commit() # saves all changes made
fruit_list = [('1','blackberry', 300.00),('2','raspberry', 250.00), ('3','bananas', 150.00), 
('4','strawberry', 200.00)] #list of tuples
cursor.executemany('INSERT INTO Fruits VALUES(?,?,?)', fruit_list) # inserting data into table

cursor.execute("DELETE FROM Fruits WHERE name='blackberry'"(cursor.eexecute("DELETE FROM Fruits WHERE price='150.00'"(

cursor.execute('SELECT * FROM Fruits')
show_all = cursor.fetchall()
for e in show_all:
print('code: {} | name: {} | price {}'.format(e[0],e[1],e[2]))

您可以将WHERE子句中的所有条件与逻辑运算符OR组合在一条语句中:

DELETE 
FROM Fruits 
WHERE name='blackberry' OR price='150.00'

相关内容

最新更新