2017-08-31 3 views
0

PythonのMySql DBを使用してテーブルの値を更新しようとしていますが、このエラーが発生しています。 TypeError: query() argument 1 must be a string or read-only buffer, not tuple。 そして私は私の答えが間違っているのは間違っています。TypeError:query()引数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でQuery1を

答えて

0

で逃したようだ%オペレータがこのエラーを取得しています。どのstr

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

、クエリが正常に動作している –

+0

@Abi Waqasが更新でそれを参照してください、 '%'演算子はquery1' 'で逃しているようです回答 – Rajez

0

に変数をバインドされた私は.formatをよりよく知っているが、あなたは、Query1を内の文字列置換用の間違ったフォーマットを持っていると思います()。

試してみてください。

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行の文字列のためのトリプル引用符を使用しないでください

  1. を。
  2. 使用形式機能
  3. あなたがオブジェクトのSTRメソッドを呼び出すためにSTRをCALする必要はありません。 形式またははあなたのために行います - 余計なメソッド呼び出しではありません。

だからあなたのコードはこの1つのように考えられます。Query1を中にエラーになって

query1 = "update universities set updatedBy = {} where id={}".format(y['id'], row['id']) 
関連する問題