简体   繁体   English

如何解决python错误“在字符串格式化时未转换所有参数”

[英]how to resolve python error “not all arguments converted while string formatting”

When i execute below program 当我执行下面的程序

import MySQLdb
cn = MySQLdb.connect(host="localhost", user="root", passwd="mysqlroot", 
db="sv_data")
cursor = cn.cursor()
cursor.execute("select addressline1, zipcode from sv_address where zipcode = '10011'")
for (addressline1, zipcode) in cursor:
print(addressline1, zipcode)
cursor.close()
cn.close()

it works fine. 它工作正常。 However when i try to add a parameter in query, like below 但是当我尝试在查询中添加参数时,如下所示

import MySQLdb
 cn = MySQLdb.connect(host="localhost", user="root", passwd="****",     
db="sv_data")
cursor = cn.cursor()

a="10011"
cursor.execute("select addressline1, zipcode from sv_address where zipcode = 
%s", (a))
for (addressline1, zipcode) in cursor:
 print(addressline1, zipcode)

cursor.close()
cn.close()

getting error ProgrammingError: not all arguments converted during string formatting 获取错误ProgrammingError:并非所有参数在字符串格式化期间转换

can you please advise how to fix this. 你能告诉我如何解决这个问题。 I tried various options. 我尝试了各种选择。 zipcode is varchar field in mysqldb zipcode是mysqldb中的varchar字段

You are not passing the argument. 你没有通过论证。 you are rather giving it a string. 你宁愿给它一个字符串。

Try this. 尝试这个。

cursor.execute("select addressline1, zipcode from sv_address where zipcode = %s" % a )

The best way to do is to separate your arguments and sql_query 最好的方法是分离你的参数和sql_query

_sql = "select addressline1, zipcode from sv_address where zipcode = {0}"
cursor.execute(_sql.format(a))

I believe the issue is that cursor is reading (a) as just a string and sees multiple values in it that aren't being converted( 1, 0, 0, 1, 1 ). 我认为问题是cursor正在读取(a)只是一个字符串,并在其中看到未被转换的多个值( 1, 0, 0, 1, 1 )。 So if you add a comma like so: 所以如果你这样添加一个逗号:

cursor.execute("select addressline1, zipcode from sv_address where zipcode = %s", (a,))

I believe that will work properly. 我相信这会正常运作。

For instance, look at this function and the outcome: 例如,看看这个函数和结果:

In [28]: def t(arg):
    ...:     print(type(arg))
    ...:     

In [29]: t(('a'))
<class 'str'>

The argument isn't being read as a tuple, it's being read as a string. 该参数不是作为元组读取的,它被读作字符串。

暂无
暂无

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

相关问题 使用列表列表时,在字符串格式化错误期间,Python并非所有参数都已转换 - Python not all arguments converted during string formatting error while working with list of lists 带有Selenium错误TypeError的Python:在字符串格式化期间并非所有参数都已转换 - Python with Selenium error TypeError: not all arguments converted during string formatting MYSQLdb / Python-并非在字符串格式化错误期间转换了所有参数? - MYSQLdb/Python - not all arguments converted during string formatting error? python错误TypeError:并非所有参数都在字符串格式化期间转换 - python error TypeError: not all arguments converted during string formatting Python错误:在字符串格式化期间,并非所有参数都已转换 - Python Error: Not all arguments converted during string formatting TypeError: not all arguments converted during string formatting 错误 python - TypeError: not all arguments converted during string formatting error python Python 错误:类型错误:并非所有参数都在字符串格式化期间转换 - Python Error: TypeError: not all arguments converted during string formatting Python和SQLite:在字符串格式化错误期间,并非所有参数都已转换 - Python and SQLite: Not all arguments converted during string formatting error Python Folium 错误:“并非所有 arguments 在字符串格式化期间都已转换” - Python Folium Error: “not all arguments converted during string formatting” Python错误:在字符串格式化期间,并非所有参数都已转换 - Python error: Not all arguments converted during string formatting
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM