472,961 Members | 1,477 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,961 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

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),
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;""")

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:

"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
Jul 18 '05 #1
1 6757
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
from MySQLdb import cursors

Help on method execute in module MySQLdb.cursors:

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


Returns long integer rows affected, if any


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

Much appreciated,
Sheila King
Jul 18 '05 #2

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

Similar topics

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,...
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...
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,...
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...
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...
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...
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...
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...
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 -...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.