从CSV列表中选择进入表格

时间:2019-02-14 21:19:42

标签: python variables access pyodbc insert-into

必须从pyodbc将CSV文件的python列表加载到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()  

3 个答案:

答案 0 :(得分:1)

为什么不像上面那样使用.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;"

答案 1 :(得分:0)

感谢您选择的答案,这是可以正常使用的最终格式:

strSQL = "SELECT * INTO [{}] FROM [text;HDR=Yes;FMT=Delimited(,);".format(csv_str) + \
          "Database=C:\PathToFolder].{};".format(csv)

答案 2 :(得分:0)

我确定您可以使用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