我有一个SQLite数据库,有四个表,分别命名为餐厅、酒吧、景点和住宿。每个表有3列,分别名为id、name和description。我正在尝试使用JSON文件中的数据填充数据库,该文件如下所示:
{
"restaurants": [
{"id": "ChIJ8xR18JUn5IgRfwJJByM-quU", "name": "Columbia", "description": "Traditional Spanish restaurant, a branch of a long-standing local chain dating back to 1905."},
],
"bars": [
{"id": "ChIJ8aLBaJYn5IgR60p2CS_RHIw", "name": "Harrys", "description": "Chain outpost serving up Creole dishes in a leafy courtyard or on a balcony overlooking the bay."},
],
"attractions": [
{"id": "ChIJvRwErpUn5IgRaFNPl9Lv0eY", "name": "Flagler", "description": "Flagler College was founded in 1968. Formerly one of Henry Flagler's hotels, the college is allegedly home to many spirits. Tours are offered"},
],
"lodging": [
{"id": "ChIJz8NmD5Yn5IgRfgnWL-djaSM", "name": "Hemingway", "description": "Cottage-style B&B offering a gourmet breakfast & 6 rooms with private baths & traditional decor."},
]
}
每当脚本试图执行查询时,我得到sqlite3.OperationalError: near "x": syntax error
,其中x是描述中的一个随机词。一个示例错误如下:sqlite3.OperationalError: near "Spanish": syntax error
。这个词并不总是西班牙语,但它总是来自其中一种描述。
我尝试过几种不同的方法,但总是得到相同的结果,以下是我尝试过的一种方法:
^{pr2}$我还试着写下这样的问题:
query = 'INSERT OR IGNORE INTO {} VALUES ({}, {}, {})'\
.format(table, detail['id'], detail['name'], detail['description'])
您当前的问题是查询中字符串值的缺少引号。在
您需要正确地参数化查询,让数据库驱动程序担心类型转换、正确地添加引号和转义参数:
注意,table name cannot be parameterized-我们必须使用字符串格式将其插入到查询中-确保表名来自您信任的源或/并正确验证它。在
相关问题 更多 >
编程相关推荐