Python 3.x 在python3中,向查询字符串传递多个字符串变量的最佳方法是什么
我需要基于两个字符串参数创建一个动态查询:Python 3.x 在python3中,向查询字符串传递多个字符串变量的最佳方法是什么,python-3.x,string-concatenation,Python 3.x,String Concatenation,我需要基于两个字符串参数创建一个动态查询: description = "This is the description" comment = "This is the comment" query = "insert into case(desc, comm) value(description, comment)" 注: 描述和注释中可能有单引号和双引号 如何使用格式化的%s生成查询字符串 多谢各位 更新: 多亏了绿斗篷人,他/她的答案还有待更正,正确的问题是: query=finsert
description = "This is the description"
comment = "This is the comment"
query = "insert into case(desc, comm) value(description, comment)"
注:
描述和注释中可能有单引号和双引号
如何使用格式化的%s生成查询字符串
多谢各位
更新:
多亏了绿斗篷人,他/她的答案还有待更正,正确的问题是:
query=finsert进入casesdescription,注释值\'{description}\',\'{comment}\'使用f字符串
query = f"insert into case({description}, {comment}) value({description}, {comment})"
不要使用任何类型的字符串格式来执行实际的数据库查询-这会导致SQL注入问题。改为使用数据库库,以正确清理数据
但是,如果您只需要将一些变量解析为字符串,那么这比其他语言通常使用的%格式更灵活,而且从技术上讲,python中仍然可以通过一些字符串%s%s%str1、str2、str3使用%格式,