mysqldb SELECT COUNT reurns 1

S

SMALLp

Hy! I nave another problem I can't solve!
<code>

import MySQLdb as mysql

connectionString = {"host":"localhost", "user":"root",
"passwd":"pofuck", "db":"fileshare"}
dataTable = "files"
conn = mysql.connect(host=connectionString["host"],
user=connectionString["user"], passwd=connectionString["passwd"],
db=connectionString["db"])

cursor = conn.cursor()
sql = "SELECT COUNT(*) FROM " + dataTable
res = cursor.execute(sql)
print res

<code>
It prints 1, and there are 88 rows in table. SELECT works fine, but
SELECT COUNT(*) makes problems.

Help! Thanks in advance!
 
R

Rob Williscroft

SMALLp wrote in in comp.lang.python:
Hy! I nave another problem I can't solve!
<code>

import MySQLdb as mysql
cursor = conn.cursor()
sql = "SELECT COUNT(*) FROM " + dataTable
res = cursor.execute(sql)

I think you need to do:
res = cursor.fetchone()[0]

print res

<code>
It prints 1, and there are 88 rows in table. SELECT works fine, but
SELECT COUNT(*) makes problems.

Rob.
 
S

SMALLp

Rob said:
SMALLp wrote in in comp.lang.python:
Hy! I nave another problem I can't solve!
<code>

import MySQLdb as mysql
cursor = conn.cursor()
sql = "SELECT COUNT(*) FROM " + dataTable
res = cursor.execute(sql)

I think you need to do:
res = cursor.fetchone()[0]

print res

<code>
It prints 1, and there are 88 rows in table. SELECT works fine, but
SELECT COUNT(*) makes problems.

Rob.
Of course! Thanks!
 
J

John Machin

Hy! I nave another problem I can't solve!
<code>

import MySQLdb as mysql

connectionString = {"host":"localhost", "user":"root",
"passwd":"pofuck", "db":"fileshare"}
dataTable = "files"
conn = mysql.connect(host=connectionString["host"],
user=connectionString["user"], passwd=connectionString["passwd"],
db=connectionString["db"])

cursor = conn.cursor()
sql = "SELECT COUNT(*) FROM " + dataTable
res = cursor.execute(sql)
print res

<code>
It prints 1, and there are 88 rows in table.

What makes you think that it should return 88?
SELECT works fine, but

In what sense does a bare "SELECT" work fine??
SELECT COUNT(*) makes problems.

Help! Thanks in advance!

Consider helping yourself, e.g.:
(1) search the web for "mysqldb tutorial"
(2) read the MySQLdb documentation
(3) read http://www.python.org/dev/peps/pep-0249/

You need to use one of the cursor.fetchXXXX methods to get your
results. The result from a call to cursor.execute is NOT defined in
the Python DBAPI. If you are interested in portability of your code
across database software, you should not rely on it. The value 1
probably just means that the execute call succeeded.

Do read carefully the descriptions of what is returned by each of the
cursor.fetchXXXX methods.
 
I

Ian Clark

connectionString = {"host":"localhost", "user":"root",
"passwd":"pofuck", "db":"fileshare"}
dataTable = "files"
conn = mysql.connect(host=connectionString["host"],
user=connectionString["user"], passwd=connectionString["passwd"],
db=connectionString["db"])

Just to let you know, that last line can be rewritten as:

conn = mysql.connect(**connectionString)


Ian
 
M

mike

connectionString = {"host":"localhost", "user":"root",
"passwd":"pofuck", "db":"fileshare"}
dataTable = "files"
conn = mysql.connect(host=connectionString["host"],
user=connectionString["user"], passwd=connectionString["passwd"],
db=connectionString["db"])

Just to let you know, that last line can be rewritten as:

conn = mysql.connect(**connectionString)

Ian

Try using cursor.fetchall() instead of cursor.fetchone()
 

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

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top