Nagging problem with MySQLdb

John Fabiani jfabiani at yolo.com
Wed Nov 24 01:55:39 CET 2004


Phillip wrote:

> Hi.
> I hate the way one has to jump through countless hoops to put data in a
> db and get it out again. The straightforward MySQLdb Interface
> requireing this SQL stuff being a point in case (against SQL and those
> RDBs that is). Since other programms have to access this data I'm forced
> to use a classical DB and actually have managed to set up Mysql and a
> presumably working connection from Python to it.
> 
> I've gotten so far as to avoid errors with this sucky SQL language.
> (Guess I've gotten to pampered by Python lately :-) )
> 
> However, for about 20 hrs. now I've been trying to write data into an
> existing table. Until now no success. I hope somebody can help me with
> this. Here's the relevant code:
> 
>  >>>>>>
> ...
> #Setting up DB connection, initializing DB cursor
> elefantDb = MySQLdb.connect(user="runbase",db="elefant")
> baseFeed = elefantDb.cursor()
> 
> # going through the DictList and adding the
> # datasets into the db
> for line in sourceLinesDictList:
> # also adding some data for fields that the DB table has but our
> # DictList does't (data01,02,29)
> data01 = 'source-one'
> data02 = '0',
> data03 = line['payments-status'],
> data04 = line['order-id'],
> data05 = line['order-item-id'],
> data06 = line['payments-date'],
> data07 = line['payments-t-id'],
> data08 = line['item-name'],
> data09 = line['listing-id'],
> data10 = line['sku'],
> data11 = float(line['price'].replace(',','.',1)),
> data12 = float(line['shipping-fee'].replace(',','.',1)),
> data13 = line['quantity-purchased'],
> data14 = float(line['total-price'].replace(',','.',1)),
> data15 = line['purchase-date'],
> data16 = int(line['batch-id']),
> data17 = line['buyer-email'],
> data18 = line['buyer-name'],
> data19 = line['recipient-name'],
> data20 = line['ship-address-1'],
> data21 = line['ship-address-2'],
> data22 = line['ship-city'],
> data23 = line['ship-state'],
> data24 = int(line['ship-zip']),
> data25 = line['ship-country'],
> data26 = line['special-comments'],
> data27 = line['upc'],
> data28 = float(line['VAT'].replace(',','.',1)),
> data29 = 'fresh-unassigned'
> 
> baseFeed.execute(
> """INSERT INTO position
> (plattform,
> position_id,
> payments-status,
> order-id,
> order-item-id,
> payments-date,
> payments-t-id,
> item-name,
> listing-id,
> sku,
> price,
> shipping-fee,
> quantity-purchased,
> total-price,
> purchase-date,
> batch-id,
> buyer-email,
> buyer-name,
> recipient-name,
> ship-address-1,
> ship-address-2,
> ship-city,
> ship-state,
> ship-zip,
> ship-country,
> special-comments,
> upc,
> vat,
> elefant-signal)
> VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s,
> %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);"""
> [data01, data02, data03, data04, data05, data06, data07, data08,
> data09, data10, data11, data12, data13, data14, data15, data16, data17,
> data18, data19, data20, data21, data22, data23, data24, data25, data26,
> data27, data28, data29]
> )
> <<<<<<
> 
> The error I get with this one is:
> "TypeError: string indices must be integers"
> 
> The other wariant I've tried (using a tuple) with
>  >>>>>>
> ...# the same stuff as above and:
> (data01, data02, data03, data04, data05, data06, data07, data08, data09,
> data10, data11, data12, data13, data14, data15, data16, data17, data18,
> data19, data20, data21, data22, data23, data24, data25, data26, data27,
> data28, data29)
> <<<<<<
> 
> returns:
> "TypeError: 'str' object is not callable"
> 
> baseFeed.execute() function causing these errors.
> I'm totaly void of possible answers now. The problem apparently also
> being scarce and false MySQL and MySQLdb documentation *shrug*.
> Can anyone help?
> Thanks a lot in advance.
> 
> Phillip

One issue 
it should be "insert into table (sum fields) values (%s...), % vars"

I'm a newbie too so I might be wrong.
John



More information about the Python-list mailing list