TypeError: unsupported operand type(s) for %: 'tuple' and 'dict'
的
variable = '12345'
如何:“”插入%(变量)(id)VALUES(%s)“”“
idunidad = 2501
sql = ("""insert into %(variable)s ( id ) VALUES (%s)""")
if cursor.execute(sql, (idunidad,) % {table_name: table_name}):
db.commit()
cursor.close()
db.close()
问题是代码将变量名称严格地视为文本,而不是指定变量的名称。
想法是在表之间找到变量'12345'的名称,如果没有创建具有变量名称的表:
else:
cursor.execute('''create table variable stocks (id)''')
cursor.execute(sql,(id,))
db.commit()
cursor.close()
db.close()
答案 0 :(得分:0)
variable = '12345'
idunidad = 2501
sql = ("""insert into u_%(variable)s ( id ) VALUES (%(idunidad)s)""" % dict(table_name = table_name))
try:
cursor.execute(sql):
db.commit()
cursor.close()
db.close()
except:
cursor.execute('''create table u_%(variable)s stocks (id)''' % dict(table_name = table_name))
cursor.execute(sql)
db.commit()
cursor.close()
db.close()