database questions ala newbie pythonist

mensanator at aol.com mensanator at aol.com
Wed Dec 1 21:23:05 EST 2004


chris wrote:
> Hello,
>           Just started with python and databases.
> I am making a small script to update a local database.
> Looked around and couldn't find to many links with info about python
with
> databases.
> Any links or other resources anyone can recommend is appreciated.
>
> My question is in the following small script:
>
> ##################################################################
> import dbi
> import odbc
> myconn = odbc.odbc('testpy')
> mycursor = myconn.cursor()
> mycursor.execute('Update Categories Set DelStatus = 0 Where ProductID
=
> 1190' )
> mycursor.close()
> myconn.close()
> ...
> ###################################################################
>
> This works fine using the literals 0 (For Delstatus) and 1190 (for
> ProductID)
> But when I try to use a variable such as:
>
> ###################################################################
> ...
> varA = '0'
> varB = '1190'
> mycursor.execute('Update Categories Set DelStatus = ' varA 'Where
ProductID
> = ' varB)

Try concatenating the components:

mycursor.execute('Update Categories Set DelStatus = ' + varA + 'Where
ProductID = ' + varB)

Without the '+' signs, you've put 4 strings inside the .exexcute
function
instead of the 1 it wants.

> ...
> ###################################################################
>
> I get errors.  Please excuse my ignorance in what is probably obvious
to
> most others within this newsgroup.....
> What I am trying to do is to update my database from a 2 field .CSV
file
> I figured I could load the CSV file into a dictionary and parse each
row
> running a query using the values from varA and VarB from the key
value
> pairs.
> Right now I am just trying to get the database part to work that is
why
> there is no CSV code.
> So I guess what I am asking is how do I pass values through the sql
> statement:
> ###################################################################
> ...
> mycursor.execute('Update Categories Set DelStatus = X1 Where
ProductID =
> X2' )    # Values replacing X1 and X2.
> ...
> ###################################################################
> Any help is greatly appreciated.
> 
> Chris




More information about the Python-list mailing list