简体   繁体   English

MySQLdb cursor.execute格式化程序

[英]MySQLdb cursor.execute formatter

I'm working with Python and MySQLdb library. 我正在使用Python和MySQLdb库。 This is part of a code which has been working for a lot of time. 这是已经使用了很多时间的代码的一部分。

I was testing if the code executes correctly in other Ubuntu versions since we are planning a SO upgrade. 我正在测试代码是否可以在其他Ubuntu版本中正确执行,因为我们计划进行SO升级。

The following code works fine in Ubuntu 12.04 (our baseline system now with Python 2.7.3), Ubuntu 14.04.5 (Python 2.7.6), but doesn't in Ubuntu 16.04.1 (Python 2.7.12): 以下代码在Ubuntu 12.04(我们的基准系统现在已使用Python 2.7.3),Ubuntu 14.04.5(Python 2.7.6)中运行良好,但在Ubuntu 16.04.1(Python 2.7.12)中却无法正常运行:

def updateOutputPath(path):
    try:
        # Connect to database
        with closing(MySQLdb.connect(host=Constants.database_host,
            user=Constants.database_user, passwd=Constants.database_passwd,
            db=Constants.database_name)) as db:

            # Create a cursor to execute queries
            with closing(db.cursor()) as cursor:
                # Execute query
                cursor.execute('UPDATE configuration SET value=%s WHERE ' +
                    'property=\'OUTPUT_PATH\'', (path))
                # Save changes in the database and close connection
                db.commit()
    except MySQLdb.Error, e:
        print_exception('Database error', e)
        print_db_query(cursor)

In the cursor.execute statement, I get the following error: not all arguments converted during string formatting. cursor.execute语句中,出现以下错误:并非在字符串格式化期间转换了所有参数。

Obviously, I checked that the only argument is a valid string describing a valid path, just as when executed in other SO versions. 显然,我检查了唯一的参数是否是描述有效路径的有效字符串,就像在其他SO版本中执行时一样。

I could just create a string and pass it to the cursor.execute statement and the problem would be over, but I am curious about this problem. 我可以创建一个字符串并将其传递给cursor.execute语句,问题将结束,但我对此问题感到好奇。

Any idea why? 知道为什么吗?

I also think it could be related to the python-mysqldb library version and not to the Python version. 我也认为它可能与python-mysqldb库版本有关,而与Python版本无关。 Its version is 1.2.3-1 in Ubuntu 12.04, 1.2.3-2 in Ubuntu 14.04, and 1.3.7-1 in Ubuntu 16.04 (I assume this update is related to the usage of Mysql-server 5.7 in this OS version). 在Ubuntu 12.04中其版本为1.2.3-1,在Ubuntu 14.04中为1.2.3-2,在Ubuntu 16.04中为1.3.7-1(我认为此更新与该OS版本中Mysql-server 5.7的使用有关) 。

The parameters passed need to be iterables ie list or tuple. 传递的参数必须是可迭代的,即列表或元组。 So it should be (path,) and not (path) 所以应该是(path,)而不是(path)

cursor.execute('UPDATE configuration SET value=%s WHERE ' +
                    'property=\'OUTPUT_PATH\'', (path,))

>>> path = 'hello'
>>> a = (path)
>>> type(a)
<type 'str'>
>>> b = (path,)
>>> type(b)
<type 'tuple'>
>>> for x in a:
...     print(x)
... 
h
e
l
l
o
>>> for x in b:
...     print(x)
... 
hello

If you pass (path ) it would be the string which will be iterated as each character of path string, and not as items of the tuple, the correct tuple format is (path,)) 如果传递(path ),它将是将作为路径字符串的每个字符而不是作为元组的项进行迭代的字符串,正确的元组格式为(path,))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM