python - Sqlite3 INSERT查询错误?

标签 python mysql python-3.x

我想在我的 DB 2 字符串中插入 var

用户输入的一个 ==> H 和

由聊天机器人生成的一个==> B

这是代码:

# initialize the connection to the database
sqlite_file = '/Users/emansaad/Desktop/chatbot1/brain.sqlite'
connection = sqlite3.connect(sqlite_file)
connection.create_function("REGEXP", 2, regexp)
cursor = connection.cursor()
connection.text_factory = str
connection = open

def new_data(Input,Output):
     row = cursor.execute('SELECT * FROM chatting_log WHERE user=?',(Input,)).fetchone()
     if row:
        return
     else:
        cursor.execute('INSERT INTO chatting_log VALUES (?, ?)', (Input, Output))

while True:
   print(("B:%s" % B))  
   H = input('H:')
   New_H= ' '.join(PreProcess_text(H))
   reply= cursor.execute('SELECT respoce FROM Conversation_Engine WHERE request REGEXP?',[New_H]).fetchone()
   if reply:
      B=reply[0]
      new_data(H,B)

代码在从数据库生成和选择重播方面工作得很好,但问题是当我返回到数据库中的chatting_log表时没有数据?

PS:我使用的是 python 3

谢谢,

最佳答案

始终记住提交您所做的更改。在本例中:connection.commit()。只是看起来您覆盖了 connection 变量。

关于python - Sqlite3 INSERT查询错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40047742/

相关文章:

python - 如何将所有模块移动到新版本的 Python(从 3.6 到 3.7)

python - LabelEncoder 未将字符串转换为数字 (0,1,2)

Mysql建表报错整数

mysql - 如果启用了数据库自动提交模式,我为什么要关心 transaction.on_commit()?

Python:使用列表理解生成几何级数

python-3.x - 绘制 pandas 数据框的 x 和 y 列,第三列值确定点的形状

python - Eclipse 调试上的变量查看器截断字符串值

python - "__method__"和 "method"之间的区别

python - 通过 Regex 在 Python 中匹配 "..."= "..."

mysql - 如何获得在数据库表中搜索字符串的正确语法?