File tree Expand file tree Collapse file tree 3 files changed +60
-0
lines changed Expand file tree Collapse file tree 3 files changed +60
-0
lines changed Original file line number Diff line number Diff line change 1
1
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
+
2
6
conn = sqlite3 .connect ('mydatabase.db' ) # create a connection object
7
+
3
8
conn .close ()
Original file line number Diff line number Diff line change
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
+
Original file line number Diff line number Diff line change
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
+
You can’t perform that action at this time.
0 commit comments