Difference between revisions of "Database Reader - Code"
From RoboWiki
(Created page with "Return back to project page: Door camera - Jakub Vojtek Python code for the Database Reader: <syntaxhighlight lang=python> impo...") |
|||
| Line 4: | Line 4: | ||
<syntaxhighlight lang=python> | <syntaxhighlight lang=python> | ||
import sqlite3 | import sqlite3 | ||
| − | |||
| − | |||
| − | |||
| − | |||
def print_data(): | def print_data(): | ||
| − | c.execute('SELECT * FROM images') | + | with sqlite3.connect('database/face_images.db') as conn: |
| − | + | c = conn.cursor() | |
| − | + | c.execute('SELECT * FROM images') | |
| − | + | rows = c.fetchall() | |
| − | + | for row in rows: | |
| + | print(row) | ||
def delete_data_with_no_faces(): | def delete_data_with_no_faces(): | ||
| − | c.execute('DELETE FROM images WHERE detected_faces = 0') | + | with sqlite3.connect('database/face_images.db') as conn: |
| + | c = conn.cursor() | ||
| + | c.execute('DELETE FROM images WHERE detected_faces = 0') | ||
| + | conn.commit() | ||
| + | if __name__ == '__main__': | ||
| + | print("Data before deletion:") | ||
| + | print_data() | ||
| + | |||
| + | delete_data_with_no_faces() | ||
| + | |||
| + | print("Data after deletion:") | ||
| + | print_data() | ||
| − | |||
</syntaxhighlight> | </syntaxhighlight> | ||
Latest revision as of 17:49, 9 June 2024
Return back to project page: Door camera - Jakub Vojtek
Python code for the Database Reader:
import sqlite3
def print_data():
with sqlite3.connect('database/face_images.db') as conn:
c = conn.cursor()
c.execute('SELECT * FROM images')
rows = c.fetchall()
for row in rows:
print(row)
def delete_data_with_no_faces():
with sqlite3.connect('database/face_images.db') as conn:
c = conn.cursor()
c.execute('DELETE FROM images WHERE detected_faces = 0')
conn.commit()
if __name__ == '__main__':
print("Data before deletion:")
print_data()
delete_data_with_no_faces()
print("Data after deletion:")
print_data()