如何处理Python执行SQL语句时参数包含单引号的问题?
- 内容介绍
- 文章标签
- 相关推荐
本文共计624个文字,预计阅读时间需要3分钟。
在编写程序时,需要实现将数据导入数据库,并支持带参数的递归。执行语句如下:
sql_str=INSERT INTO teacher(t_name, t_info, t_phone, t_email) VALUES ('%s', '%s', '%s', '%s')sql_str +=%s (%s)
在编写自己的程序时,需要实现将数据导入数据库,并且是带参数的传递。
执行语句如下:
sql_str = "INSERT INTO teacher(t_name, t_info, t_phone, t_email) VALUES\ (\'%s\', \'%s\', \'%s\', \'%s\')" % (result, result2, phoneNumber, Email) cur.execute(sql_str)
执行程序后,产生错误:
ProgrammingError: (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 '07、PRICAI'08、ACML'09 程序委员会主席/共同主席,多次担任 ACM K' at line 1")
发现是因为result2参数为一个字符串,而字符串中出现了单引号 ',mysql语句受到影响报错。
原本认为这个问题应该会是有标准解决方案,可是网上查询了一下,遇到这个问题的人不少,但没有很好的方法解决。
本文共计624个文字,预计阅读时间需要3分钟。
在编写程序时,需要实现将数据导入数据库,并支持带参数的递归。执行语句如下:
sql_str=INSERT INTO teacher(t_name, t_info, t_phone, t_email) VALUES ('%s', '%s', '%s', '%s')sql_str +=%s (%s)
在编写自己的程序时,需要实现将数据导入数据库,并且是带参数的传递。
执行语句如下:
sql_str = "INSERT INTO teacher(t_name, t_info, t_phone, t_email) VALUES\ (\'%s\', \'%s\', \'%s\', \'%s\')" % (result, result2, phoneNumber, Email) cur.execute(sql_str)
执行程序后,产生错误:
ProgrammingError: (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 '07、PRICAI'08、ACML'09 程序委员会主席/共同主席,多次担任 ACM K' at line 1")
发现是因为result2参数为一个字符串,而字符串中出现了单引号 ',mysql语句受到影响报错。
原本认为这个问题应该会是有标准解决方案,可是网上查询了一下,遇到这个问题的人不少,但没有很好的方法解决。

