Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sqlite3
- __connection = None
- def get_connection():
- global __connection
- if __connection is None:
- __connection = sqlite3.connect("database.db", check_same_thread = False)
- return __connection
- def init_db(force: bool = False):
- conn = get_connection()
- c = conn.cursor()
- if force:
- c.execute("DROP TABLE IF EXISTS users_subs")
- c.execute("""
- CREATE TABLE IF NOT EXISTS users_subs (
- id INTEGER PRIMARY KEY,
- user_id INTEGER NOT NULL,
- name_sub_service TEXT NOT NULL,
- date_sub TEXT NOT_NULL,
- summ_sub INTEGER NOT NULL,
- period_sub INTEGER NOT NULL,
- finish_sub TEXT NOT NULL,
- notify_date_sub TEXT NOT NULL
- )
- """)
- conn.commit()
- def add_sub_data(user_id: int, name_sub_service: str, date_sub: str, summ_sub: int, period_sub: int, finish_sub: str, notify_date_sub: str):
- conn = get_connection()
- c = conn.cursor()
- c.execute("INSERT INTO users_subs (user_id, name_sub_service, date_sub, summ_sub, period_sub, finish_sub, notify_date_sub) VALUES(?, ?, ?, ?, ?, ?, ?)", (user_id, name_sub_service, date_sub, summ_sub, period_sub, finish_sub, notify_date_sub))
- conn.commit()
- def user_exist(user_id):
- conn = get_connection()
- c = conn.cursor()
- info = c.execute("SELECT * FROM users_subs WHERE user_id = ?", (user_id,))
- if info.fetchone() is None:
- return False
- else:
- return True
- def list_subs(user_id: int, limit: int = 10000):
- conn = get_connection()
- c = conn.cursor()
- c.execute("SELECT name_sub_service, date_sub, summ_sub, period_sub FROM users_subs WHERE user_id = ? ORDER BY id DESC LIMIT ?", (user_id, limit))
- return c.fetchall()
- if __name__ == "__main__":
- init_db()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement