当前位置:   article > 正文

PyMySQL 更新语句之 1064错误及解决方案_pymysql 1064 update ‘

pymysql 1064 update ‘
''' 
更新语句通常写法:
	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
                                   ]
                                   )
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 运行结果提示:
修改数据时的异常:  (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")
  • 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))
                    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/508938
推荐阅读
相关标签
  

闽ICP备14008679号