在Django中更改mysql-python(MySQLdb)的CLIENT_FOUND_ROWS标志? [英] change CLIENT_FOUND_ROWS flag in django for mysql-python (MySQLdb)?

查看:80
本文介绍了在Django中更改mysql-python(MySQLdb)的CLIENT_FOUND_ROWS标志?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对MySQL 5.5 INSERT ... ON DUPLICATE KEY UPDATE行有问题,导致正常Django项目中的cursor.rowcount不匹配

I have a problem with MySQL 5.5 INSERT ... ON DUPLICATE KEY UPDATE rows effected mismatch cursor.rowcount in a normal Django project

根据 doc :

对于INSERT ... ON DUPLICATE KEY UPDATE语句,如果将行作为新行插入,则每行的受影响行值为1;如果更新了现有行,则为2;如果将现有行设置为,则为0.它的当前值. 如果您指定CLIENT_FOUND_ROWS标志,如果将现有行设置为当前值,则受影响的行值为1(而不是0).

For INSERT ... ON DUPLICATE KEY UPDATE statements, the affected-rows value per row is 1 if the row is inserted as a new row, 2 if an existing row is updated, and 0 if an existing row is set to its current values. If you specify the CLIENT_FOUND_ROWS flag, the affected-rows value is 1 (not 0) if an existing row is set to its current values.

所以我尝试了一下,看来在mysql shell中,重复显示INSERT ... ON DUPLICATE KEY UPDATE语句会显示

So I tried in out, it seems that in mysql shell, repeating the INSERT ... ON DUPLICATE KEY UPDATE statement will show

查询确定,受影响的0行(0.00秒)

Query OK, 0 rows affected (0.00 sec)

在mysql-python(导入MySQLdb)中,

In mysql-python (import MySQLdb),

cursor.rowcount始终为1,无论插入/更新/什么都没有更新

cursor.rowcount will always be 1 regardless of insert/update/nothing updated

我到处搜索,找不到在Django中更改CLIENT_FOUND_ROWS标志的方法.有人知道吗?

I searched everywhere and can not find a way to change the CLIENT_FOUND_ROWS flag in Django. Anyone know how?

推荐答案

好的,我知道了.

在django/db/backends/mysql/base.py中有

In django/db/backends/mysql/base.py there's

kwargs['client_flag'] = CLIENT.FOUND_ROWS
kwargs.update(settings_dict['OPTIONS'])

从源代码中,我们可以像这样更改django项目settings.py

From the source code we could just change django project settings.py like this

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.',
        'NAME': '',                      
        'USER': '',                      
        'PASSWORD': '',                  
        'HOST': '', 
        'PORT': '',                      
        'OPTIONS': {
            'client_flag': 0
        }
    }
}

这篇关于在Django中更改mysql-python(MySQLdb)的CLIENT_FOUND_ROWS标志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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