关于python连接sql数据库--使用executemany方法插入多条数据
from openpyxl import load_workbook
import pymssql
#对excel进行读取
path = r"C:/Users/xxxx/Desktop/xxxx.xlsx"
wb = load_workbook(path)
sheet = wb.active
#对于每行数据进行元组化进列表
articledata = []
for row in sheet.rows:
record = []
for cell in row:
record.append(cell.value)
articledata.append(tuple(record))
# 连接数据库
server = 'xxxxx'
port = xxxx
user = 'xx'
password = 'xxxxxx'
database = 'xxxxx'
conn = pymssql.connect(host=server,user=user,password=password,database=database,port=port)
cursor = conn.cursor()
# executemany方法多条数据插入
cursor.executemany(
"INSERT INTO 表名(列名1,列名2,列名3)VALUES(%s,%s,%s)",articledata)
conn.close()
#很方便的连接方法与导入方法,遇到导入问题欢迎去我的博客看关于导入一块的所有问题呀!!点个赞再走吧,求求啦

浙公网安备 33010602011771号