【问题标题】:Separating data from a constantly appended file into a new file将数据从不断附加的文件中分离到一个新文件中
【发布时间】:2010-10-07 07:14:42
【问题描述】:

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

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

【问题讨论】:

  • 是否有某些原因不能使用 MyODBC 驱动程序并直接从 Access 执行此操作?此外,您的问题还不清楚在什么情况下您会一直想要导入 MySQL 的 CSV 文件中的旧数据。

标签: mysql windows ms-access scripting batch-file


【解决方案1】:

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

如何使用

只需将代码复制到应用程序中的 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

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

【讨论】:

  • 不确定您所说的“独立文件”是什么意思。该代码应位于 Access 应用程序的 Access VBA 模块中。您只需将要保存为 CSV 的 SQL 或表的名称或查询的名称传递给它。如果文件已经存在,它将追加到它而不是创建一个新文件。
  • 我的意思是这可以从 .vbs 文件而不是从访问数据库中运行吗?我对编辑访问文件有很大的限制。
  • 不,您必须从 Access 运行它,但它不需要是原始应用程序:只需创建一个新的 Access 文件,将代码放入其中并链接表或查询您想用到原文件。这样一来,您根本不必修改原始应用程序。
  • 谢谢。做了一个梦。使用普通访问导出向导导出时,我遇到了科学记数法数字的问题。
【解决方案2】:

a) 强制访问附加到文件,或放置自己的标记

没有。导出每次都会写入一个全新的文件,如果文件已经存在,则会出现问题。

b) 每次导出到不同的文件,文件名可能是一个变量,例如日期

是的,将路径/文件名构建为字符串,并将日期/时间附加到末尾

c) 通过外部操纵来克服这种行为

好吧,您可以将新的内容转储到一个虚拟文件中,然后使用批处理脚本或系统调用:

echo marker >> goodfile.csv
type dummy >> goodfile.csv
del dummy

但是,如果您只是想添加新记录,更好的方法是只处理虚拟文件,而不是尝试寻找最后一个标记并处理其下方的所有内容。

【讨论】:

    【解决方案3】:

    您还可以使用 VBScript 来执行用于导出的查询,并将这些记录附加到现有文件中。

    【讨论】:

    • 或者使用 vbscript 我什至可以在每次我想的时候放入一个新文件?问题是我上次尝试使用 vbscript 时,它的输出与宏的输出不一样。
    • 是的,你可以。 Access 宏的问题之一是它们确实不那么灵活。您几乎仅限于使用 GUI 可以做的事情。 VBA 模块和/或 VBScript 脚本为您提供更强大的功能,但更复杂。
    • 如果我是你,我会编写一个 VBA 函数来执行导出,然后我可以在其中以编程方式控制要导出到的文件的名称,然后只需使用宏调用该函数即可。
    猜你喜欢
    • 2014-08-06
    • 2015-10-17
    • 2011-07-05
    • 1970-01-01
    • 2016-04-28
    • 1970-01-01
    • 2021-03-05
    • 1970-01-01
    • 2019-08-15
    相关资源
    最近更新 更多