Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sqlite3
- from sqlite3 import Error
- class DB:
- def __init__(self, dbname):
- try:
- self.connect = sqlite3.connect(dbname)
- self.cursor = self.connect.cursor()
- except Error:
- print("Ошибка подключения к базе данных")
- else:
- print(f"connect to {dbname}")
- def table_is_exist(self, table_name):
- sql = f"""SELECT name
- FROM sqlite_master
- WHERE type='table' AND name='{table_name}';"""
- self.cursor.execute(sql)
- rows = self.cursor.fetchall()
- return len(rows) == 1
- def create_tables(self):
- if not self.table_is_exist('notes'):
- sql = """
- CREATE table notes(
- id integer PRIMARY KEY AUTOINCREMENT,
- date_note datetime,
- title text,
- note_text text,
- is_done integer
- );
- """
- self.cursor.execute(sql)
- def add_note(self, date_note, title, note_text):
- sql = """
- INSERT INTO notes(date_note,
- title,
- note_text)
- VALUES(?, ?, ?)
- """
- self.cursor.execute(sql, (date_note, title, note_text))
- self.connect.commit()
- def read_notes(self):
- sql = """SELECT id, date_note, title, note_text, is_done
- FROM notes
- """
- self.cursor.execute(sql)
- rows = self.cursor.fetchall()
- for row in rows:
- print(row)
- db = DB("my_notes.db")
- print("SELECT notes #1")
- db.read_notes()
- db.add_note('2020-12-20 9:00:00',
- 'Выучить python!',
- "Изучить python3 и написать новый гугл")
- print("SELECT notes #2")
- db.read_notes()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement