def insert_update(self, word, note=""): try: sql = "insert into words (word,note) values (%s,%s)" self.cursor.execute(sql, (word, note)) print("增加成功") except: # 如果单词已经存在就执行更新 if note != "": try: sql = "update words set note=%s where word=%s" self.cursor.execute(sql, (word, note)) print("更新成功") except Exception as e: print(e)无法插入单词的定义
时间: 2024-01-29 12:02:18 浏览: 93
在这段代码中,如果单词已经存在,执行更新的语句中的参数顺序应该是 `(note, word)` 而不是 `(word, note)`,因为 `note` 应该对应 SQL 语句中的第一个 `%s` 占位符,而 `word` 应该对应第二个。修改代码如下:
```
try:
sql = "insert into words (word,note) values (%s,%s)"
self.cursor.execute(sql, (word, note))
print("增加成功")
except:
# 如果单词已经存在就执行更新
if note != "":
try:
sql = "update words set note=%s where word=%s"
self.cursor.execute(sql, (note, word))
print("更新成功")
except Exception as e:
print(e)
```
阅读全文