Skip to content

Commit 3a4928c

Browse files
added files
1 parent 44483a6 commit 3a4928c

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed

code/_5_Sqlite3_UPDATE.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#updating the SQLite table data using UPDATE
2+
3+
import sqlite3
4+
5+
# New data that will be used to update.
6+
new_temp1 = 2900.5
7+
new_temp2 = 3500.0
8+
new_temp3 = 2800.7
9+
10+
timestamp_to_update = 1746599012 # time stamp of the first row
11+
12+
update_data_sql_query = '''
13+
UPDATE data_logger
14+
SET Temp_Sensor1 = ?, Temp_Sensor2 = ?, Temp_Sensor3 = ?
15+
WHERE TimeStamp = ?
16+
'''
17+
18+
# Connect to database
19+
with sqlite3.connect("datalogger.sqlite") as connnection_object:
20+
cursor_object = connnection_object.cursor()
21+
22+
23+
24+
cursor_object.execute(update_data_sql_query, (new_temp1, new_temp2, new_temp3, timestamp_to_update))
25+
connnection_object.commit()
26+
27+
if cursor_object.rowcount > 0:
28+
print("Row updated successfully.")
29+
else:
30+
print("No row found with the given timestamp.")

code/_6_Sqlite3_DELETE_Row.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#deleting a row using Python and SQLite
2+
3+
import sqlite3
4+
5+
timestamp_to_delete = 1746705380 # time stamp of the first row
6+
7+
delete_sql_query = "DELETE FROM data_logger WHERE TimeStamp = ?"
8+
9+
with sqlite3.connect("datalogger.sqlite") as connnection_object:
10+
cursor_object = connnection_object.cursor()
11+
12+
13+
cursor_object.execute(delete_sql_query, (timestamp_to_delete,))
14+
15+
connnection_object.commit()
16+
17+
if cursor_object.rowcount > 0:
18+
print(f"{cursor_object.rowcount} row(s) deleted.")
19+
else:
20+
print("No row found with that timestamp.")

code/_7_Sqlite3_Table_Schema.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#Retrieve the Schema of a Table Using Python
2+
3+
import sqlite3
4+
5+
# Connect to the SQLite database
6+
with sqlite3.connect("datalogger.sqlite") as connnection_object:
7+
cursor_object = connnection_object.cursor()
8+
9+
# Query the schema of a specific table (e.g., 'data_logger')
10+
cursor_object.execute("""
11+
SELECT sql FROM sqlite_master
12+
WHERE type='table' AND name='data_logger'
13+
""")
14+
15+
result = cursor_object.fetchone()
16+
17+
if result:
18+
print("Schema for 'data_logger':\n")
19+
print(result[0]) # This is the CREATE TABLE statement
20+
21+
else:
22+
print("Table 'data_logger' not found.")

code/datalogger.sqlite

8 KB
Binary file not shown.

0 commit comments

Comments
 (0)