En este artículo, discutiremos cómo eliminar una fila específica de la tabla SQLite usando Python.
Para eliminar una fila en particular de una tabla en SQL, usamos la consulta DELETE . La instrucción DELETE en SQL se usa para eliminar registros existentes de una tabla. Podemos eliminar un solo registro o varios registros según la condición que especifiquemos en la cláusula WHERE.
Sintaxis:
ELIMINAR DE table_name
DONDE condición;
Vamos a crear una tabla y luego realizar operaciones de eliminación en ella.
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()
Producción:
Ship table created successfully
Ejemplo 1:
Programa de Python para insertar datos y eliminar datos donde 2 es la identificación del envío.
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()
Producción:
Ejemplo 2:
En este ejemplo, elimine los datos donde la dirección del envío es hyderabad en la misma tabla.
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()
Producción:
Publicación traducida automáticamente
Artículo escrito por manojkumarreddymallidi y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA