简体   繁体   中英

MySQLdb cursor.execute formatter

I'm working with Python and MySQLdb library. 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.

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):

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.

Obviously, I checked that the only argument is a valid string describing a valid path, just as when executed in other SO versions.

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.

Any idea why?

I also think it could be related to the python-mysqldb library version and not to the Python version. 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).

The parameters passed need to be iterables ie list or tuple. So it should be (path,) and not (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,))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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