Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.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
Python 3.x sqlite3.0错误:接近;这句话的意思是:语法错误/参数化sql_Python 3.x_Sqlite - Fatal编程技术网

Python 3.x sqlite3.0错误:接近;这句话的意思是:语法错误/参数化sql

Python 3.x sqlite3.0错误:接近;这句话的意思是:语法错误/参数化sql,python-3.x,sqlite,Python 3.x,Sqlite,我一直收到错误消息: sqlite3.OperationalError: near "The": syntax error 排除故障后,我确定了它是书名中撇号(')的原因。我已经读到我需要参数化sql,但是我在这样做时遇到了麻烦。有关守则: #Connect to database top_ten_db = connect('top_ten.db') #Retrieve view of database db_view = top_ten_db.cursor() #Insert the i

我一直收到错误消息:

sqlite3.OperationalError: near "The": syntax error
排除故障后,我确定了它是书名中撇号(')的原因。我已经读到我需要参数化sql,但是我在这样做时遇到了麻烦。有关守则:

#Connect to database
top_ten_db = connect('top_ten.db')

#Retrieve view of database
db_view = top_ten_db.cursor()

#Insert the information from selected list. Simple loop to do akk the insert commands neatly.
ranking = ['1','2','3','4','5','6','7','8','9','10']
ranking_list_number = 0
name_list_number = 0
description_list_number = 0
for information in range(10):
    db_view.execute('''INSERT INTO top_ten VALUES ("'''+ date[0] +'''","''' + ranking[ranking_list_number] + ''' ","''' + NYT_names[name_list_number] +'''","'''+ description[description_list_number] +'''");''')
    ranking_list_number = ranking_list_number+1
    name_list_number = name_list_number +1
    description_list_number = description_list_number +1

#Commit changes
top_ten_db.commit()
#Close the database
db_view.close()
top_ten_db.close()
请注意,变量“date”、“NYT_name”和“description”都是通过从在线网站获取信息的正则表达式搜索找到的,因此我对输入没有控制权


还应该注意的是,在引入撇号之前,代码运行良好。此外,本书的标题(如果需要)是:钟表匠的女儿

你应该通过将单引号/撇号加倍来避开它们(使用
'
表示文字
'
):

摘自:

字符串常量是通过将字符串括在单引号中形成的 ('). 字符串中的单个引号可以通过放置两个 一行中的单引号-如在Pascal中。使用 不支持反斜杠字符,因为它们不是标准字符 SQL。BLOB文本是包含十六进制数据和 前面有单个“x”或“x”字符。。。可以使用文本值 也可以是标记“NULL”

或者,可以使用绑定参数:

db_view.execute('INSERT INTO top_ten VALUES (?, ?, ?, ?)', (date[0], ranking[ranking_list_number], NYT_names[name_list_number], description[description_list_number]))

当运行您提供的代码时,我现在得到了以下错误AttributeError:“int”对象没有属性“replace”,只是修复了输入错误。请现在再试一次。不,不要那样做。将值绑定到查询中的占位符,而不是尝试将它们直接嵌入字符串中。这样做可以避免wierd引用问题、SQL注入攻击等。非常感谢,绑定参数的替代方法起到了作用,我不再收到任何错误。在中有许多如何使用占位符的示例。
db_view.execute('INSERT INTO top_ten VALUES (?, ?, ?, ?)', (date[0], ranking[ranking_list_number], NYT_names[name_list_number], description[description_list_number]))