我正在尝试使用Python MySql DB更新表的值但是收到此错误。
TypeError: query() argument 1 must be a string or read-only buffer, not tuple
。
而且我对我的答案出了什么问题一无所知。
def id_of_unverifedUniversity():
cur3.execute('select id from universities where verified=0 and deleted=0;')
print "===================Unverififed University================"
for row in cur3.fetchall():
#cur3.execute('SELECT id FROM Users where universityId='+str(row['id']))
print row['id']
query = ('SELECT id FROM users where universityId = %s order by id asc limit 1' %(str(row['id'])))
cur3.execute(query)
result = cur3.fetchall()
for y in result:
if y['id']:
print str(y['id'])
print 'update query statred'
query1 = ("""update universities set updatedBy = %s where id=%s""", (str(y['id']),str(row['id'])))
cur3.execute(query1)
我在query1
中收到此错误答案 0 :(得分:0)
在query1
中,似乎错过了运营商%
。哪个将变量绑定到str
query1 = '''update `universities` set `updatedBy` = %s where `id`=%s''' % (str(y['id']),str(row['id']))
答案 1 :(得分:0)
我认为你在query1中对字符串替换有错误的格式,虽然我对.format()比较熟悉。
尝试:
query1 = ("""update universities set updatedBy = {} where id={}""".format(str(y['id']),str(row['id'])))
答案 2 :(得分:0)
问题是你的 query1 是一个元组。
query1 = ("""update universities set updatedBy = %s where id=%s""", (str(y['id']),str(row['id'])))
我在这里有一些评论:
所以你的代码可能就像这样:
query1 = "update universities set updatedBy = {} where id={}".format(y['id'], row['id'])