程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

python---mysql batch insert data

編輯:Python

文章目錄

  • 一、批量插入表數據
  • 一、Bulk insert operation steps

一、批量插入表數據

說明:
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()

一、Bulk insert operation steps

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


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved