MySql -Python question

A

ataanis

Hey all, I'm running a query within some python code, and I'm having
difficulties doing something that seems to be really simple . I'm
running a query in the following form:
query01 = 'select max(DirectorID) +1 from Director;'
cursor.execute(query01)
table = cursor.fetchall()
the resulting table , is a 1 row , 1 column table,showing 1 number and
I just need that number inside that table,and save it for a different
query, but I can't get it , can anybody tell me the best way to do
this? thanks
 
D

Dennis Lee Bieber

Hey all, I'm running a query within some python code, and I'm having
difficulties doing something that seems to be really simple . I'm
running a query in the following form:
query01 = 'select max(DirectorID) +1 from Director;'
cursor.execute(query01)
table = cursor.fetchall()
the resulting table , is a 1 row , 1 column table,showing 1 number and
I just need that number inside that table,and save it for a different
query, but I can't get it , can anybody tell me the best way to do
this? thanks

What does

table[0] #first element of list "table"

return to you? Or, since fetchall() might be assuming multiple rows even
for a single row data set...

table[0][0] #first element of first sublist of list "table"
--
 
V

Vladimir 'Yu' Stepanov

Dennis said:
Hey all, I'm running a query within some python code, and I'm having
difficulties doing something that seems to be really simple . I'm
running a query in the following form:
query01 = 'select max(DirectorID) +1 from Director;'
cursor.execute(query01)
table = cursor.fetchall()
the resulting table , is a 1 row , 1 column table,showing 1 number and
I just need that number inside that table,and save it for a different
query, but I can't get it , can anybody tell me the best way to do
this? thanks

What does

table[0] #first element of list "table"

return to you? Or, since fetchall() might be assuming multiple rows even
for a single row data set...

table[0][0] #first element of first sublist of list "table"

There is no method for work with data who are presented only in one
column. This method would raise speed of work with such data as it is
not necessary to make allocation one-item tuple object.

Therefore I have a some expanded the module py-sqlplug_mysql
(http://sourceforge.net/projects/py-sqlplg-mysql) where such API it is
delivered through methods of the cursor: fetchcol0, fetchallcol0,
fetchmanycol0.

Now on a site of the project the documentation is inaccessible. The
basic methods correspond to the specification described in PEP-249.

P.S. The project while was tested only on FreeBSD and Linux though can
work and on other platforms.

P.P.S. For drawing up of the documentation the translator from Russian
is required.
 
A

ataanis

both of your suggestions don't work, that's kind of what I was trying
the whole time
 
F

Frank Millman

both of your suggestions don't work, that's kind of what I was trying
the whole time

I am sure you have read this before somewhere, but -

1. Tell us what you have tried so far
2. Tell us what you expected (or hoped) would happen
3. Tell us what actually happened, including any traceback

Then there is a possibility that someone might be able to help you.

Frank Millman
 
D

Dennis Lee Bieber

I am sure you have read this before somewhere, but -

1. Tell us what you have tried so far
2. Tell us what you expected (or hoped) would happen
3. Tell us what actually happened, including any traceback

Then there is a possibility that someone might be able to help you.
At the least, shove a print statement under the .fetchxxx() so we
can see what it claims to have returned in raw form...

No problem here:
cn = MySQLdb.connect(db="bestiaria", user="BestiariaCP")
qry = "select max(ID) + 1 from comics"
cr = cn.cursor()
cr.execute(qry) 1L
tbl = cr.fetchall()
print tbl ((85L,),)
print tbl[0] (85L,)
print tbl[0][0] 85

..fetchall() looks to be returning a tuple of tuples rather than a list
 
S

Steve Holden

both of your suggestions don't work, that's kind of what I was trying
the whole time
Well it seems firly obvious that your universe is somehow broken. Please
send it back to God in a plain wrapper and it will be replaced with a
new universe in which all programming is done in assembly language :)

regards
Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,294
Messages
2,571,511
Members
48,197
Latest member
ปั๊มเฟส|JoyLikeSEO

Latest Threads

Top