我想更改sqlite表itemNotes
的note
字段中的sometext
(匹配所有大小写组合,如sometext
、SOMETEXT
、SOmeTExt
)。你知道吗
因为sqlite不支持不区分大小写的更新(我已经问了2个问题here和here),所以我使用python
和regex
。你知道吗
但是我得到了这个错误:sqlite3.OperationalError: near "<": syntax error
匹配线的cursor.execute(f'REPLACE
我的代码中没有<
,所以我认为它来自包含html源代码的note
字段。你知道吗
这是我的密码:
keyword ="sometext"
replacement_word="abc"
# load sqlite3
db = sqlite3.connect(path_to_sqlite)
cursor = db.cursor()
# search for all therm
cursor.execute(f'SELECT * FROM itemNotes WHERE note like "%{keyword}%"')
print("\nfetch one:")
# itemID_list = cursor.fetchone()
# pour chacun des result, change
for row in cursor.fetchall():
# print(each)
row_regex = re.compile(re.escape(keyword), re.IGNORECASE)
row_regex_replaced = row_regex.sub(replacement_word, row[2])
rowindex = row[0]
cursor.execute(
f'REPLACE INTO itemNotes (note) VALUES ({row_regex_replaced}) where itemID = {rowindex}')
在查找了“sql注入”之后,我得出了以下结论:
sql = "REPLACE INTO itemNotes (note) VALUES (?) where itemID = (?)"
data = (row_regex_replaced, rowindex,)
cursor.execute(sql, data)
但是现在我得到了这个错误:sqlite3.OperationalError: near "where": syntax error
从sqlite doc:
INSERT
没有WHERE
子句。查询需要写成“常规”插入,系统将“决定”是否替换。类似于INSERT into itemNotes (itemID,note) VALUES (?,?)
。(注意,上面示例中data
列表的顺序需要更改)。你知道吗相关问题 更多 >
编程相关推荐