DB beginner help

Frithiof Andreas Jensen frithiof.jensen at die_spammer_die.ericsson.com
Thu Aug 5 09:58:28 CEST 2004

"Zeljko Vrba" <mordor at fly.srk.fer.hr> wrote in message
news:slrnch3ms4.7mn.mordor at fly.srk.fer.hr...

> In Perl I always use question marks and have no trouble switching the
> application between different databases. How do you guys do it in Python
> (of course, without rewriting all queries?)

Do your database work from a "connection" object and use a database-specific
function to create that object. Most of the time that will work with several
SQL databases.

I use a class wrapper for the specific database that is given a connection
object when the class is initialised. The wrapper hides the lameness of SQL
in some obvious situations - f.ex calculating a running balance like a bank
statement is easiest done by the application, but procedures will work also.

I also think that a dicts are a good way to "paste" information into SQL
query strings

It goes sort of like:

        # open the stock database and insert the information.
            cursor = self.conn.cursor()
                INSERT INTO stocks (ticker, exchange, stockName,
                VALUES ('%(Ticker)s', '%(Exchange)s', '%(stockName)s,
                """ % colkey)
                Failed to create new Entry for %(Ticker)s, on %(Exchange)s'
                """ % colkey, exc_info=True)
            # account opened.
            log.info('Entry Created for %(Ticker)s, on %(Exchange)s'
                     % colkey)
        # done
        return None

..... Funny how every amateur programmer seem to dabble in stocks :-)

The "connection" is the standardised way of representing a database as an
object in Python. *How* to get the connection is database-dependent. You
might also be bitten by the various interpretations of SQL in the underlying
databases; Time/Date fields are always dubious, some databases only work
with few data types, even Strings Only(!), and some corners of SQL like
CHECK constraints (PySQLite) and even FOREIGN KEY constraints (Seen it on
something, I discarded for that reason - GadFly?) may not be enforced!

More information about the Python-list mailing list