在python中使用mysql Connector插入数据

时间:2020-12-31 17:08:28

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

数据库测试只包含一个用户表,包括3个字段,即id,用户名和密码,用户名是唯一的,id是A_I

note i've used this query also ! :

请注意我也使用过此查询! :

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

“”插入用户(用户名,密码)值('userName','passWord');“”“

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

尝试了很多方法,但没有发生任何事情(没有插入,也没有例外!)

2 个解决方案

#1


13  

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.

您可能忘记使用con.commit,将更改提交到数据库。

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

不要对每个INSERT / UPDATE操作使用con.commit(),将其用于操作组,该逻辑按SQL规则组合

#2


4  

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

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

#1


13  

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.

您可能忘记使用con.commit,将更改提交到数据库。

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

不要对每个INSERT / UPDATE操作使用con.commit(),将其用于操作组,该逻辑按SQL规则组合

#2


4  

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

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