【问题标题】:Select into table from list of CSV从 CSV 列表中选择到表中
【发布时间】:2019-02-14 21:19:42
【问题描述】:

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

【问题讨论】:

    标签: python ms-access pyodbc


    【解决方案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;"
    

    【讨论】:

      【解决方案2】:

      感谢选择的答案,这是正确工作的最终格式:

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

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-02-17
          • 2018-03-28
          • 1970-01-01
          • 2022-09-28
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多