python - pymysql報(bào)錯(cuò)1146 xls表格導(dǎo)入數(shù)據(jù)庫(kù)失敗 不知道問(wèn)題出在哪
問(wèn)題描述
Python 4.3.0定義了一個(gè)類(lèi)用來(lái)操作本地的數(shù)據(jù)庫(kù),然后分別定義了兩個(gè)函數(shù)來(lái)導(dǎo)入對(duì)應(yīng)兩張表格。奇怪的是第一個(gè)函數(shù)input_HA()可以使用,表單導(dǎo)入成功,第二個(gè)input_ImpactList()卻不行有高手幫忙看看嗎?新手自學(xué)的python,代碼有點(diǎn)難看請(qǐng)多多包涵,謝謝啦!
# coding: utf-8-sigclass DBoperator(object): def input_HA(wbname):import xlrdimport pymysqlbook = xlrd.open_workbook(wbname + ’.xls’)sheet = book.sheet_by_name(’owssvr(2)’)conn = pymysql.connect(host=’localhost’, user=’root’, passwd=’init#201605’, db=’cc’, charset=’utf8’)cur = conn.cursor()tbcreate = ’create table ’ + wbname + ’(id INT(11) NOT NULL AUTO_INCREMENT,`Valid_or_not` VARCHAR(50) NULL DEFAULT NULL,`DCI_Number` VARCHAR(30) NULL DEFAULT NULL,`HA_DM_Number` VARCHAR(30) NULL DEFAULT NULL,`Harness_Number` VARCHAR(30) NULL DEFAULT NULL,`HA_DM_Name` VARCHAR(30) NULL DEFAULT NULL,`Basic_Number` VARCHAR(20) NULL DEFAULT NULL,`Configuration_No` VARCHAR(10) NULL DEFAULT NULL,`HA_Version` VARCHAR(10) NULL DEFAULT NULL,`Effectivity` VARCHAR(50) NULL DEFAULT NULL,`GH_compared` VARCHAR(10) NULL DEFAULT NULL,`Delivery_Date` VARCHAR(50) NULL DEFAULT NULL,`Released_Date` VARCHAR(50) NULL DEFAULT NULL,`Rejected_Date` VARCHAR(50) NULL DEFAULT NULL,`Comments` VARCHAR(1000) NULL DEFAULT NULL,`ECP_Number` VARCHAR(50) NULL DEFAULT NULL,`IDEAL_Status` VARCHAR(50) NULL DEFAULT NULL,`Item_Type` VARCHAR(50) NULL DEFAULT NULL,`Path` VARCHAR(50) NULL DEFAULT NULL,PRIMARY KEY (`id`))COLLATE='utf8_general_ci'’cur.execute(tbcreate)conn.commit()query = ’insert into ’ + wbname + ’(Valid_or_not, DCI_Number, HA_DM_Number, Harness_Number, HA_DM_Name, Basic_Number, Configuration_No, HA_Version, Effectivity, GH_compared, Delivery_Date, Released_Date, Rejected_Date, Comments, ECP_Number, IDEAL_Status, Item_Type, Path) values (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)’for r in range(1, sheet.nrows): Valid_or_not = sheet.cell(r, 0).value DCI_Number = sheet.cell(r, 1).value HA_DM_Number = sheet.cell(r, 2).value Harness_Number = sheet.cell(r, 3).value HA_DM_Name = sheet.cell(r, 4).value Basic_Number = sheet.cell(r, 5).value Configuration_No = sheet.cell(r, 6).value HA_Version = sheet.cell(r, 7).value Effectivity = sheet.cell(r, 8).value GH_compared = sheet.cell(r, 9).value Delivery_Date = sheet.cell(r, 10).value Released_Date = sheet.cell(r, 11).value Rejected_Date = sheet.cell(r, 12).value Comments = sheet.cell(r, 13).value ECP_Number = sheet.cell(r, 14).value IDEAL_Status = sheet.cell(r, 15).value Item_Type = sheet.cell(r, 16).value Path = sheet.cell(r, 17).value values = (Valid_or_not, DCI_Number, HA_DM_Number, Harness_Number, HA_DM_Name, Basic_Number,Configuration_No, HA_Version, Effectivity, GH_compared, Delivery_Date, Released_Date, Rejected_Date, Comments, ECP_Number, IDEAL_Status, Item_Type, Path) values = [’NULL’ if x == ’’ else x for x in values] values = [’NULL’ if x == 0 else x for x in values] #values2 = [’NULL’ if x == ’’ else x for x in values] print(tuple(values)) cur.execute(query , tuple(values)) conn.commit()cur.close()conn.close()pass def input_ImpactList(wbname):import xlrdimport pymysqlbook = xlrd.open_workbook(wbname+’.xls’)sheet = book.sheet_by_name(’Extract’)conn = pymysql.connect(host=’localhost’, user=’root’, passwd=’init#201605’, db=’cc’, charset=’utf8’)cur = conn.cursor()tbcreate = ’create table ’ + wbname + ’(id INT(11) NOT NULL AUTO_INCREMENT,`Change_Number` VARCHAR(50) NULL DEFAULT NULL,`ImpactedItem` VARCHAR(50) NULL DEFAULT NULL,`Change_Action` VARCHAR(50) NULL DEFAULT NULL,`EDZ` VARCHAR(50) NULL DEFAULT NULL,`Type` VARCHAR(50) NULL DEFAULT NULL,`Harness` VARCHAR(50) NULL DEFAULT NULL,`ECP_Num` VARCHAR(50) NULL DEFAULT NULL,PRIMARY KEY (`id`))COLLATE='utf8_general_ci'’cur.execute(tbcreate)conn.commit()query = ’insert into’ + wbname + ’(Change_Number, ImpactedItem, Change_Action, EDZ, Type, Harness, ECP_Num) values (%s, %s, %s, %s, %s, %s, %s)’for r in range(1, sheet.nrows): Change_Number = sheet.cell(r, 0).value ImpactedItem = sheet.cell(r, 1).value Change_Action = sheet.cell(r, 2).value EDZ = sheet.cell(r, 3).value Type = sheet.cell(r, 4).value Harness = sheet.cell(r, 5).value ECP_Num = sheet.cell(r, 6).value values = (Change_Number, ImpactedItem, Change_Action, EDZ, Type, Harness, ECP_Num) values = [’NULL’ if x == ’’ else x for x in values] values = [’NULL’ if x == 0 else x for x in values] print(tuple(values)) cur.execute(query, tuple(values)) conn.commit()cur.close()conn.close()pass
以下為報(bào)錯(cuò)信息:
問(wèn)題解答
回答1:找到原因了,是由于’insert into’中缺少空格導(dǎo)致的將’insert into’改為’insert into ’即可
相關(guān)文章:
1. mysql - 這種分級(jí)一對(duì)多,且分級(jí)不平衡的模型該怎么設(shè)計(jì)表?2. javascript - js setTimeout在雙重for循環(huán)中如何使用?3. python - pip install出現(xiàn)下面圖中的報(bào)錯(cuò) 什么原因?4. PHP類(lèi)屬性聲明?5. java - 創(chuàng)建maven項(xiàng)目失敗了 求解決方法6. javascript - vue 2.0 :key的作用7. javascript - vue-cli npm run build編譯報(bào)錯(cuò)8. javascript - Object.define 可以監(jiān)聽(tīng)object變化,那基本類(lèi)型變量如何監(jiān)聽(tīng)9. atom開(kāi)始輸入!然后按tab只有空格出現(xiàn)沒(méi)有html格式出現(xiàn)10. java - eclipse 使用git clone項(xiàng)目時(shí)報(bào)錯(cuò),請(qǐng)問(wèn)該怎么解決?
