必须从pyodbc将一个python的CSV文件列表加载到Access中。我不明白如何编写SQL字符串以容纳变量来代替显式定义INTO TABLE和FROM CSV文件。
单个CSV的完整功能SQL语句如下所示:
strSQL = "SELECT * INTO [TableName] FROM
[text;HDR=Yes;FMT=Delimited(,);" +
"Database=C:\Path\To\Folder].first.csv;"
是否可以修改此语句以容纳表示要导入的CSV的变量(即INTO [TableName]和FROM数据库)?
我理解它的形式类似于:
strSQL ="SELECT * INTO ? FROM Database=?",[csv_string, csv]
但引用数据库的复杂FROM语句让我摸不着头脑。
# DATABASE CONNECTION
access_path = "C:\Path\To\Access\\DB.mdb"
con = pyodbc.connect("DRIVER={{Microsoft Access Driver
(*.mdb,*.accdb)}};DBQ={};".format(access_path))
for csv in csv_list:
# RUN QUERY
strSQL = "SELECT * INTO [TableName] FROM
[text;HDR=Yes;FMT=Delimited(,);" +
"Database=C:\Path\To\Folder].first.csv;"
cur = con.cursor()
cur.execute(strSQL)
con.commit()
con.close()
你为什么不像上面那样使用.format()
?
您可以执行以下操作:
table = "TableName"
database = "C:\Path\To\Folder"
strSQL = """
SELECT * INTO [{}] FROM
[text;HDR=Yes;FMT=Delimited(,);
{}].first.csv;
""".format(table, database)
或者您可以使用以下格式:
strSQL = f"SELECT * INTO [{table}] FROM [text;HDR=Yes;FMT=Delimited(,);{database}].first.csv;"
由于选择了答案,这里是最终格式正常工作:
strSQL = "SELECT * INTO [{}] FROM [text;HDR=Yes;FMT=Delimited(,);".format(csv_str) + \
"Database=C:\PathToFolder].{};".format(csv)
我相信你可以使用Python将CSV文件加载到Access中,但为什么要经历所有麻烦。只需使用Access导入CSv文件(不要为自己创建大量不必要的工作)。
将CSV文件导入单独的表:
Function DoImport()
Dim strPathFile As String
Dim strFile As String
Dim strPath As String
Dim strTable As String
Dim blnHasFieldNames As Boolean
' Change this next line to True if the first row in CSV worksheet
' has field names
blnHasFieldNames = True
' Replace C:\Documents\ with the real path to the folder that
' contains the CSV files
strPath = "C:\Users\Excel\Desktop\test\"
' Replace tablename with the real name of the table into which
' the data are to be imported
strFile = Dir(strPath & "*.csv")
Do While Len(strFile) > 0
strTable = Left(strFile, Len(strFile) - 4)
strPathFile = strPath & strFile
DoCmd.TransferText acImportDelim, , strTable, strPathFile, blnHasFieldNames
' Uncomment out the next code step if you want to delete the
' EXCEL file after it's been imported
' Kill strPathFile
strFile = Dir()
Loop
End Function
要么...
将CSV文件导入一个表:
Private Sub Command1_Click()
Dim strPathFile As String, strFile As String, strPath As String
Dim strTable As String, strBrowseMsg As String
Dim blnHasFieldNames As Boolean
' Change this next line to True if the first row in EXCEL worksheet
' has field names
blnHasFieldNames = False
strBrowseMsg = "Select the folder that contains the CSV files:"
strPath = "C:\Users\Excel\Desktop\Coding\LinkedIn\Import all CSV Files into Different Access Tables\"
If strPath = "" Then
MsgBox "No folder was selected.", vbOK, "No Selection"
Exit Sub
End If
' Replace tablename with the real name of the table into which
' the data are to be imported
strTable = "tablename"
strFile = Dir(strPath & "\*.csv")
Do While Len(strFile) > 0
strPathFile = strPath & "\" & strFile
DoCmd.TransferText acImportDelim, , strTable, strPathFile, blnHasFieldNames
' Uncomment out the next code step if you want to delete the
' EXCEL file after it's been imported
' Kill strPathFile
strFile = Dir()
Loop
End Sub