SQLite connector to create and manage database with error reporting using Python Tkinter
SQLite is a file based database. We will create one file browser using Tkinter to browse the local directory or file system and connect to SQLite3 database file. Our application will have the following features.
Use file browser (Tkinter filedialog ) to browse local directory system and select the database file.
On selection of the database file, it will show all the tables inside it.
If a wrong file which is not a database file is selected then error message will be displayed.
There will be one button to create one new SQLite 3 database after browsing the local directory – file system.
We will keep all our connection and file handling code inside the file connection.py. Our main file connector-sqlit-part-1.py will import the function my_connect from connection.py.
Copy the source code below.
In next part we will display the list of available tables inside database with number of records, empty table and delete table commands.
We will also add query window to run any SQL.
Using file browser to locate the file
We will use one file browser or directory locator to select one database file. If this database file is having tables, then we will display the name of all tables.
try:
file1='sqlite:///'+ file
my_conn = create_engine(file1)
my_conn.execute("SELECT name FROM sqlite_master WHERE type = 'table'")
my_output='ok'
except SQLAlchemyError as e:
error = str(e.__dict__['orig'])
my_output=error
The main file connector-sqlit-part-1.py will import the connection.py file. On click of the Browse & Connect button, the function my_connect_p() will execute and it will first establish the connection by calling my_connect().
If the connection is established and there is no error then we will check the number of tables available inside the database.
We will enable or disable the query button b2 ( discussed in Next part ) based on connection status.