inserting data using mysql Connector in python

Hamoudaq picture Hamoudaq · Feb 21, 2013 · Viewed 7.6k times · Source

i've faced an wired problem that's when i'am about to insert data into my database it's not inserting and not failing (throwing exception) either ! ,, when data is duplicated or wrong table provided it's throws exceptions !!

here's my code !

from mysql import connector
con = connector.Connect(user='root',password='root',database='test',host='localhost')
cur=con.cursor()
cur.execute("""insert into user values ('userName', 'passWord')""") 

the database test include only one table which is users and include 3 fields which is id and username and password , username is unique and id is A_I

note i've used this query also ! :

"""insert into user (username,password) values ('userName', 'passWord');"""

and tried so many ways , but nothing happend (not inserted and no exceptions throwed either !)

Answer

Alex Troush picture Alex Troush · Feb 21, 2013
from mysql import connector
con = connector.Connect(user='root',password='root',database='test',host='localhost')
cur=con.cursor()
cur.execute("""insert into user values ('userName', 'passWord')""")
**con.commit()**
con.close()

You probably forget to use con.commit, to commit your changes to database.

Don't use con.commit() for for each INSERT/UPDATE operation, use it for group of operations, that logical combines by SQL rules