This tutorial is intended to be a conversation with newbies to Dabo.  Please 
check the Dabo wiki for more information and other tutorials.

Let me say what this is not.  It is not a tutorial on Python , installation of 
Databases, or the use of SQL.  I have to assume you understand the syntax of 
Python and SQL.  And you will follow the install instructions provided by 
your favorite database.  I will make an effort not use any thing to advanced 
and I'm sure the code will be easy to read even for a laymen. 

OK let's start with some background.  Dabo's primary goal is to provide an 
easy way to create a desktop application and most desktop applications 
require persistent data.  In that light Dabo supports several database 
engines.  SQLite, MySQL, MS-SQL, FireBird, and my favorite Postgres.  
However, Dabo does not provide the actual interfaces to access the database 
engines.  The developer is expected to download and install the python 
interfaces to the database of choice.  Please check the 
http://dabodev.com/wiki/FrontPage for instructions on where to find the 
required files.  For example if you are using Postgres you will need 
psycopg2.  The wiki provides the link to 
http://initd.org/tracker/psycopg/wiki/PsycopgTwo where you will find the 
required files.  I highly recommend that you test your setup by connecting 
with the database and retrieving some data.  That way you know your setup 
works and therefore you would expect Dabo to work too.  

I know some reader will be thinking that their favorite database is better 
than my choice.  I don't disagree nor do I agree.  It's just that I have had 
great success with Postgres and it is the database I'm using for this 
tutorial.   However, everything thing I'll be showing should apply to any of 
the databases supported.  In it's basic form Dabo database support is 
agnostic.  But rest assured that Dabo can  support your favorite databases 
advance features too.  

Dabo is a 3 tier framework.  The User Interface, Business Rules, and the 
Database.  We will be working with “bizObjects”.  What is a 'bizObject” you 
ask?  There is a very good explanation on the wiki FAQ – 
http://dabodev.com/wiki/FAQs#Bizobj.  I like to think of it in simple terms.  
It's the code that allows me to create, save, and delete data using my UI.  
The “bizObject” provides validation and understands my data (business 
rules?).  A simple but very powerful concept.  So to support the bizObject we 
need to supply a few things, a connection to a database, a description of the 
table, and any rules we want enforced.  

The Connection:

All the python interfaces Dabo uses are suppose to comply with DB-API 2.0.  
That means there is a standard way to access the database.  Here's the link 
http://www.python.org/dev/peps/pep-0249/ if you need to check it out.  If you 
do read it there is a section on “connection” and you come to realize that 
what is passed for the connection parameter is not defined.  It  (the 
parameters) depends on what is required by the database engine.  So Dabo 
needs you to supply those parameters before it can make a connection to your 
database.

Right off the bat let me say the easiest way to setup a connection is to use 
the “CxnEditor.py” app.  It works and and is a great example of Dabo eating 
it's own dog food (CxnEditor was created using Dabo).  I use it for my 
projects and if there was a better way - I'd use it.  But it really does not 
do much (all the real work is done in the framework).  CxnEditor creates a 
XML file that contains the parameters required by the python connection 
interface that applies to your database.  Like user name, password, host, 
database name or anything else that is needed to allow a database connection. 
It makes sense that we will need a connection before we start asking for data 
from the database.  But where do you put the code and what does the code look 
like.

When you are hand coding a form you will create something similar to the 
following:

class MainForm(dabo.ui.dForm):
        
        def afterInit(self):
                #place some UI code here
                
                self.layout()
                
if __name__ == "__main__":
        app = dabo.dApp()
        
        app.BasePrefKey = "fileTutor"
        app.setAppInfo("appName", "File Tutorial ")
        
        app.MainFormClass = MainForm
        
        app.start()


And in the “class MainForm” (our entry point) you will need to add a 
method “createBizobjs”.  This is where you will add your connection 
instructions and add descriptions of bizObjects.  To start we add the 
connection information as follows:

#this method uses the cnxml file created by CxnEditor
def createBizobjs(self):
        self.Application.addConnectFile("FileName.cnxml")
                # FileName = the name created with the CxnEditor.py
                # This call will actually create the connection if it hasn't 
already
                # been made. If it has, it returns the existing connection, so 
that 
                # multiple connections aren't used up.
        self.connection = self.Application.getConnectionByName("connectionName")

Don't let the two names "FileName.cnxml" and "connectionName" confuse you.  
The "connectionName" is the name of the connection within the 
file "FileName.cnxml".  It is possible to have more than one connection name 
within the same file but that is beyond this tutorial.  So “self.connection” 
is the actual connection we will be providing to our bizObjects.  Remember, 
that is what really is allowing our program to talk to the database.   When 
ever Dabo needs to communicate with the database it uses 
the “self.connection” object.

For those of you that need to know how to create a “self.connection” without 
using the CxnEditor here it is:

from dConnectInfo import dConnectInfo
connInterface = dConnectInfo(DbType=”Postgres”)
connInterface.Host = "192.168.1.201"
connInterface.Database = "DatabaseName"
connInterface.User = "john"
connInterface.PlainTextPassword = "passWord"

self.connection = dConnection(connInterface).getConnection()

If your database needs more – such as a port address.  Add it – as in

connInterface.Port = "5432".  

But take my advise and use the CxnEditor.  It's easy and it works.  There 
maybe some special case where the cnxml file will not meet your needs.  But I 
haven't run into it.  

So our code looks like:

class MainForm(dabo.ui.dForm):
        
        def afterInit(self):
                #place some UI code here
                
                self.layout()

        def createBizobjs(self):
                self.Application.addConnectFile("FileName.cnxml")
                # FileName = the name created with the CxnEditor.py
                # This call will actually create the connection if it hasn't 
already
                # been made. If it has, it returns the existing connection, so 
that 
                # multiple connections aren't used up.
                self.connection = 
self.Application.getConnectionByName("connectionName")
                
if __name__ == "__main__":
        app = dabo.dApp()
        
        app.BasePrefKey = "bizObjTutor"
        app.setAppInfo("appName", "bizObject Tutorial ")
        
        app.MainFormClass = MainForm
        
        app.start()

So far we haven't said much.  Only that we need a connection to a database and 
the bizObjects use it to communicate with the database.  But without this 
information nothing happens and the most powerful Dabo feature is useless.  

The next installment I'll provide a small form with a UI and we will build our 
first bizObject.  Your job will be to insure that you have your favorite 
database running and working with python.

John Fabiani


_______________________________________________
Post Messages to: Dabo-users@leafe.com
Subscription Maintenance: http://leafe.com/mailman/listinfo/dabo-users
Searchable Archives: http://leafe.com/archives/search/dabo-users
This message: http://leafe.com/archives/byMID/dabo-users/[EMAIL PROTECTED]

Reply via email to