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 sqlite3模块时出现操作错误_Python_Sqlite - Fatal编程技术网

使用Python sqlite3模块时出现操作错误

使用Python sqlite3模块时出现操作错误,python,sqlite,Python,Sqlite,inventoryCurs.execute(''更新表集状态='缺货',其中id=%s'),i[0]) 操作错误:靠近“%”:语法错误 如果看不到更多的代码,就很难完全解决问题,但是看看您的代码,我认为问题可能是这行中的%s: def StatusUpdate(self, table): inventoryCurs.execute('SELECT * from Table') for i in inventoryCurs: html = urlopen(i[5])

inventoryCurs.execute(''更新表集状态='缺货',其中id=%s'),i[0]) 操作错误:靠近“%”:语法错误


如果看不到更多的代码,就很难完全解决问题,但是看看您的代码,我认为问题可能是这行中的
%s

def StatusUpdate(self, table):
    inventoryCurs.execute('SELECT * from Table')
    for i in inventoryCurs:
        html = urlopen(i[5]).read()
        Soup = BeautifulSoup(html)

        if table.StockStatus(Soup) == 'Out of Stock':        
            inventoryCurs.execute('''UPDATE table SET status = 'Out of Stock' WHERE id = %s)''', i[0])
根据和中SQLite模块的文档,
sqlite3
模块需要一个
作为占位符,而不是
%s
或其他格式字符串

根据,可以像这样使用
%s
占位符:

inventoryCurs.execute('''UPDATE table SET status = 'Out of Stock' WHERE id = %s)''', i[0])
但这是一个简单的格式字符串,实际上不是数据库的占位符。此外,正如注释所示,您不应该以这种方式构建查询,因为这会使它们容易受到SQL注入的攻击。相反,您应该像这样构建它们,使用

import sqlite3
conn = sqlite3.connect('example.db')
c = conn.cursor()
# Never do this -- insecure!
symbol = 'IBM'
c.execute("select * from stocks where symbol = '%s'" % symbol)
文档中有更多的细节,但我相信这就是您发布的错误的解决方案

import sqlite3
conn = sqlite3.connect('example.db')
c = conn.cursor()
# Do this instead
t = (symbol,)
c.execute('SELECT * FROM stocks WHERE symbol=?', t)