inserting data using mysql Connector in python

徘徊边缘 提交于 2019-12-08 17:18:53

问题


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


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/15013597/inserting-data-using-mysql-connector-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!