Python-自动执行MySQL索引:传递参数 [英] Python - automating MySQL index: passing parameter

查看:145
本文介绍了Python-自动执行MySQL索引:传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数,该函数具有用于自动表索引的代码的新改进版本:

I have a function with a new improved version of the code for automatic table indexing:

def update_tableIndex(self,tableName):
    getIndexMySQLQuery = """SELECT numberID
    FROM %s;""" % (tableName,)

    updateIndexMySQLQuery = """UPDATE %s 
    SET numberID=%s WHERE numberID=%s;""" % (tableName,)

    updateIndex=1
    self.cursorMySQL.execute(getIndexMySQLQuery)
    for row in self.cursorMySQL:
        indexID = row[0]
        self.cursorMySQL.execute(updateIndexMySQLQuery,(updateIndex,indexID))
        updateIndex+=1

虽然查询'getIndexMySQLQuery'可以使用此语法正常运行,但另一个'updateIndexMySQLQuery'无法正常工作.

While the query 'getIndexMySQLQuery' works fine with this syntax, the other one 'updateIndexMySQLQuery' doesn't work.

任何提示或建议如何解决该问题?

Any hints or suggestion how to get that fixed?

所有评论和建议都将受到高度赞赏.

All comments and suggestions are highly appreciated.

推荐答案

第二个不起作用,因为您在查询字符串中使用了三个占位符,并且仅提供了一个用于插值的变量.

Second one doesn't work, because you are using three placeholders inside the query string and provide only one variable for interpolation.

updateIndexMySQLQuery = """UPDATE %s 
SET numberID=%%s WHERE numberID=%%s;""" % (tableName,)

通过这种方式,字符串格式化机制不希望您提供3个值,因为百分号已转义"(答案的第一个版本令我感到羞耻).

This way the string formatting mechanism doesn't expect you to provide 3 values, as the percent signs are "escaped" (shame on me for the first version of the answer).

这篇关于Python-自动执行MySQL索引:传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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