-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_script.py
More file actions
67 lines (55 loc) · 1.73 KB
/
setup_script.py
File metadata and controls
67 lines (55 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"""
setup_script.py
This script is designed to provide an example of creating and inserting into a sqlite database.
"""
import sqlite3
from sqlite3 import Error
def create_table(conn, create_table_sql):
"""
create a table from the create_table_sql statement
:param conn: Connection object
:param create_table_sql: a CREATE TABLE statement
"""
try:
c = conn.cursor()
c.execute(create_table_sql)
except Error as e:
print('Error: ' + str(e))
def insert_log_message(name, statement):
"""
insert a record into the specified table
:param name: provide the name of the existing database
:param statement: provide a valid insert statement
"""
try:
conn = sqlite3.connect(name + '.db')
if conn:
c = conn.cursor()
c.execute('INSERT INTO Log (log_message) VALUES (?)', statement)
conn.commit()
except Error as e:
print('Error: ' + str(e))
finally:
if conn:
conn.close()
def create_database(name):
"""
create a connection and a table if one doesn't exist
:param name: provide a name for the database
"""
conn = None
sql_create_sample_table = 'CREATE TABLE IF NOT EXISTS Log (log_id INTEGER PRIMARY KEY AUTOINCREMENT, log_message ' \
'VARCHAR(255) NOT NULL);'
try:
conn = sqlite3.connect(name + '.db')
if conn:
create_table(conn, sql_create_sample_table)
except Error as e:
print('Error: ' + str(e))
finally:
if conn:
conn.close()
if __name__ == '__main__':
create_database('sample')
insert_log_message('sample', ['A message to test the log table'])
print('Script executed.')