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 5 4970
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
On Tue, 2 Sep 2003 11:00:58 -0500, Skip Montanaro <sk**@pobox.com> wrote in
comp.lang.python in article
<ma*********************************@python.org> : 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("""UPDATE 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/
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("""UPDATE 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.Connection(...) c = db.cursor() c.execute("select * from cities where city='San Jose'")
4L c.execute("update cities set city='San Jose' where city='San Jose'")
0L
Skip
On Tue, 2 Sep 2003 14:04:20 -0500, Skip Montanaro <sk**@pobox.com> wrote in
comp.lang.python 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("""UPDATE 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.Connection(...) >>> c = db.cursor() >>> c.execute("select * from cities where city='San Jose'") 4L >>> c.execute("update 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.python in article
<ma*********************************@python.org> :
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/
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.python 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("""UPDATE 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.Connection(...) >>> c = db.cursor() >>> c.execute("select * from cities where city='San Jose'") 4L >>> c.execute("update 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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"...
|
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;
}...
|
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
|
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...
|
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...
|
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
|
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...
|
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...
|
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...
|
by: erikbower65 |
last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA:
1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 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: Taofi |
last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same
This are my field names
ID, Budgeted, Actual, Status and Differences
...
|
by: DJRhino1175 |
last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this -
If...
|
by: Rina0 |
last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
|
by: DJRhino |
last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer)
If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _
310030356 Or 310030359 Or 310030362 Or...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: lllomh |
last post by:
How does React native implement an English player?
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
| |