Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

inserting data using mysql Connector in python

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

like image 232
Hamoudaq Avatar asked Feb 21 '13 22:02

Hamoudaq


2 Answers

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

like image 175
Alex Troush Avatar answered Oct 06 '22 12:10

Alex Troush


It looks like you're probably forgetting to commit your changes.

like image 41
David Marx Avatar answered Oct 06 '22 12:10

David Marx