Hamoudaq
Hamoudaq

Reputation: 1498

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

Upvotes: 9

Views: 8166

Answers (2)

Alex Troush
Alex Troush

Reputation: 677

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

Upvotes: 14

David Marx
David Marx

Reputation: 8558

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

Upvotes: 4

Related Questions