K
K
Kir Mozor2020-12-23 19:46:32
Python
Kir Mozor, 2020-12-23 19:46:32

How to delete multiple rows of SQLite data in Python?

And so, I need to delete one row of data in the sqlite table, but I can’t figure out how to do it. And so the table has the following data. 5fe37300289fd121921226.png
I need to delete this row
5fe37335d3856502706409.png
. On the Internet, the deletion example is everywhere.
5fe3739b259ea286176847.png
But this command will delete all rows where Finance is present in the department_name column. But I need to do this:
id = 674759776
If id_1 == with my id
Then delete the data line where my id is present. That is the following line
5fe3745bea9a5130009991.png
How can this be done?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
K
Kir Mozor, 2020-12-24
@150_Kirill_150

In short, I solved the problem in the following way:

import sqlite3

conn = sqlite3.connect("local.db", check_same_thread = False) # или :memory: чтобы сохранить в RAM
cursor = conn.cursor()

my_id = 74654758345

cursor.execute('SELECT id_2 FROM stol_game WHERE id_1 = ?', (my_id, ))
id_2_turtle = cursor.fetchone()
id_2 = int(id_2_turtle[0])

cursor.execute('SELECT brosokone2 FROM stol_game WHERE id_1 = ?', (my_id, ))
brosokone2turtle = cursor.fetchone()
brosokone2 = int(brosokone2turtle[0])
                    
cursor.execute('SELECT brosoktwo2 FROM stol_game WHERE id_1 = ?', (my_id, ))
brosoktwo2turtle = cursor.fetchone()
brosoktwo2 = int(brosoktwo2turtle[0])
                    
cursor.execute('SELECT vsumme2 FROM stol_game WHERE id_1 = ?', (my_id, ))
vsumme2turtle = cursor.fetchone()
vsumme2 = int(vsumme2turtle[0])
                    
cursor.execute('SELECT stavka2 FROM stol_game WHERE id_1 = ?', (my_id, ))
stavka2turtle = cursor.fetchone()
stavka2 = int(stavka2turtle[0])

cursor.execute('SELECT frend_id FROM stol_game WHERE id_1 = ?', (my_id, ))
frend_id2turtle = cursor.fetchone()
frend_id = int(frend_id2turtle[0])
cursor.execute('SELECT brosokone1 FROM stol_game WHERE id_1 = ?', (my_id, ))
brosokone1turtle = cursor.fetchone()
brosokone1 = int(brosokone1turtle[0])
                    
cursor.execute('SELECT brosoktwo1 FROM stol_game WHERE id_1 = ?', (my_id, ))
brosoktwo1turtle = cursor.fetchone()
brosoktwo1 = int(brosoktwo1turtle[0])
                    
cursor.execute('SELECT vsumme1 FROM stol_game WHERE id_1 = ?', (my_id, ))
vsumme1turtle = cursor.fetchone()
vsumme1 = int(vsumme1turtle[0])
                    
cursor.execute('SELECT stavka1 FROM stol_game WHERE id_1 = ?', (my_id, ))
stavka1turtle = cursor.fetchone()
stavka1 = int(stavka1turtle[0])

cursor.execute('SELECT win FROM stol_game WHERE id_1 = ?', (my_id, ))
win2turtle = cursor.fetchone()
win = int(win2turtle[0])

cursor.execute('DELETE FROM stol_game WHERE id_1 = ? AND frend_id = ? AND brosokone1 = ? AND brosoktwo1 = ? AND vsumme1 = ? AND stavka1 = ? AND id_2 = ? AND brosokone2 = ? AND brosoktwo2 = ? AND vsumme2 = ? AND stavka2 = ? AND win = ?', (my_id, frend_id, brosokone1, brosoktwo1, vsumme1, stavka1, id_2, brosokone2, brosoktwo2, vsumme2, stavka2, win, ))
conn.commit()

D
Dr. Bacon, 2020-12-23
@bacon

It makes sense to read a SQL tutorial to understand the basics.

P
plyshk1n2k, 2020-12-23
@plyshk1n2k

Have you tried adding another condition to the database query?
The rows have different id_1 and id_2
Or you can use TOP, LIMIT, ROWNUM
If you are sure that the first line in the database is exactly what you need and you should delete it, then feel free to use what I described in the line above

P
PavelMos, 2020-12-23
@PavelMos

But this command will delete all lines where there is Finance in the department_name column.
Well, what's the problem then? you need to have the right number in the right column
DELETE FROM departments
WHERE id_1=674759776

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question