· 6 years ago · Nov 24, 2019, 11:16 PM
1#usr/bin/env python
2#-*- coding:utf8 -*-
3
4import sqlite3
5
6class database:
7
8 def __init__(self, file):
9 self.file = file
10
11 def connect(self):
12 self.conn = sqlite3.connect(self.file)
13 print("Connected to the database {db}".format(db=self.file))
14 self.c = self.conn.cursor()
15
16 # Create the tables 'Messages', 'Topics' and 'Queue' if they don't exist
17
18 self.c.execute("""CREATE TABLE IF NOT EXISTS Messages (
19 MessageID decimal,
20 TopicID decimal,
21 Date datetime,
22 Author varchar,
23 Message text
24 )""")
25 self.c.execute("""CREATE TABLE IF NOT EXISTS Topics (
26 URL text,
27 TopicID decimal,
28 Title text,
29 Author varchar,
30 Date datetime
31 )""")
32 self.c.execute("""CREATE TABLE IF NOT EXISTS Queue (
33 Url text,
34 TopicID text
35 )""")
36 self.conn.commit()
37
38 def add_message(self, msgID, topicID, date, author, msg):
39 self.c.execute('INSERT INTO Messages VALUES (?, ?, ?, ?, ?)', (msgID, topicID, date, author, msg))
40 self.conn.commit()
41
42 def read_messages(self):
43 self.c.execute("SELECT * FROM messages")
44 print(self.c.fetchall())
45
46 def add_topic(self, url, topicID, title, author, date):
47 self.c.execute('INSERT INTO Topics VALUES (?, ?, ?, ?, ?)', (url, topicID, title, author, date))
48 self.conn.commit()
49
50 def read_topics(self):
51 self.c.execute("SELECT * FROM Topics")
52 print(self.c.fetchall())
53
54 def add_to_queue(self, url, topicID):
55 self.c.execute('INSERT INTO Queue VALUES (?, ?)', (url, topicID))
56 self.conn.commit()