Why does pymysql prompt success after executing the SQL statement, but does not actually operate the database?

 

the reason:

  When pymysql connects to the database, there will be a parameter autocommit, which indicates whether the SQL statement is automatically submitted to the real database after execution. The default is False, and it is not automatically submitted, so the prompt is successful after the SQL statement is executed, but the database is not actually operated.

 

solve:

  Method 1: Set autocommit = True when creating the database connection object

1 message = {
2             "host":host,
3             "user":user,
4             "password":db_pwd,
5             "database":db_name,
6             "autocommit":True
7             }
8 db = pymysql.connect(**message)
9 return db

  

  Method 2: After each execution of SQL, display the submission

1 sql = " insert into users (id, name, age) values ​​(1, 'Amy', 13) " 
2 cursor.execute (sql)        # cursor is the cursor 
3 db.commit ()       # db is the database connection object

 

Guess you like

Origin www.cnblogs.com/chunqiu666/p/12750135.html