473,404 Members | 2,170 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Nagging problem with MySQLdb

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
Jul 18 '05 #1
5 1641
Phillip wrote:

...
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"


Right now you're just writing the equivalent of

"%s is %d years old" (name, age)

which is attempting to call a string like a function. You meant:

"%s is %d years old" % (name, age)

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
Have you fell in love with somebody / Who didn't know
-- Zhane
Jul 18 '05 #2
Phillip wrote:
Hi.
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:
>>>>>>
...
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);"""


I think you may need a comma here to separate the two arguments to execute.
[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"

Jul 18 '05 #3
Phillip wrote:
I hate the way one has to jump through countless hoops to put data in a
db and get it out again.
You would have it much easier if you discovered how to use
more of Python's capabilities to write shorter code... see
some ideas below.
# 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'], ..... data10 = line['sku'],
data11 = float(line['price'].replace(',','.',1)), ..... data16 = int(line['batch-id']),
For this sort of code, you can often make it more data-driven,
along these lines (I use globals() instead of an object since
I'm not sure you're familiar with OO programming, but an object
would make this even cleaner):

for line in sourceLinesDictList:
for i, (key, type) in enumerate([
('source-one', 'direct'),
('0', 'direct'),
('payments-status', 'str'),
...
('sku', 'str'),
('price', 'float'),
('batch-id', 'int'),
...]):
if type == 'direct':
val = key
elif type == 'str':
val = line[key]
elif type == 'float':
val = float(line[key].replace(',','.',1))
elif type == 'int':
val = int(line[key])
globals()['data%02d' % i] = val

In other words, use a list containing the names and "types" of
information, and programmatically create variables in the
global namespace, eliminating all duplication in the extraction
of data from "line".
baseFeed.execute(
"""INSERT INTO position
(plattform,
position_id,
payments-status,
order-id, .... 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]
)


Having done the insertion into globals() above, you could now replace
this monster with something like this:

... VALUES (%s);""" % ', '.join(dataList)

(in other words, inserting the list of data items, with commas
separating them) where dataList had previously been formed in a
manner something like this:

dataList = [globals()['data%02d' % i] for i in xrange(1, 30)]

....and similar ideas.

I hope something in here helps you make this task more manageable...

-Peter
Jul 18 '05 #4
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
Jul 18 '05 #5

As much as I can see
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);"""
there's a missing comma here
[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]
)


--
damjan
Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Peter Nikolaidis | last post by:
Greetings, I am attempting to get MySQLdb 0.9.2 installed on Mac OS 10.2 with a Fink distribution of Python 2.2.2. I have seen only a few posts on the subject, some of them relate to...
1
by: Michael | last post by:
Is there a mailing list or anything like that for users of the MySQLdb module? Using version 0.9.2 with Python 2.3.2 and whenever I try to open a connection I get this error: File...
0
by: Stephen Roderick | last post by:
Getting weird error, and I was wondering if anyone had any suggestions. My system appears to satisfy all MySQLdb prerequisities in terms of python, mysql, etc. Also, phpMyAdmin works fine with...
0
by: Wesley Kincaid | last post by:
I'm attempting to run a simple query through MySQLdb's cursor.execute(). However, when the request includes a timestamp field, I'm getting "ValueError: invalid literal for int(): 9-." Could...
2
by: ws Wang | last post by:
MySQLdb is working fine at command line, however when I tried to use it with mod_python, it give me a "server not initialized" error. This is working fine: ----------------------- testmy.py...
2
by: Mondal | last post by:
Hi, I am using MySQL 5.0 beta and Active Python 2.4. I have the correct version of MySQLdb installed. The problem is that I can't connect to MySQL through the Active Python interactive window. ...
4
by: elmo | last post by:
Hello, after two days of failed efforts and googling, I thought I had better seek advice or observations from the experts. I would be grateful for any input. We have various small internal web...
15
by: John Nagle | last post by:
I've been installing Python and its supporting packages on a dedicated server with Fedora Core 6 for about a day now. This is a standard dedicated rackmount server in a colocation facility,...
1
by: Aljosa Mohorovic | last post by:
i have a working MySQLdb module (/usr/lib/python2.4/site-packages/ MySQL_python-1.2.2-py2.4-linux-i686.egg), using it without problems. "clean shell" after login: python -c "import MySQLdb"...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.