SQLite Connector to Manage Database

Tkinter SQLite SQLite Connector Part -2 Part -3

SQlite connector

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.
  1. Use file browser (Tkinter filedialog ) to browse local directory system and select the database file.
  2. On selection of the database file, it will show all the tables inside it.
  3. If a wrong file which is not a database file is selected then error message will be displayed.
  4. 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.
def my_connect(type):
    global my_conn
    f_types = [('All Files', '*.*'), 
              ('Text Document', '*.txt'),
              ('Database files',"*.db")]
    if(type=='existing'):
        file = filedialog.askopenfilename(filetypes=f_types)
    else:
        file = filedialog.asksaveasfilename(filetypes=f_types,defaultextension=".db",)

Error handling

If we select wrong file then the error message will be displayed. For this we will use our exception handling with try except code block.
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.

connection.py
connector-sqlite-part-1
SQLite Connector : Using Query window in Part - 2 SQLite Connector Part -3 (Run Queries)
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer