473,763 Members | 1,395 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

MySQLdb -- any way to get "rows matched"?

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, but is there any
way to get the number of rows matched? I want to find out, when rows
updated = 0, if there were no updates because the row wasn't found
(rows matched will = 0) or because the update would not have changed
any data (rows matched = 1).

mysql> select * from test;
+------+------+
| rver | s |
+------+------+
| 1 | new |
+------+------+
1 row in set (0.00 sec)

mysql> update test set rver = 1, s = 'new' where rver = 1;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 0

mysql> update test set rver = 1, s = 'new' where rver = 17;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0 Changed: 0 Warnings: 0

mysql> update test set rver = 1, s = 'neww' where rver = 1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0

Thanks,
Chris
Jul 18 '05 #1
5 5086

Chris> When issuing updates in mysql (in the console window), mysql will
Chris> tell you if any rows matched and how many rows were updated (see
Chris> below). I know how to get number of rows udpated using MySQLdb,
Chris> but is there any way to get the number of rows matched?

I believe the return value of the cursor's execute() method gives you the
number of rows which matched.

Skip

Jul 18 '05 #2
On Tue, 2 Sep 2003 11:00:58 -0500, Skip Montanaro <sk**@pobox.com > wrote in
comp.lang.pytho n in article
<ma************ *************** ******@python.o rg>:

Chris> When issuing updates in mysql (in the console window), mysql will
Chris> tell you if any rows matched and how many rows were updated (see
Chris> below). I know how to get number of rows udpated using MySQLdb,
Chris> but is there any way to get the number of rows matched?

I believe the return value of the cursor's execute() method gives you the
number of rows which matched.

Skip


That's not what this says?

http://groups.google.com/groups?selm...t%40python.org

Just for confirmation, a little experimentation :

MYSQL at the command line:

mysql> SELECT * from example;
+--------+------+---------+
| name | AGE | COUNTRY |
+--------+------+---------+
| sheila | 30 | US |
| arthur | 23 | NL |
| bob | 30 | US |
+--------+------+---------+
3 rows in set (0.00 sec)

mysql> UPDATE example SET AGE=30 WHERE AGE=30;
Query OK, 0 rows affected (0.00 sec)
ws matched: 2 Changed: 0 Warnings: 0
MySQLdb in Python:
c = conn.cursor()
c.execute("""UP DATE example SET AGE=30 WHERE AGE=30;""") 0L c.messages [] c.info()

''
Being a MySQL and MySQLdb newbie, I'm not sure if there is any way to find
out how many matched, via Python's MySQLdb module. I thought, from the
source code, that .messages or .info() for the cursor attributes/methods
might do it, but apparently not???
I'm using MySQL 3.23.57 and MySQLdb 0.92
--
Sheila King
http://www.thinkspot.net/sheila/
http://www.k12groups.org/
Jul 18 '05 #3

mysql> UPDATE example SET AGE=30 WHERE AGE=30;
Sheila> Query OK, 0 rows affected (0.00 sec)
Sheila> ws matched: 2 Changed: 0 Warnings: 0
Sheila> MySQLdb in Python:
c = conn.cursor()
c.execute("""UP DATE example SET AGE=30 WHERE AGE=30;""") Sheila> 0L c.messages Sheila> [] c.info() Sheila> ''

Sheila> Being a MySQL and MySQLdb newbie, I'm not sure if there is any
Sheila> way to find out how many matched, via Python's MySQLdb module. I
Sheila> thought, from the source code, that .messages or .info() for the
Sheila> cursor attributes/methods might do it, but apparently not???

I'm not sure what the return value means when executing an update. My guess
is that it returns the number of rows changed. If you want to know the
number of rows matched, execute a SELECT which matches your WHERE clause:
db = MySQLdb.Connect ion(...)
c = db.cursor()
c.execute("sele ct * from cities where city='San Jose'") 4L c.execute("upda te cities set city='San Jose' where city='San Jose'")

0L

Skip

Jul 18 '05 #4
On Tue, 2 Sep 2003 14:04:20 -0500, Skip Montanaro <sk**@pobox.com > wrote in
comp.lang.pytho n in article
<ma************ *************** *******@python. org>:
mysql> UPDATE example SET AGE=30 WHERE AGE=30;
Sheila> Query OK, 0 rows affected (0.00 sec)
Sheila> ws matched: 2 Changed: 0 Warnings: 0

Sheila> MySQLdb in Python:
>>>> c = conn.cursor()
>>>> c.execute("""UP DATE example SET AGE=30 WHERE AGE=30;""") Sheila> 0L >>>> c.messages Sheila> [] >>>> c.info() Sheila> ''

Sheila> Being a MySQL and MySQLdb newbie, I'm not sure if there is any
Sheila> way to find out how many matched, via Python's MySQLdb module. I
Sheila> thought, from the source code, that .messages or .info() for the
Sheila> cursor attributes/methods might do it, but apparently not???

I'm not sure what the return value means when executing an update. My guess
is that it returns the number of rows changed. If you want to know the
number of rows matched, execute a SELECT which matches your WHERE clause:
>>> db = MySQLdb.Connect ion(...)
>>> c = db.cursor()
>>> c.execute("sele ct * from cities where city='San Jose'") 4L >>> c.execute("upda te cities set city='San Jose' where city='San Jose'")
0L

Skip


Right. That is what I was saying, except that the OP wants to get the
number of matches from an UPDATE statement, even if they entries are not
updated.

IOW, he doesn't want to have to do both SELECT and UPDATE but is hoping to
get the information from only an UPDATE statement.

I was correcting what I believe was incorrect information in an earlier
post. i.e. you wrote:

On Tue, 2 Sep 2003 11:00:58 -0500, Skip Montanaro <sk**@pobox.com > wrote in
comp.lang.pytho n in article
<ma************ *************** ******@python.o rg>:
Chris> When issuing updates in mysql (in the console window), mysql will
Chris> tell you if any rows matched and how many rows were updated (see
Chris> below). I know how to get number of rows udpated using MySQLdb,
Chris> but is there any way to get the number of rows matched?

I believe the return value of the cursor's execute() method gives you the
number of rows which matched.

Skip


which is not correct.

Roberto Gomez from Argentina contacted me in private email in response to
my last post (apparently having trouble posting to the mailing list today
for some reason) and pointed out that the MySQLdb exceeds the required
DB-API for Python, as the result of "execute" for a cursor is undefined
according to the DB-API. True enough. His point was, that if there is a
concern that the program be compatible with possible other databases in the
future, besides MySQLdb, that this feature of the MySQLdb cursor.execute
method should not be assumed.

In summary the "gee I'm a newbie" remark was meant to indicate that I do
not know the answer to the OP's question, not that I didn't know how to get
the result from a SELECT statement. Sorry for being unclear.
--
Sheila King
http://www.thinkspot.net/sheila/
http://www.k12groups.org/
Jul 18 '05 #5
On Tue, 02 Sep 2003 20:30:26 GMT, Sheila King <sh****@spamcop .net>
wrote:
On Tue, 2 Sep 2003 14:04:20 -0500, Skip Montanaro <sk**@pobox.com > wrote in
comp.lang.pyth on in article
<ma*********** *************** ********@python .org>:
mysql> UPDATE example SET AGE=30 WHERE AGE=30;
Sheila> Query OK, 0 rows affected (0.00 sec)
Sheila> ws matched: 2 Changed: 0 Warnings: 0

Sheila> MySQLdb in Python:
>>>> c = conn.cursor()
>>>> c.execute("""UP DATE example SET AGE=30 WHERE AGE=30;""")

Sheila> 0L
>>>> c.messages

Sheila> []
>>>> c.info()

Sheila> ''

Sheila> Being a MySQL and MySQLdb newbie, I'm not sure if there is any
Sheila> way to find out how many matched, via Python's MySQLdb module. I
Sheila> thought, from the source code, that .messages or .info() for the
Sheila> cursor attributes/methods might do it, but apparently not???

I'm not sure what the return value means when executing an update. My guess
is that it returns the number of rows changed. If you want to know the
number of rows matched, execute a SELECT which matches your WHERE clause:
>>> db = MySQLdb.Connect ion(...)
>>> c = db.cursor()
>>> c.execute("sele ct * from cities where city='San Jose'")

4L
>>> c.execute("upda te cities set city='San Jose' where city='San Jose'")

0L

Skip


Right. That is what I was saying, except that the OP wants to get the
number of matches from an UPDATE statement, even if they entries are not
updated.

IOW, he doesn't want to have to do both SELECT and UPDATE but is hoping to
get the information from only an UPDATE statement.


[snip]

Exactly correct. Since mysql provides this info "for free" when doing
an update, I was hoping to use that via MySQLdb, but it appears that
it's not available. Thanks for the help.

-Chris
Jul 18 '05 #6

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

Similar topics

1
2087
by: Max | last post by:
Hello everybody, I have a problem with javascript. Basically I have a frame that is split with this code: <frameset rows="0,*"> <frame name="top" src="nothing.html"> <frame name="center" src="page.html"> </frameset>
2
4307
by: Kiki | last post by:
Hi, just a small question. I've just looked at a friends code and he's done this: int rowsAffected = sql.ExecuteNonQuery("stored_procedure", cmdParams); if(rowsAffected < 1) { return false; } else {.......
9
24973
by: jensen bredal | last post by:
I have a dataView and i only want to get the first x rows . Is there a way of doing this without using Foreach? Thanks JB
3
3873
by: Andy Fish | last post by:
Hi, when there are no rows to display in my asp.net datagrid control, I'd like to put in a message saying "No results" or some such Ideally I'd like the message to be centred across all the columns, but I'd settle for the message appearing in a single cell, or header/footer line. maybe there's a way to make the whole control invisible when there are no rows in the data source.
0
2225
by: romancoelho | last post by:
Hey everyone, I know this has to be simple, but can't figure out how to do it. How can I display the grid lines in all rows in the gird. The default behavior of the DataGridView seems to be that it only shows grid lines around rows that have data in it. If there are not enough rows to fill the height of the grid, then the grid lines don't show up around the "empty rows" I say "empty rows" because there actually a row there. So it seems...
17
4586
by: pbd22 | last post by:
Hi. How does one return a range of rows. I know that "Top 5" will return rows 0 - 5 but, how do I get 6 - 10? thanks
3
2083
by: nkoriginal | last post by:
Hi: I want to create an store procedure for an ASP page. I've a form with 100 fields and I want to insert the information in: 1) one table with 100 rows or 2) in 5 tables Now, my idea is create an store procedure for insert BUT if tomorrow I need to add more fields and rows in sql, I would like not touch nothing in my code.
5
3874
by: maury | last post by:
Hello, I have a DB table with data filled from a weather sensor probe, I have one row every 10 minutes and the data fields is not in DateTime format but in string format: yyyyMMddHHmm So for example I have 200804140340 200804140350 200804140400 200804140410
8
2756
by: phub11 | last post by:
Hi - I have a function which appends row(s) to the bottom of a table: function mouseUpHandler() { var table = document.getElementById("mytable"); var rowCount = table.rows.length; var addRow = table.insertRow(rowCount); addRow.id = cnt; var addCell = addRow.insertCell(1); addCell.id = cnt;
0
9386
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10145
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9938
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5270
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5406
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3523
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2793
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.