2017-08-31 36 views
0

我想使用Python MySql数据库更新表的值,但得到此错误。 TypeError: query() argument 1 must be a string or read-only buffer, not tuple。 我无能为力,我的答案出了什么问题。TypeError:查询()参数1必须是字符串或只读缓冲区,而不是元组

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

query1收到此错误操作%似乎错过了。这是变量绑定到str

query1 = '''update `universities` set `updatedBy` = %s where `id`=%s''' % (str(y['id']),str(row['id'])) 
+0

得到错误,查询工作正常 –

+0

@Abi Waqas似乎''%运营商在'query1'错过,看到它的更新回答 – Rajez

0

我认为你有字符串替换QUERY1错误的格式,虽然我更熟悉.format()。

尝试:

query1 = ("""update universities set updatedBy = {} where id={}""".format(str(y['id']),str(row['id']))) 
0

的问题是,你的QUERY1是一个元组。

query1 = ("""update universities set updatedBy = %s where id=%s""", (str(y['id']),str(row['id']))) 

我有一些评论对您的位置:

  1. 不要使用三引号的单行线。
  2. 使用格式功能
  3. 您不需要CAL STR调用海峡你的对象的方法。 格式%s会为你做 - 不会是多余的方法调用。

所以,你的代码可能是像这样的:在QUERY1

query1 = "update universities set updatedBy = {} where id={}".format(y['id'], row['id']) 
相关问题