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
SQLite3中的Python操作错误_Python_Sqlite - Fatal编程技术网

SQLite3中的Python操作错误

SQLite3中的Python操作错误,python,sqlite,Python,Sqlite,在DB中插入时,出现以下错误: sqlite3.OperationalError: near "word": syntax error 代码: 此字符串是“name”中的“word” 如何修复它?您所犯的主要错误是没有将文字包含在引号中 con = sqlite3.connect('films.db') cur = con.cursor() cur.execute('CREATE TABLE films (id INTEGER PRIMARY KEY, name VARCHAR(100), '

在DB中插入时,出现以下错误:

sqlite3.OperationalError: near "word": syntax error
代码:

此字符串是“name”中的“word”


如何修复它?

您所犯的主要错误是没有将文字包含在引号中

con = sqlite3.connect('films.db')
cur = con.cursor()
cur.execute('CREATE TABLE films (id INTEGER PRIMARY KEY, name VARCHAR(100), ' +
            'img BLOB, imbd VARCHAR(30), country_year VARCHAR(50))')
con.commit()

for i in range(97):
    cur.execute('INSERT INTO films (name, img, imbd) VALUES("' + names[i].text_content() + '", "' + str(urllib.request.urlopen(img[i].get('src')).read()) + '", "' + imbd[i].text_content() + '" )')
    con.commit()
    print(cur.lastrowid)
但是如果你这样做,你就犯了地球上最大的错误。insert语句应改为

cur.execute('INSERT INTO films (name, img, imbd) VALUES(?,?,?)', (names[i].text_content(), str(urllib.request.urlopen(img[i].get('src')).read()), imbd[i].text_content()))
否则,您将无法接受SQL语法。阅读说明,了解为什么必须使用
而不是串联

最后看看这张图片,试着理解出了什么问题。

cur.execute('INSERT INTO films (name, img, imbd) VALUES(?,?,?)', (names[i].text_content(), str(urllib.request.urlopen(img[i].get('src')).read()), imbd[i].text_content()))