Skip to content

Commit 44483a6

Browse files
added new files
1 parent 5e8508d commit 44483a6

File tree

5 files changed

+80
-0
lines changed

5 files changed

+80
-0
lines changed

code/_3_Sqlite3_INSERT_data.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#adding some data into sqlite database
2+
3+
import sqlite3
4+
import time
5+
6+
# Sample data
7+
temp1 = 25.3
8+
temp2 = 34.6
9+
temp3 = 24.8
10+
ip_address = '192.168.0.101'
11+
timestamp = int(time.time()) # current Unix time
12+
13+
insert_data_sql_query = '''
14+
15+
INSERT INTO data_logger (Temp_Sensor1,
16+
Temp_Sensor2,
17+
Temp_Sensor3,
18+
IP_Address,
19+
TimeStamp)
20+
VALUES (?, ?, ?, ?, ?)
21+
'''
22+
23+
24+
# Connect and insert data
25+
with sqlite3.connect('datalogger.sqlite') as connection_object:
26+
cursor_object = connection_object.cursor()
27+
28+
cursor_object.execute(insert_data_sql_query, (temp1, temp2, temp3, ip_address, timestamp))
29+
30+
print("Data inserted successfully.")
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#adding a lot of data into table using executemany()
2+
3+
import sqlite3
4+
import time
5+
6+
# Sample data list
7+
8+
data_list = [ (25.3, 34.6, 24.8, '192.168.0.101', int( time.time() ) ),
9+
(26.1, 35.2, 25.4, '192.168.0.102', int( time.time() ) ),
10+
(24.9, 33.8, 23.7, '192.168.0.103', int( time.time() ) )
11+
]
12+
13+
14+
15+
16+
17+
insert_data_sql_query = '''
18+
19+
INSERT INTO data_logger (Temp_Sensor1,
20+
Temp_Sensor2,
21+
Temp_Sensor3,
22+
IP_Address,
23+
TimeStamp)
24+
VALUES (?, ?, ?, ?, ?)
25+
'''
26+
27+
28+
# Connect and insert data
29+
with sqlite3.connect('datalogger.sqlite') as connection_object:
30+
cursor_object = connection_object.cursor()
31+
32+
cursor_object.executemany(insert_data_sql_query,data_list)
33+
34+
print("Data inserted successfully.")
35+

code/_4_Sqlite3_Read_data_fetchall.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#Reading data from SQLite database using fetchall
2+
3+
import sqlite3
4+
5+
# Connect to the database
6+
with sqlite3.connect('datalogger.sqlite') as connection_object:
7+
cursor_object = connection_object.cursor()
8+
9+
cursor_object.execute('SELECT * FROM data_logger') #Execute a SELECT query
10+
11+
rows = cursor_object.fetchall() #Fetch all rows
12+
13+
# Print each row
14+
for row in rows:
15+
print(row)

code/datalogger.sqlite

12 KB
Binary file not shown.

code/mydatabase.db

Whitespace-only changes.

0 commit comments

Comments
 (0)