A generic database browser¶
The basic anatomy of a Camelot application is described in the hello world tutorial.
This tutorial will demonstrate how to build a more complex application. The application that will be build is a generic database browser, something like a modest version pgAdmin, MySql Workbench or SQL Server Management Studio.
Topics demonstrated will include the storing of user preferences, connecting to a database with SQLAlchemy and presenting database tables to the user.
To try this tutorial, make sure you have Python installed with a database driver for which you have a test database.
Get started¶
The hello world tutorial explains how to get started with a very basic application. This application is going to be a bit more complex, the first difference is that our database browser will store user preferences such as the name of the database to connect to. Store user preferences is taken care of by Qt through the QSettings. To be able to store and retrieve these preferences, Qt needs some information about your application. In a Camelot application, this information is stored in an instance of camelot.admin.application_admin.ApplicationAdmin :
from camelot.admin.application_admin import ApplicationAdmin
app_admin = ApplicationAdmin(name='Database Administrator',
author='Conceptive Engineering',
domain='app_admin.python-camelot.com')
Now we will create the actual camelot.admin.action.application.Application action, with a model_run method that simply informs the user something great is going to happen. The constructor of this action takes an camelot.admin.application_admin.ApplicationAdmin instance as its argument to know where to store and retrieve preferences :
from camelot.admin.action.application import Application
from camelot.view import action_steps
class DatabaseAdministrator(Application):
"""Application that alows the user to view and modify table data
in a database"""
def model_run(self, model_context):
yield action_steps.UpdateProgress('Start Database Administrator')
To complete the skeleton of the application, we add some magic to get it running when the file is executed:
from camelot.view.main import main_action
if __name__=='__main__':
db_admin = DatabaseAdministrator(app_admin)
main_action(db_admin)
For the application to have a purpose, we will now implement the model_run method.
Connect to the database¶
Camelot comes with a small class to store and retrieve database connection information (profiles) in the user preferences, this is the camelot.core.profile.ProfileStore. It also has the associated action that allows the user to modify this information, and select a database connection from all the connections it knows about. This makes it straightforward to ask this information to the user :
from camelot.core.profile import ProfileStore
from camelot.admin.action.application_action import SelectProfile
profile_store = ProfileStore()
profile = yield SelectProfile(profile_store)
When the application now starts, it will ask the user for the needed connection information.

Once the user has set the database connection information, we can use this to create a SQLAlchemy Engine. The Engine acts as a connection pool to the selected database.
engine = profile.create_engine()