赞
踩
''' 更新语句通常写法: update 表名 set 列名 = new_value where 列名 = value; 动态占位符写法: update user_id set %s = %s where %s = %s; ''' alter_self_info = 'update user_id set %s = %s where %s = %s' print(alter_self_info) cursor.execute(query=alter_self_info, args=[ set_column_name, new_value, column_name, where_column_name ] )
修改数据时的异常: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''id' = '123456' where 'id' = '123453'' at line 1")
syntax to use near ''id'
莫名奇妙的多了个单引号
解决方案:
# 根据输入完成更新操作
set_column_name = input('请输入字段名[set id =""]: ') #
new_value = input('请输入修改的新值: ')
column_name = input('请输入字段名[set id =""]: ')
where_column_name = input('请输入依据的条件字段名[where ="123456"]: ')
# 这句解决上面的 1046 错误,最后正确更新
cursor.execute('update user_id set %s = %s where %s = %s '%(str(set_column_name),new_value,column_name,where_column_name))
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。