各位高手好:在嘗試著把之前自己的工作紀錄導進本機建立的Mysql 數據庫中網上找到了某大神的代碼:?如連接:?https://www.cnblogs.com/longbigbeard/p/9309180.html運行代碼后顯示運行成功,如圖:然而,當在MySQL 表格中復查的時候,發(fā)現(xiàn)數據庫表格為空值(如圖)要導入的excel 表格截圖如下:(好遺憾,似乎慕課網不能添加excel作為附件)仿寫代碼如下:高手們知道問題出在那里么?import?xlrd
import?pymysql
import?xlrd
#?打開excel----------------------------------------------------------
def?open_excel():
????try:
????????book?=?xlrd.open_workbook('C:/Users/lenovo/Desktop/工作紀錄就靠它了.xlsx')??#文件名,把文件與py文件放在同一目錄下
????except:
????????print("open?excel?file?failed!")
????try:
????????sheet?=?book.sheet_by_name('ANZ紀錄')???#execl里面的worksheet1
????????return?sheet
????except:
????????print("locate?worksheet?in?excel?failed!")
#?驗證:open_excel?是跑的通的
#-----------------------------------------------------------------
#連接數據庫-------------------------------------------------------
try:
????db?=?pymysql.connect(host='localhost',user="root",
????????passwd='QAZwsx12345678',
????????db='test',
????????charset='utf8')
except:
????print("could?not?connect?to?mysql?server")
#?驗證:本機數據庫連接?是跑的通的
#----------------------------------------------------------------
def?search_count():
????cursor?=?db.cursor()
????select?=?"select?count(PN)?from?pn_infor"?#獲取表中xxxxx記錄數
????cursor.execute(select)?#執(zhí)行sql語句
????line_count?=?cursor.fetchone()
????print(line_count[0])
#驗證:search_count?是可以跑通的
#-----------------------------------------------------------------
def?insert_deta():
????sheet?=?open_excel()
????cursor?=?db.cursor()
????for?i?in?range(5,?sheet.nrows):?#前5行是標題名,對應表中的字段名所以應該從第6行開始,計算機以0開始計數,所以值是6
????????inidate?=?sheet.cell(i,0).value?#取第i行第0列
????????initiation_date?=?xlrd.xldate_as_datetime(inidate,0)?#?把數字型的日期解碼成真正的日期
????????requestor?=?sheet.cell(i,1).value#取第i行第1列,下面依次類推
????????EB??=?sheet.cell(i,2).value
????????EOL_By?=?xlrd.xldate_as_datetime(EB,0)?#?把數字型的日期解碼成真正的日期
????????customer_name?=?sheet.cell(i,3).value
????????Type_of_Service?=?sheet.cell(i,4).value
????????PN?=?sheet.cell(i,5).value
????????description30?=?sheet.cell(i,6).value
????????description80?=?sheet.cell(i,7).value
????????Enable?=?sheet.cell(i,8).value
????????CostUSD?=?sheet.cell(i,9).value
????????CostAUD?=?sheet.cell(i,10).value
????????ListpriceAUD?=?sheet.cell(i,11).value
????????DistipriceAUD?=?sheet.cell(i,12).value
????????print(initiation_date)
????????print(requestor)
????????print(EOL_By)
????????print(customer_name)
????????print(Type_of_Service)
????????print(PN)
????????print(description30)
????????print(description80)
????????print(Enable)
????????print(CostUSD)
????????print(CostAUD)
????????print(ListpriceAUD)
????????print(DistipriceAUD)
????????print("---------------")
????????value?=?(initiation_date,requestor,EOL_By,customer_name,Type_of_Service,PN,description30,description80,Enable,CostUSD,CostAUD,ListpriceAUD,DistipriceAUD)
????????print(value)
????????sql?=?"INSERT?INTO?pn_infor(initiation_date,requestor,EOL_By,customer_name,Type_of_Service,PN,description30,description80,Enable,CostUSD,CostAUD,ListpriceAUD,DistipriceAUD)VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
????????cursor.execute(sql,value)?#執(zhí)行sql語句
????????db.commit()
????cursor.close()?#關閉連接
insert_deta()
db.close()#關閉數據
print?("ok?")
添加回答
舉報
0/150
提交
取消