maroph

Create/Connect SQLite Database

Nov 5th, 2019
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.74 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. #
  3. # Source: SQLite Python: Creating a New Database
  4. # https://www.sqlitetutorial.net/sqlite-python/creating-database/
  5. #
  6. import sqlite3
  7. from sqlite3 import Error
  8.  
  9. def create_connection(db_file):
  10.     """ create a database connection to a SQLite database """
  11.     con = None
  12.     try:
  13.         if db_file != "":
  14.             # use a file based database
  15.             conn = sqlite3.connect(db_file)
  16.         else:
  17.             # use a in-memory database
  18.             conn = sqlite3.connect(':memory:')
  19.         print(sqlite3.version)
  20.     except Error as e:
  21.         print(e)
  22.     finally:
  23.         if conn:
  24.             conn.close()
  25.  
  26. if __name__ == '__main__':
  27.     #create_connection("./pythonsqlite.db")
  28.     create_connection("")
Advertisement
Add Comment
Please, Sign In to add comment