[Zope-DB] Python Scripts and Database Connections

Matthew T. Kromer matt@zope.com
Mon, 13 May 2002 08:53:37 -0400

Unfortunately, you cannot do this really easily.  You sort-of-can, in 
that you can write either python scripts to create the DA object but not 
add it to any object manager, so that it is deleted when it is 
dereferenced.  Arguably, you'd want to attach it to a SESSION type 
container so that it persists for your session duration.

You'd do that by calling its manage_addSoAndSoConnection method.   Since 
the DA doesn't exist yet, you'll still need to clone a different  DA's 
base python code to make it.

DA's create connections when they are called, so you can just call it to 
get a connection object back.  The connection object has a query method, 
which takes an SQL query string and executes it.  However, a lot of this 
isn't intended to be called from DTML or python scripts, so you may have 
to finesse it quite a bit.

On Friday, May 10, 2002, at 05:03 PM, William Trenker wrote:

> Hello,
> I'm testing a Zope DA that I've put together for SQLite 
> (http://hwaci.com/sw/sqlite/index.html).  I'm getting pretty familiar 
> with Zope and am quite experienced with Python.  I've read the doc's 
> but I'm still a bit confused on one point (well, lots of points).
> Is there a way to create a Zope DB Connection instance on the fly from 
> a Python Script?  What I'd like to do is programmatically create a DB 
> Connection object on the fly, feed it some SQL, again on-the-fly, and 
> then delete the connection object.
> (The reason is that I'd like to set up a script with a front-end that 
> asks the user for a DSN and some SQL.  I don't know, in advance, what 
> database connection the user may wish to access.  But I don't want the 
> usual ZMI management screens to flash up when the script is running.)
> Thanks in advance,
> Bill
> ----------
> "The commandments of the LORD are right, bringing joy to the heart. The 
> commands of the LORD are clear, giving insight to life . . . For this 
> is the love of God, that we keep His commandments. And His commandments 
> are not burdensome." (Psalm 19:8, 1John 5:3)    
> <http://torahteacher.com/>torahteacher.com
> ---
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.351 / Virus Database: 197 - Release Date: 4/19/02