Eman S.
Eman S.

Reputation: 145

Sqlite3 INSERT query ERROR?

i want to insert in my DB 2 strings var

one entered from the user ==> H and

one generated form the chatbot==> B

Here is the code:

# 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)

The codes works perfectly in generating and selecting the replay from the DB , but the problem is when i go back to the chatting_log table in the DB there is no data?

PS: i am using python 3

thank you,

Upvotes: 2

Views: 173

Answers (1)

David
David

Reputation: 125

Always remember to commit the changes that you make. In this case: connection.commit(). Except it looks like you overrode your connection variable.

Upvotes: 2

Related Questions