第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何從 Excel>Python>Microsft Access 傳輸數(shù)據(jù)

如何從 Excel>Python>Microsft Access 傳輸數(shù)據(jù)

犯罪嫌疑人X 2023-08-22 14:39:39
我已經(jīng)為此工作好幾天了。請有人提供任何您可以提供的建議。這是我當(dāng)前的代碼(它不起作用,我知道為什么它不起作用):import pyodbcimport openpyxlpath = ('C:\\Access_Test.xlsx')wb = openpyxl.load_workbook(path)sheet = wb.activeb2 = a2 = sheet['A2']b2 = sheet['B2']c2 = sheet['C2']d2 = sheet['D2']e2 = sheet['E2']f2 = sheet['F2']g2 = sheet['G2']h2 = sheet['H2']i2 = sheet['I2']j2 = sheet['J2']k2 = sheet['K2']l2 = sheet['L2']m2 = sheet['M2']n2 = sheet['N2']o2 = sheet['O2']test2 = (")'")test =  (a2.value, b2.value, c2.value, d2.value, e2.value, f2.value, g2.value, h2.value, i2.value, j2.value, k2.value, l2.value, m2.value),(test2)  #Everything to this point is fine.  I can read & print everything from the Excel document (though the formatting is an issue with how the query statements work in pyodbc).driver = '{Microsoft Access Driver(*.mdb, *accdb)}'filepath = 'C:\\Users\\Db_Mngr\\Desktop\\PythonTests\\Microsoft Studio Projects\\HUD Report-2001-Copy For Python.mdb'#Find data sourcesmyDataSources = pyodbc.dataSources()access_driver = myDataSources['MS Access Database']#This is the full command to open the Access databasecnxn = pyodbc.connect(driver=access_driver, dbq=filepath, autocommit=True)crsr = cnxn.cursor()crsr.execute(str(test))如果我使用:print:(test)我的輸出如下所示(此測試的所有數(shù)據(jù)都是假的):("'''INSERT INTO Python_Test([Case2], [Last], [First], [Initial Intake], [Intake], [Age], [Gender], [Ethnic], [Race], [DOB], [SSN], [Educ Lvl], [Marital])VALUES", ('Sep00000', 'Test', 'Test', '01/01/2020', '01/01/2020', 1, 'Male', 'A. Hispanic', 'E. White', '01/01/2020', 0, 'High School'), ")'")正如你所看到的,對于 pyodbc 來說,這大約是 70% 正確的,但它顯然會拋出錯誤(開頭太多引號,“VALUES”后面的引號,“VALUES”后面的“”......等等,你明白我的意思)。有誰能夠嘗試解釋如何使這段代碼發(fā)揮作用嗎?刪除開頭的額外引號不一定是最大的問題,我想我可以解決這個問題;但“VALUES”部分之后發(fā)生的一切都是一團(tuán)糟。如有任何反饋,將不勝感激!
查看完整描述

2 回答

?
慕姐8265434

TA貢獻(xiàn)1813條經(jīng)驗 獲得超2個贊

我猜你只需要正確格式化字符串即可成為有效的 SQL 查詢。嘗試這樣的事情

sql = f"INSERT INTO table([Case2], [Last], ...) VALUES ({a2.value}, {b2.value}, ...)"

或者

sql = ''.join(test)


查看完整回答
反對 回復(fù) 2023-08-22
?
瀟瀟雨雨

TA貢獻(xiàn)1833條經(jīng)驗 獲得超4個贊

這是工作代碼!


import pyodbc


import openpyxl


path = ('C:\\Users\\Db_Mngr\\Desktop\\PythonTests\\Microsoft Studio Projects\\Access_Test.xlsx') #Set the path to the Excel document that you want to transfer data from

wb = openpyxl.load_workbook(path)

sheet = wb.active



b2 = sheet['B2']

c2 = sheet['C2']

d2 = sheet['D2']

e2 = sheet['E2']

f2 = sheet['F2']

g2 = sheet['G2']

h2 = sheet['H2']

i2 = sheet['I2']

j2 = sheet['J2']

k2 = sheet['K2']

l2 = sheet['L2']

m2 = sheet['M2']

n2 = sheet['N2']

o2 = sheet['O2']


#This is the trouble spot.  If you've ever worked with this stuff you know that the formatting has to be PERFECT.  A single space out of place throws errors. 


startcmmd = "'''INSERT INTO Python_Test([Case2], [Last], [First], [Initial Intake], [Intake], [Age], [Gender], [Ethnic], [Race], [DOB], [SSN], [Educ Lvl], [Marital])VALUES('{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}')'''".format(b2.value, c2.value, d2.value, e2.value, f2.value, g2.value, h2.value, i2.value, j2.value, k2.value, l2.value, m2.value, n2.value)


#Get connected to your Access document


driver = '{Microsoft Access Driver(*.mdb, *accdb)}'

filepath = 'C:\\Users\\Db_Mngr\\Desktop\\PythonTests\\Microsoft Studio Projects\\HUD Report-2001-Copy For Python.mdb'



myDataSources = pyodbc.dataSources()

access_driver = myDataSources['MS Access Database']


#set up your cursor    


cnxn = pyodbc.connect(driver=access_driver, dbq=filepath, autocommit=True)

crsr = cnxn.cursor()


#Now execute!  Don't forget to run this with eval!

crsr.execute(eval(startcmmd))



查看完整回答
反對 回復(fù) 2023-08-22
  • 2 回答
  • 0 關(guān)注
  • 1679 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號