473,385 Members | 1,622 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,385 software developers and data experts.

MySQLdb: execute query results...

I have searched for an answer to this question on the comp.lang.python
archives at Google Groups, and also looked through the (sparse) MySQLdb
documentation, and cannot confirm for a FACT what I think I know.

Looking for affirmation before I go "assuming" something and getting myself
into trouble somewhere along the line...

Here is what I *think* I know...let me know if I'm wrong, for crying out
loud!!!

If I do something like:
db = MySQLdb.connect(passwd="mypass", host="localhost", .... db="mydb", user="myusername") c = db.cursor()
and then later do:
result = c.execute("""<some proper MySQL query here>""")
doesn't result always return the number of affected rows?

As a more concrete example:

I made the following table:

mysql> CREATE TABLE example(
-> name VARCHAR(14),
-> AGE INT,
-> COUNTRY VARCHAR(15));
Query OK, 0 rows affected (0.03 sec)
And then in Python did the following INSERTS and UPDATE:
c.execute("""INSERT INTO example (name, AGE, COUNTRY) VALUES ("sheila", 29, "US");""") 1L c.execute("""INSERT INTO example (name, AGE, COUNTRY) VALUES ("arthur", 23, "NL");""") 1L c.execute("""INSERT INTO example (name, AGE, COUNTRY) VALUES ("bob", 29, "US");""") 1L result = c.execute("""UPDATE example SET AGE=30 WHERE AGE=29;""")
result

2L
Can I *RELY* on that "2L" result of the UPDATE statement under the cursor's
"execute" method representing the number of rows updated? Or am I assuming
too much?

Like I say, I looked through several sources, including my O'Reilly MySQL
book and cannot verify this for a fact from docs.

I did find this, however:
http://www.mysql.com/doc/en/UPDATE.html

"UPDATE returns the number of rows that were actually changed. In MySQL
Version 3.22 or later, the C API function mysql_info() returns the number
of rows that were matched and updated and the number of warnings that
occurred during the UPDATE. "

Which seems to corroborate, although they are not referring to the Python
DB-API specifically.

Thanks for any clarification on this matter,
--
Sheila King
http://www.thinkspot.net/sheila/
http://www.k12groups.org/
Jul 18 '05 #1
1 6790
On Sun, 31 Aug 2003 19:14:00 -0500, mackstann <ma**@incise.org> wrote in
comp.lang.python in article
<ma**********************************@python.org >:
Looked in site-packages/MySQLdb/cursors.py, and it seems you are
correct.
from MySQLdb import cursors
help(cursors.BaseCursor.execute)


Help on method execute in module MySQLdb.cursors:

execute(self, query, args=None) unbound MySQLdb.cursors.BaseCursor
method
Execute a query.

[...]

Returns long integer rows affected, if any

HTH,


Thanks for the confirmation. I didn't look at the source code. :/

Much appreciated,
--
Sheila King
http://www.thinkspot.net/sheila/
http://www.k12groups.org/
Jul 18 '05 #2

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

Similar topics

5
by: Chris Stromberger | last post by:
When issuing updates in mysql (in the console window), mysql will tell you if any rows matched and how many rows were updated (see below). I know how to get number of rows udpated using MySQLdb,...
7
by: Brian Kelley | last post by:
I am trying to use threads and mysqldb to retrieve data from multiple asynchronous queries. My basic strategy is as follows, create two cursors, attach them to the appropriate databases and then...
2
by: Tim Williams | last post by:
I'm trying to write a simple python program to access a MySQL database. I'm having a problem with using MySQLdb to get the results of a SQL command in a cursor. Sometimes the cursor.execute works,...
21
by: John Fabiani | last post by:
Hi, I'm a newbie and I'm attempting to learn howto create a select statement. When I use >>> string1='18 Tadlock Place' >>> cursor.execute("SELECT * FROM mytest where address = %s",string1) All...
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...
7
by: Bernard Lebel | last post by:
Hello, I'm stumbled at a serious problem, and quite frankly getting desparate. This is a rather long-winded one so I'll try to get straight to the point. I have this Python program, that...
2
by: borris | last post by:
doesn anyone know a good reference, tute or examples of MySQLdb's dictCursor I want to pass dictionaries into the sql exec statements. I could only succeed with text as values this is how I...
2
by: dave.dex | last post by:
Hi all, I've been searching the docs like mad and I'm a little new to python so apologies if this is a basic question. I would like to extract the results of the following query into a list -...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.