1

I have connected to database in python successfully but it is not saving the data I provide.Here is the code :

    try:
    connection = MySQLdb.connect(host="localhost",user="root",passwd="",db="traffic_alert")
except Exception as e:
    print('Not connected')


   cursor = connection.cursor()
cursor.execute("insert into data (main_status,extracted_info,time) values (%s,%s,%s)",[sentence,final_result,time])
print(sentence)
print(final_result)
print(time)

it can successfully print sentence, final_result and time but not saving them in database.sentence,final_result and time are less than 50 characters.I have created the database manually in mysql with these rows :

id -> auto_increment,pk
main_status -> varchar(255)
extracted_info -> varchar(255)
seen  -> int(10),Null=True
time ->varchar(255)
2
  • I presume copy/paste or formatting error, but your opening try indentation is off. Also, any error messages either on python or db side perhaps? Commented Jun 10, 2018 at 15:20
  • connection.commit() is the solution. Then it saved in database. Commented Jun 12, 2018 at 10:04

1 Answer 1

6
connection.commit()

After cursor.execute

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.