从分隔符分隔值文件恢复数据集
简而言之,我是delphi新手,我想实现以下目标:
- 我将表定义为.cds文件{index,data,date},以及一些.csv格式的数据。
- 我想将 .csv 文件加载到表中并显示其更改和错误的日志(例如:无效的日期格式)。
问题
如何优雅地解决这个任务?
In short, i'm new to delphi and I want to achieve the following:
- I have table definition as .cds file {index, data, date}, and some data in .csv format.
- I want to load the .csv file to the table and show log it's changes and errors (ex: invalid date format).
Question
How to solve this task elegantly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我会使用 JvCsvDataSet(JEDI JVCL 组件),因为它可以正确解析 CSV 文件,然后使用数据泵组件将数据移动到客户端数据集中,并进行一些验证。
但是,如果您真正需要做的只是向数据感知控件提供 CSV 文件,我会完全省略 ClientDataSet,而只使用为您想要实现的目的而构建的组件。不要将螺丝当钉子使用,也不要将钉子当螺丝使用。它们都是由金属制成的,但它们的工作不同。
CSV 文件表定义与 CDS 表定义在用途上有很大不同,JvCsvDataSet 提供了一个简单的字符串属性,您可以设置该属性来提供元数据(字段数据类型,如整数、字符串或日期时间,以及关联的字段名称,对于缺少标题行的 CSV 文件)比您希望在 ClientDatSet 中完成的操作更容易。
I would use JvCsvDataSet (JEDI JVCL component) because it parses CSV files properly, and then use a data-pump component, to move the data into the client dataset, along with some validation.
But if all you really need to do is provide a CSV file, to a data-aware control, I would leave out the ClientDataSet completely, and just use a component built for the purpose you are trying to do. Don't use a screw as a nail, or a nail as a screw. They are both made of metal, but they do different jobs.
CSV file table definitions are quite different in purpose, to a CDS table definition, and the JvCsvDataSet provides a simple string property which you can set up to give the metadata (field datatypes like integer or string or date-time, and associated field names, for CSV files that lack a header row) more easily, than you could hope to do it in ClientDatSet.
您可以从 .csv 中逐行读取,将每一行设置为 StringList 的“DelimitedText”,将记录附加到数据集,循环字符串列表以设置每个字段的值,然后发布到数据集。
您可以将“字段值分配”/“发布”放在 try- except 块中,并记录引发异常的任何错误消息以及您喜欢的信息(例如格式错误的字段值/名称、行号和/或整个行等)到文件 fi
(我不明白你所说的“更改”是什么意思,根据我的理解,.csv 中的行将被插入到数据集中,因此所有更改都将被插入。)
编辑:为了能够讨论具体的事情(我很难掌握任务:))
示例数据(CodeGear示例“Clients.cds”的一部分):
You can read line by line from the .csv, set each line to 'DelimitedText' of a StringList, append a record to the dataset, loop the string list to set each field's value and then post to the dataset.
You can put the 'field value assinging'/'posting' in a try-except block and log any error message of raised exceptions together with information you like (e.g. malformed field value/name, line number, and/or entire line etc.) to a file f.i.
(I don't understand what you mean by 'changes', from what I understood, lines from the .csv will be inserted to a dataset, hence all changes will be inserts.)
edit: To be able to discuss on something concrete (I'm having a hard time grasping the task :))
Sample data (part of CodeGear sample 'Clients.cds'):
AFAIK,没有直接的方法将 .csv 数据加载到
TClientDataset
中。我能想到的最简单的方法是使用
TTextDataSet
(在Demos\Delphi\Database\TextData
中找到,可从开始->所有程序- >Embarcadero RAD Studio XE->示例
)。您可以像任何其他 TDataSet 一样使用它,这意味着您可以从它的Fields
中读取或使用FieldByName
,并且它支持Bof
、Eof
、下一个
和先前
。您可以简单地迭代并尝试分配给您的 CDS 列,它将生成您可以处理或记录的错误。
您可以像任何其他组件一样安装
TTextDataset
,或者只是将单元添加到uses
子句并在运行时创建它。文件夹中有一个readme.htm
文件,不多解释;关键属性是FileName
和Active
。 :)它包括预先设计的包 (
TextPkg.dproj
) 和测试应用程序 (TextTest.dproj
)。还有一个项目组 (TextDataGroup.groupproj
) - 您只需在 IDE 中打开它,构建并安装TextPkg
包,然后编译并运行测试应用程序。测试应用程序的源代码很好地显示了使用情况。AFAIK, there's no direct way to load .csv data into a
TClientDataset
.The easiest way I can think of would be to use the
TTextDataSet
(found inDemos\Delphi\Database\TextData
, available fromStart->All Programs->Embarcadero RAD Studio XE->Samples
). You can use it just like any other TDataSet, meaning you can read from it'sFields
or useFieldByName
, and it supportsBof
,Eof
,Next
, andPrior
.You can simply iterate through and try to assign to your CDS columns, and it will generate errors you can then handle or log.
You can install
TTextDataset
like any other component, or just add the unit to theuses
clause and create it at runtime. There's areadme.htm
file in the folder that doesn't explain much; the key properties areFileName
andActive
. :)It includes both a pre-designed package (
TextPkg.dproj
) and a test app (TextTest.dproj
). There's also a project group (TextDataGroup.groupproj
) - you can simply open this in the IDE, build and install theTextPkg
package, and then compile and run the test app. The source for the test app shows usage pretty well.如果您的数据库是 DBISAM,您可以简单地使用 IMPORT SQL 语句。
其他数据库可能具有类似的功能。
In the off-chance that your database is DBISAM, you can simply use the IMPORT SQL statement.
Other databases may have a similar feature.