fork download
  1. import sqlite3
  2. import logging
  3.  
  4. logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
  5. level=logging.INFO)
  6. logger = logging.getLogger(__name__)
  7.  
  8. connect = sqlite3.connect('database.db')
  9. cursor = connect.cursor()
  10. cursor.execute("CREATE TABLE IF NOT EXISTS quotes (Id INTEGER PRIMARY KEY, text TEXT);")
  11. connect.commit()
  12.  
  13.  
  14. def quote_get(id=None):
  15. data = None
  16. try:
  17. with connect:
  18. if id:
  19. cursor.execute("SELECT Id, text FROM quotes WHERE Id = (?);", (id,))
  20. data = cursor.fetchone()
  21. else:
  22. cursor.execute("SELECT * FROM quotes ORDER BY RANDOM() LIMIT 1;")
  23. data = cursor.fetchone()
  24. except Exception as error:
  25. logger.warning("quote_get('%s') caused an '%s" % (id, error))
  26. return data
  27.  
  28.  
  29. if __name__ == '__main__':
  30. print(quote_get())
  31. print(quote_get(1))
  32.  
Runtime error #stdin #stdout #stderr 0.02s 34144KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "./prog.py", line 8, in <module>
sqlite3.OperationalError: unable to open database file