简体   繁体   中英

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

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

看起来您可能忘记提交更改。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM