Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
查询SQLite3时Python中的字符串格式化_Python_String_Sqlite - Fatal编程技术网

查询SQLite3时Python中的字符串格式化

查询SQLite3时Python中的字符串格式化,python,string,sqlite,Python,String,Sqlite,我正在用Python中的SQlite3创建一个API应用程序。在我处理名称列中带有“”的项之前,一切都正常。条目将写入数据库,但当我尝试使用以下命令从数据库获取该条目时,代码将中断: received_data={“name”:“带有‘name’的示例”,“price”:43,…} new_item=cursor.execute(“SELECT*FROM items,其中name='{}'.format(接收的_数据['name'])).fetchall() 我得到这个错误: new_ite

我正在用Python中的SQlite3创建一个API应用程序。在我处理名称列中带有“”的项之前,一切都正常。条目将写入数据库,但当我尝试使用以下命令从数据库获取该条目时,代码将中断:

received_data={“name”:“带有‘name’的示例”,“price”:43,…}
new_item=cursor.execute(“SELECT*FROM items,其中name='{}'.format(接收的_数据['name'])).fetchall()
我得到这个错误:

new_item=cursor.execute(“SELECT*FROM items,其中name='{}'.format(接收的_数据['name'])).fetchall()
sqlite3.error:靠近“名称”:语法错误

既然我猜不出(我猜…)某人可能会在名字中使用的所有字符,那么最好的解决办法是什么?

不要使用
格式,因为这样做很危险。使用参数化查询:

new_item = cursor.execute("SELECT * FROM items WHERE name=?", (received_data['name'],)).fetchall()