有没有办法用 python 字典更新数据库 table?
Is there a way to update a database table with a python dictionary?
我正在尝试将数据写回到数据库的列中 table。
数据存储在 python 字典中,因为我需要键来标识我要更新的行。
这是我目前的代码片段:
l = []
for line in text:
soup = bs(line, 'html.parser')
for script in soup(["script", "style"]):
script.extract()
autor = soup.get_text()
s = autor.replace('\n', '')
l.append(s)
dictW = dict(zip(t,l))
sql = 'UPDATE tablename SET Reintext WHERE = ...'
cursor.executemany(sql, dictW)
dictW 看起来像这样:
{'file:/C:/Users/125.html' : 'long string of a html file'}
我卡在 UPDATE 语句上了。
如何编写它以在使用键作为标识符的同时使用字典中的值更新数据库table?
for key in dictW:
x = dictW[key]
print(key, x)
此外观分别为您提供键和值,因此您可以在 SQL 语句中使用它们。这将遍历您的字典,以便您可以分别为您的 SQL 语句
获取键和值列表
sql = 'UPDATE tablename SET Reintext WHERE ? = ? '
cursor.executemany(sql, (key, x))
我正在尝试将数据写回到数据库的列中 table。
数据存储在 python 字典中,因为我需要键来标识我要更新的行。
这是我目前的代码片段:
l = []
for line in text:
soup = bs(line, 'html.parser')
for script in soup(["script", "style"]):
script.extract()
autor = soup.get_text()
s = autor.replace('\n', '')
l.append(s)
dictW = dict(zip(t,l))
sql = 'UPDATE tablename SET Reintext WHERE = ...'
cursor.executemany(sql, dictW)
dictW 看起来像这样:
{'file:/C:/Users/125.html' : 'long string of a html file'}
我卡在 UPDATE 语句上了。
如何编写它以在使用键作为标识符的同时使用字典中的值更新数据库table?
for key in dictW:
x = dictW[key]
print(key, x)
此外观分别为您提供键和值,因此您可以在 SQL 语句中使用它们。这将遍历您的字典,以便您可以分别为您的 SQL 语句
获取键和值列表sql = 'UPDATE tablename SET Reintext WHERE ? = ? '
cursor.executemany(sql, (key, x))