本章主要分享的是將excel存放的資料匯入到mysql存放。
1.安裝python3
2.安裝庫
pip install pymysql==1.0.2
pip install xlrd==1.2.0
excel資料匯入mysql.py
# _*_ coding:utf-8 _*_
import pymysql, xlrd, os
class suncksql():
def __init__(self, host, user, passwd, dbname='', charset='utf8'):
self.host = host
self.user = user
self.passwd = passwd
self.dbname = dbname
self.charset = charset
def connet(self):
self.db = pymysql.connect(host=self.host, user=self.user, passwd=self.passwd, db=self.dbname,
charset=self.charset) # 連線資料庫
self.cursor = self.db.cursor() # 獲取操作游標
def close(self):
self.cursor.close() # 釋放游標
self.db.close() # 關閉資料庫連線
# 查詢
def get_all(self, sql):
res = none
try:
self.connet()
self.cursor.execute(sql) # 執行sql語句
res = self.cursor.fetchall() # 返回查詢所有結果
except exception as e:
print('查詢失敗:%s' % e)
finally:
self.close()
return res
# 增加、刪除、修改
def shell_sql(self, sql):
"執行sql語句"
# print(sql)
count = 0
try:
self.connet()
count = self.cursor.execute(sql) # 執行sql語句
self.db.commit() # 提交
except exception as e:
print('事務提交失敗:%s' % e)
self.db.rollback() # 如果提交失敗,回滾到上一次資料
finally:
self.close()
return count
def get_xlsx(excel_path, sheet):
"獲取指定excel資料"
file = xlrd.open_workbook(excel_path) # 開啟excel
list =
sheet = file.sheet_by_name(sheet) # 獲得指定sheet資料
row_value1 = sheet.row_values(0) # 獲取第1行的標題
nrows = sheet.nrows # 獲取當前sheet行數
ncols = sheet.ncols # 獲取當前sheet列數
for i in range(1, nrows): # 從第2行遍歷當前sheet
row = sheet.row_values(i) # 獲取行資料
dict = {} # 建立空字典
for j in range(0, ncols): # 遍歷sheet列,組成字典
if row_value1[j] == '序號':
dict[row_value1[j]] = int(row[j])
else:
dict[row_value1[j]] = row[j] # 從第一列開始,將每一列的資料與第1行的資料組成乙個鍵值對,形成字典
return list
def get_sheet_name(excel_path):
file = xlrd.open_workbook(excel_path) # 開啟excel
sheets_name = file.sheet_names()
return sheets_name
def create_db(ip, name, passwd, db_name):
db = suncksql(ip, name, passwd)
db.shell_sql("drop database if exists %s;" % db_name) # 刪除資料庫
db.shell_sql("create database if not exists %s charset utf8;" % db_name) # 建立資料庫
def excel_db1(ip, name, passwd, db_name, db_table, data):
"""建立資料表並插入資料"""
my = suncksql(ip, name, passwd, db_name)
my.shell_sql(
"create table if not exists %s (序號 int (16) not null,題目 varchar(8192),選項a varchar (2048),"
"選項b varchar(2048), 選項c varchar(2048),選項d varchar(2048),答案 varchar(32))" % db_table) # 建立資料表
for i in data:
my.shell_sql(
"insert into %s (序號,題目,選項a,選項b,選項c,選項d,答案) values (%d,'%s','%s','%s','%s','%s','%s')" % (
db_table, i['序號'], i['題目'], i['選項a'], i['選項b'], i['選項c'], i['選項d'], i['答案'])) # 向資料表插入資料
def excel_db2(ip, name, passwd, db_name, db_table, data):
"""建立資料表並插入資料"""
my = suncksql(ip, name, passwd, db_name)
my.shell_sql(
"create table if not exists %s (序號 int (16) not null,題目 varchar(8192),答案 varchar(8192))" % db_table) # 建立資料表
for i in data:
my.shell_sql(
"insert into %s (序號,題目,答案) values (%d,'%s','%s')" % (db_table, i['序號'], i['題目'], i['答案'])) # 向資料表插入資料
def main(ip, name, passwd, db_name):
excel_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), '軟體測試試題庫.xlsx') # 獲取用例檔案路徑
create_db(ip, name, passwd, db_name) # 建立資料庫
sheet_name = get_sheet_name(excel_path) # 獲取excel中sheet名稱
for i in range(0, len(sheet_name)):
db_list = get_xlsx(excel_path, sheet_name[i]) # 獲取sheet測試用例
if i < 4:
excel_db1(ip, name, passwd, db_name, sheet_name[i], db_list) # excel資料匯入mysql
print('excel資料匯入mysql完成:%s' % sheet_name[i])
else:
excel_db2(ip, name, passwd, db_name, sheet_name[i], db_list) # excel資料匯入mysql
print('excel資料匯入mysql完成:%s' % sheet_name[i])
if __name__ == '__main__':
main(ip='資料庫ip位址', name='mysql賬號', passwd='mysql密碼', db_name='軟體測試試題庫')
將Excel資料匯入MySql
將excel資料匯入mysql 1.將選中的資料快兒拷貝到乙個txt文字檔案中 記得把後面的空格消掉。假如存到 d data.txt 這個位置裡。2.根據要匯入的資料快兒建立mysql資料庫和表,然後進入命令提示符裡使用命令 load data local infile d data.txt int...
將Excel資料匯入MySql
1.將選中的資料快兒拷貝到乙個txt文字檔案中 記得把後面的空格消掉。否則匯入資料庫後會有對應的空行 假如存到 d data.txt 這個位置裡。2.根據要匯入的資料快兒建立mysql資料庫和表,然後進入命令提示符裡使用命令 load data local infile d data.txt int...
將Excel資料匯入MySql
1.將選中的資料快兒拷貝到乙個txt文字檔案中 記得把後面的空格消掉。假如存到 d data.txt 這個位置裡。2.根據要匯入的資料快兒建立mysql資料庫和表,然後進入命令提示符裡使用命令 load data local infile d data.txt into table exceltom...