在mysql中将值插入表时如何处理值中的换行符
我有一个带有两个varchar/text字段的表,我有两个字段都带有换行符的数据,如何将它们正确插入mysql?目前mysql正在抛出错误: 以下是一个例子:在mysql中将值插入表时如何处理值中的换行符,mysql,python-3.x,Mysql,Python 3.x,我有一个带有两个varchar/text字段的表,我有两个字段都带有换行符的数据,如何将它们正确插入mysql?目前mysql正在抛出错误: 以下是一个例子: insert into usecase(description, comment) value('Use Case : 1 of 65 Lorem ipsum dolor sit amet, in sea, natum's cetero (an per) ', ' Lorem ipsum Consul appetere ') 错误:1
insert into usecase(description, comment) value('Use Case : 1 of 65
Lorem ipsum dolor sit amet,
in sea, natum's cetero (an per)
', '
Lorem ipsum
Consul appetere
')
错误:1064(42000):您的SQL语法有错误;检查
与右边的MySQL服务器版本相对应的手册
使用接近“用例(描述、注释)值”的语法('用例:1/2
65 Lorem ipsum dolor sit amet,“1号线MySQL连接已关闭
[更新]
这是python3程序的一部分
query = f"insert into usecase(description, comment) value(\'{description}\', \'{comment}\')"
print(query)
if (connection.is_connected()):
cursor = connection.cursor()
cursor.execute(query)
connection.commit()
多谢各位
[更新/回答]:
多亏了@Anonymous和@nbk,使用参数化查询是解决方案,下面是工作代码:
query = "insert into usecase(description, comment) value(%s, %s)"
args = description, comment
if (connection.is_connected()):
c1 = connection.cursor()
c1.execute(query, args)
connection.commit()
print("New case inserted.")
为什么要用python标记它?你不应该用python标记表名或字段名。你需要转义你的值。你能显示运行此语句的python代码吗?当然,谢谢,更新了OP。请使用参数化查询…为什么用python标记它?你不应该用python标记表名或字段名。你需要转义你的值。你能吗显示运行此语句的Python代码?当然,谢谢,更新了OP。请使用参数化查询。。。