說明:
The following example is to insert three pieces of data in batches,how much to insert,可以自己定義
dbinfo = {
"host": "192.168.90.0",
"user": "root",
"password": "XXX",
"port": 3306,
}
name_list = ["hi", "hello", "hey"]
address_list = ["10", "20", "30"]
mobile_list = ["10", "20", "30"]
val_list = [[name_list[i], address_list[i], mobile_list[i]] for i in range(len(name_list))]
print(val_list)
ins_sql = "INSERT INTO {}(name,address,mobile)VALUES ({}, {},{});"
sql = ins_sql.format(table_name,'%s','%s','%s')
cursor = dbinfo.cursor()
cursor.executemany(sql,val_list)
db.commit()
Fields that need to be bulk inserted:
[[name_list[i], address_list[i], mobile_list[i]] for i in range(len(name_list))]
有三個字段,分別是:
name_list、address_list、mobile_list
Take each subscript value from these three fields
name_list[i], address_list[i], mobile_list[i]
Loop in this interval:
len(name_list)
執行sql:
INSERT INTO {}(name,address,mobile)VALUES ({}, {},{});
Connect to the database to execute the statement,提交:
cursor.executemany(sql,val_list)
db.commit()