标签:table .sh pytho http exe har name str 分享图片
文件目录如下
# -*- coding:utf-8 -*-
import xlrd
import MySQLdb
# Open the workbook and define the worksheet
book = xlrd.open_workbook("3.xls")
sheet = book.sheet_by_name("Table1")
#建立一个MySQL连接
database = MySQLdb.connect (host="", user = "", passwd = "", db = "", port = 3306, charset=‘utf8‘)
# 获得游标对象, 用于逐行遍历数据库数据
cursor = database.cursor()
# 创建插入SQL语句
query = """INSERT INTO t_test (id_card) VALUES (%s)"""
# 创建一个for循环迭代读取xls文件每行数据的, 从第二行开始是要跳过标题
for r in range(1, sheet.nrows):
id_card = sheet.cell(r,0).value
values = (id_access,id_user,haspermission,id_operation)
# 执行sql语句
cursor.execute(query, values)
# 关闭游标
cursor.close()
# 提交
database.commit()
# 关闭数据库连接
database.close()
# 打印结果
print ""
print "Done! "
print ""
columns = str(sheet.ncols)
rows = str(sheet.nrows)
print u"我刚导入了 " +columns + u" 列 and " + rows + u" 行数据到MySQL!"
标签:table .sh pytho http exe har name str 分享图片
原文地址:https://www.cnblogs.com/minyi/p/10138854.html