-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathsqlite-connection.py
37 lines (32 loc) · 1.49 KB
/
sqlite-connection.py
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
import os
import sqlite3
from datetime import datetime
# f"sqlite://{db_path}:?cache=shared"
# sqlite:///:memory: (or, sqlite://)
# sqlite:///relative/path/to/file.db
# sqlite:////absolute/path/to/file.db
path_to_database = os.environ.get("PATH_TO_SQLITE")
if not path_to_database:
path_to_database = "file::memory:?cache=shared"
if __name__ == '__main__':
connection = sqlite3.connect(path_to_database)
connection.execute("""CREATE TABLE if not exists messages (
receive_time timestamp,
message_group text,
message_url text,
message_title text,
message_full_url text,
black_marker_reason text);""")
connection.execute("insert into messages (message_group, message_title, receive_time) values (?, ?, ?)",
(u"Java-Entwicklung group", u"message #1", datetime.now()))
connection.execute("insert into messages (message_group, message_title, receive_time) values (?, ?, ?)",
(u"Python-Entwicklung group", u"message #2",
# datetime parsing, parsing datetime, datetimeformat
datetime.strptime("01/07/2020, 3:46 PM", '%d/%m/%Y, %I:%M %p')))
connection.commit()
single_value = connection.execute("select * from messages where message_group = ?",
("Java-Entwicklung group",)).fetchone()
print(single_value)
for each_record in connection.execute("select * from messages where message_group is not null"):
print(each_record)
connection.close()