mysql - 将数据从一个不断附加的文件中分离到一个新文件中

标签 mysql windows ms-access scripting batch-file

我正在使用宏将 Microsoft Access 数据库中的表导出到 csv 文件,以便导入到 mysql 数据库中。我最终使用了一个批处理文件,它会在导出之前在文本文件中放置一个标记,然后将最后一个标记之后的所有内容放入一个新文件中。这工作正常,除了 access 不附加的事实,但每次都会重新创建文件,因此不可能使用任何类型的标记。

有没有办法,使用 Access 或批处理文件或其他方式,a) 强制 Access 附加到文件,或放置自己的标记,或 b) 每次导出到不同的文件,可能是文件名是一个变量,例如日期,或者 c) 通过外部操作克服这种行为

最佳答案

您可以简单地创建一些代码来打开文件,然后将数据附加到其中,而不是使用宏来导出表格。

如何使用

只需将代码复制到您的应用程序中的 VBA 模块并像这样调用它:

' Export the Table "Orders" to "orders.csv", appending the data to the       '
' existing file if there is one.                                             '
ExportQueryToCSV "Orders", "C:\orders.csv", AppendToFile:=True

' Export the result of the query to "stock.csv" using tabs as delimiters     '
' and no header or quotes around strings                                     '
ExportQueryToCSV "SELECT * FROM Stock WHERE PartID=2", _
                 "C:\stock.csv", _
                 AppendToFile:=False, _
                 IncludeHeader:=False, _
                 Delimiter:=chr(9), _
                 QuoteString:=false

代码

'----------------------------------------------------------------------------'
' Export the given query to the given CSV file.                              '
'                                                                            '
' Options are:                                                               '
' - AppendToFile : to append the record to the file if it exists instead of  ' 
'                  overwriting it (default is false)                         '
' - Delimiter    : what separator to use (default is the coma)               '
' - QuoteString  : Whether string and memo fields should be quoted           '
'                  (default yes)                                             '
' - IncludeHeader: Whether a header with the field names should be the first '
'                  line (default no)                                         '
' Some limitations and improvements:                                         '
' - Memo containing line returns will break the CSV                          '
' - better formatting for numbers, dates, etc                                '
'----------------------------------------------------------------------------'
Public Sub ExportQueryToCSV(Query As String, _
                            FilePath As String, _
                            Optional AppendToFile As Boolean = False, _
                            Optional Delimiter As String = ",", _
                            Optional QuoteStrings As Boolean = True, _
                            Optional IncludeHeader As Boolean = True)
    Dim db As DAO.Database
    Dim rs As DAO.RecordSet

    Set db = CurrentDb
    Set rs = db.OpenRecordset(Query, dbOpenSnapshot)
    If Not (rs Is Nothing) Then
        Dim intFile As Integer

        ' Open the file, either as a new file or in append mode as required '
        intFile = FreeFile()
        If AppendToFile And (Len(Dir(FilePath, vbNormal)) > 0) Then
            Open FilePath For Append As #intFile
        Else
            Open FilePath For Output As #intFile
        End If

        With rs
            Dim fieldbound As Long, i As Long
            Dim record As String
            Dim field As DAO.field

            fieldbound = .Fields.count - 1

            ' Print the header if required '
            If IncludeHeader Then
                Dim header As String
                For i = 0 To fieldbound
                    header = header & .Fields(i).Name
                    If i < fieldbound Then
                        header = header & Delimiter
                    End If
                Next i
                Print #intFile, header
            End If

            ' print each record'
            Do While Not .EOF
                record = ""
                For i = 0 To fieldbound
                    Set field = .Fields(i)
                    If ((field.Type = dbText) Or (field.Type = dbMemo)) And QuoteStrings Then
                        record = record & """" & Nz(.Fields(i).value, "") & """"
                    Else
                        record = record & Nz(.Fields(i).value)
                    End If
                    If i < fieldbound Then
                        record = record & Delimiter
                    End If
                    Set field = Nothing
                Next i
                Print #intFile, record
                .MoveNext
            Loop
            .Close
        End With
        Set rs = Nothing
        Close #intFile
    End If
    Set rs = Nothing
    Set db = Nothing
End Sub

请注意,它并不完美,您可能需要调整代码以反射(reflect)您希望如何格式化数据,但在大多数情况下默认值应该没问题。

关于mysql - 将数据从一个不断附加的文件中分离到一个新文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/543387/

相关文章:

c++ - 有什么办法可以捕捉到 window 晃动吗?

mysql连接查询?

windows - 如何捕获本地网络服务器上的流量?

mysql - mysql 客户端 ".my.cnf"在 XAMPP for Windows 中的位置是什么?

c++ - 获取已安装的应用程序列表

ms-access - 调用 RunSQL 时表已锁定或正在使用

delphi - 使用MS Access数据库的Delphi 7应用程序阻止通过MS Access Access

python - 在非 Windows 平台(Linux 或 Mac)上使用 Python 中的 Access 数据库

php - 我无法回显变量,但我可以在函数中使用它吗?

mysql - 从一个表中选择多行,而另一表的计数为