gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 07:33:58 28 4
gpt4 key购买 nike

我正在使用宏将 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com