如何获取匹配的行从MySQLdb.cursors.Cursor python2.6 [英] How to get matched Rows from MySQLdb.cursors.Cursor python2.6

查看:348
本文介绍了如何获取匹配的行从MySQLdb.cursors.Cursor python2.6的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用python2.6和MySQLdb。我有一个包含此数据的表格

I'm working with python2.6 and MySQLdb. I have a table with this data

+----+--------+
| id | status |
+----+--------+
| 1  |     A  |
| 2  |     B  |
| 3  |     B  |
+----+--------+

以这样的例子做一个mysql更新:

I want to do an mysql update like this example:

UPDATE my_table SET status = "A" where id in (1,2,3,10001);
Query OK, 2 rows affected (0.03 sec)
Rows matched: 3  Changed: 2  Warnings: 0

我需要知道更新中的所有id是否在数据库中退出。我得到这个信息的想法是比较我尝试更新的项目数与匹配的行数。在示例中,数字是4对3。

And I need to know if all the ids in the update exits in the database. My idea to get this information was to compare the number of items I tried to update vs the number of matched rows. In the example the numbers are 4 vs 3.

问题是,我不知道如何从游标信息获得匹配行。我只在cursor._info ='行匹配:3更改:2警告:0'中看到此信息。

The problem is that i don't know how to get the "Matched Rows" from the cursor information. I only see this information in cursor._info = 'Rows matched: 3 Changed: 2 Warnings: 0'.

cursor.rowcount是已更改的行数,因此=(

The cursor.rowcount is the number of changed rows, so =(

谢谢!

推荐答案

FOUND_ROWS选项使 cursor.rowcount 返回匹配的行数: / p>

The FOUND_ROWS option makes cursor.rowcount return the number of matched rows instead:

db_connection = MySQLdb.connect(
        host = settings['dbHost'],
        user = settings['dbUser'],
        passwd = settings['dbPass'],
        db = settings['dbName'],
        client_flag = MySQLdb.constants.CLIENT.FOUND_ROWS
        )

文件:

http://mysql-python.sourceforge.net/MySQLdb- 1.2.2 / public / MySQLdb.constants.CLIENT-module.html
http://dev.mysql.com/doc/refman/5.6/en/mysql-real-connect.html

(MySQLdb文档中有一个错字。 client_flags应为client_flag)

(There's a typo in the MySQLdb docs. "client_flags" should be "client_flag")

这篇关于如何获取匹配的行从MySQLdb.cursors.Cursor python2.6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆