0
Follow
0
View

There is no change in the database after Python inserts variables into the database

dqszstrain 注册会员
2023-02-25 18:14

the reference answer GPT ᴼ ᴾ ᴱ ᴺ ᴬ ᴵ < br / > you need to pass the SQL statement to execute() method, and the statement of the placeholder substitution for you to insert value.

Here is an example of the modified code:

import pymysql

a = (('666', '666', '666', '666'), ('666', '666', '666', '666'))

mydb = pymysql.connect(host="localhost", user="root", passwd="", database='123')
mycursor = mydb.cursor()

sql = "INSERT INTO 数据 VALUES (%s, %s, %s, %s)"
mycursor.executemany(sql, a)

mydb.commit()
print(mycursor.rowcount, "was inserted.")
mydb.close()


In the above code, we use the %s placeholder to represent the value to be inserted, then replace the placeholder with the value in the a variable in the executemany() method.

It is also recommended that you close the cursor and database connection after inserting the data.

About the Author

Question Info

Publish Time
2023-02-25 18:14
Update Time
2023-02-25 18:14