Skip to content

Commit 5476fbc

Browse files
updated code
1 parent 8ad6986 commit 5476fbc

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

code/_1_Sqlite3_Connect.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
import sqlite3
2+
3+
print("SQLite version :", sqlite3.sqlite_version) # version of the sqlite
4+
print("sqlite3 module version:", sqlite3.version) # version of the module library
5+
26
conn = sqlite3.connect('mydatabase.db') # create a connection object
7+
38
conn.close()

code/_2_Sqlite3_Create_Table.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#Python code to create a table in sqlite using STRICT option
2+
3+
import sqlite3
4+
5+
create_table_sql_query = '''
6+
7+
CREATE TABLE IF NOT EXISTS data_logger(
8+
Id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, --Primary Key
9+
Temp_Sensor1 REAL, -- Temperature values in Degrees like 34.67
10+
Temp_Sensor2 REAL, -- Temperature values in Degrees
11+
Temp_Sensor3 REAL, -- Temperature values in Degrees
12+
IP_Address TEXT, -- ip address like 192.168.1.1
13+
TimeStamp INTEGER -- time in unix time
14+
15+
) STRICT; -- Space before STRICT '''
16+
17+
connection_object = sqlite3.connect('datalogger.sqlite') # Connect to a database (or create it if it doesn't exist)
18+
cursor_object = connection_object.cursor() # Create a cursor object using the previous connection object
19+
20+
cursor_object.execute(create_table_sql_query) # create the table
21+
connection_object.commit() # commit the changes to the database
22+
connection_object.close() # close the connection
23+
24+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#Python code to create a table in sqlite using STRICT option
2+
#using context manager 'with'
3+
4+
import sqlite3
5+
6+
create_table_sql_query = '''
7+
8+
CREATE TABLE IF NOT EXISTS data_logger(
9+
Id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, --Primary Key
10+
Temp_Sensor1 REAL, -- Temperature values in Degrees like 34.67
11+
Temp_Sensor2 REAL, -- Temperature values in Degrees
12+
Temp_Sensor3 REAL, -- Temperature values in Degrees
13+
IP_Address TEXT, -- ip address like 192.168.1.1
14+
TimeStamp INTEGER -- time in unix time
15+
16+
) STRICT; -- Space before STRICT '''
17+
18+
with sqlite3.connect('datalogger.sqlite') as connection_object : # Connect to a database (or create it if it doesn't exist)
19+
cursor_object = connection_object.cursor() # Create a cursor object using the previous connection object
20+
cursor_object.execute(create_table_sql_query) # create the table
21+
22+
# connection_object.commit()
23+
# You don’t need connection_object.commit() inside a with block
24+
# SQLite auto-commits when the with block exits without errors.
25+
# But including it explicitly doesn’t harm.
26+
27+
28+
29+
30+
31+

0 commit comments

Comments
 (0)