make unique values in database and add handling exception in add_user method
Some checks failed
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is failing

This commit is contained in:
mitsuha_s 2022-05-21 13:55:28 +00:00
parent 8b04adef2b
commit c25e0c78fe

View file

@ -3,13 +3,16 @@ class Database():
def __init__(self, path): def __init__(self, path):
self.conn = sqlite3.connect(path) self.conn = sqlite3.connect(path)
self.cur = self.conn.cursor() self.cur = self.conn.cursor()
self.cur.execute('CREATE TABLE IF NOT EXISTS users (id INTEGER, telegram_id NUMERIC, PRIMARY KEY(id))') self.cur.execute('CREATE TABLE IF NOT EXISTS users (id INTEGER, telegram_id NUMERIC NOT NULL UNIQUE, PRIMARY KEY(id))')
self.cur.execute('CREATE TABLE IF NOT EXISTS feeds (id INTEGER, link TEXT, PRIMARY KEY(id))') self.cur.execute('CREATE TABLE IF NOT EXISTS feeds (id INTEGER, link TEXT NOT NULL UNIQUE, PRIMARY KEY(id))')
self.cur.execute('CREATE TABLE IF NOT EXISTS subscribes (user_id INTEGER, feed_id INTEGER, FOREIGN KEY(user_id) REFERENCES users(id), FOREIGN KEY(feed_id) REFERENCES feeds(id))') self.cur.execute('CREATE TABLE IF NOT EXISTS subscribes (user_id INTEGER, feed_id INTEGER, FOREIGN KEY(user_id) REFERENCES users(id), FOREIGN KEY(feed_id) REFERENCES feeds(id))')
self.cur.execute('CREATE TABLE IF NOT EXISTS feeds_last_items (feed_id INTEGER, link TEXT, title TEXT, description TEXT, date NUMERIC, FOREIGN KEY(feed_id) REFERENCES feeds(id))') self.cur.execute('CREATE TABLE IF NOT EXISTS feeds_last_items (feed_id INTEGER, link TEXT NOT NULL UNIQUE, title TEXT, description TEXT, date NUMERIC, FOREIGN KEY(feed_id) REFERENCES feeds(id))')
def add_user(self, telegram_id): def add_user(self, telegram_id):
self.cur.execute('INSERT INTO users (telegram_id) VALUES (:telegram_id)', {'telegram_id': telegram_id}) try:
self.cur.execute('INSERT INTO users (telegram_id) VALUES (:telegram_id)', {'telegram_id': telegram_id})
except sqlite3.IntegrityError:
return 1
self.conn.commit() self.conn.commit()
def add_rss_feed(): def add_rss_feed():
@ -29,3 +32,5 @@ class Database():
def storing_feed_content(): def storing_feed_content():
pass pass
allo = Database('bot.db')