Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sqlite3
- class PhrasesStorage():
- dbfile = "phrases.db"
- def __init__(self):
- with sqlite3.connect(self.dbfile) as conn:
- cursor = conn.cursor()
- cursor.execute("""CREATE TABLE IF NOT EXISTS phrases
- (id INTEGER PRIMARY KEY, phrase TEXT)
- """)
- def editPhrase(self, id, new_text):
- with sqlite3.connect(self.dbfile) as conn:
- cursor = conn.cursor()
- cursor.execute("UPDATE phrases SET phrase=? WHERE id=?", (new_text, id,))
- conn.commit()
- return True
- def getPhrases(self, id=None):
- with sqlite3.connect(self.dbfile) as conn:
- cursor = conn.cursor()
- if id is None:
- cursor.execute("SELECT * FROM phrases")
- else:
- cursor.execute("SELECT * FROM phrases WHERE id=?", [(id)])
- return cursor.fetchall()
- def addPhrase(self, text):
- with sqlite3.connect(self.dbfile) as conn:
- cursor = conn.cursor()
- cursor.execute("INSERT INTO phrases (phrase) VALUES (?)", (text,))
- conn.commit()
- return True
- def deletePhrase(self, id):
- with sqlite3.connect(self.dbfile) as conn:
- cursor = conn.cursor()
- cursor.execute("DELETE FROM phrases WHERE id=?", [(id),])
- conn.commit()
- return True
- def main():
- ps = PhrasesStorage()
- ps.addPhrase("Привет")
- ps.addPhrase("Как дела")
- ps.addPhrase("Что делаешь")
- phrases = ps.getPhrases()
- ps.deletePhrase( phrases[1][0] )
- phrases = ps.getPhrases()
- ps.editPhrase( phrases[-1][0], "Как поживаешь?" )
- phrases = ps.getPhrases()
- for p in phrases:
- print(p[0], p[1])
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement