ADO 提取 CSV 的数据类型问题
好的,在使用 ADO 从 CSV 文件中提取邮政编码时遇到问题。发生的情况是,如果第一组邮政编码的长度为 5 位,则数据类型将被分配为记录集中整个字段属性的整数,如果以下邮政编码不是 9 位邮政编码,则不会出现问题带有破折号的代码(99999 与 99999-9999)。当遇到这些 9 位邮政编码时,该字段将生成空值。因此,我只能假设 JET 4 获取一小部分数据样本(前 3 条记录?)并将数据类型分配给该字段。我尝试在运行时设置字段类型,但失败了,因为我不知道我在做什么,或者它超出了我的控制范围。
有几点值得注意: 1.我可以控制提取信息的SQL语句(使用group by子句) 2. 在数据被拉入之前我的控制能力有限。 3.运行语言VB6使用ADO。
有什么建议或指示吗?
Ok, running into a problem with pulling a Zip code from a CSV file using ADO. Whats happening is that if the first set of zip codes are 5 digits long, the datatype is assigned as an integer for the whole field property in the recordset which wouldn't be a problem if the following zip codes weren't the 9 digit zip code with the dash (99999 vs 99999-9999). When these 9 digit zip codes are encountered, the field yields a null. So, I can only assumet that JET 4 takes a small sample of the data (first 3 records?) and assigns the datatype to that field. Ive tried setting the field type during runtime, but failed either because I don't know what I am doing or its beyond my control.
A couple of note worthies:
1. I have control of the SQL statement that pulls the info (which uses a group by clause)
2. I have limited control before the data is pulled in.
3. The runtime language VB6 using ADO.
Any suggestions or pointers?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
似乎执行此操作的常见方法是使用 架构.ini文件:
我使用以下文件设置了一个示例:
test.csv:
Schema.ini
test.vbs
输出如下所示:
请记住Schema.ini 文件必须与文本文件位于同一文件夹中,并且必须按名称引用文本文件(上例中的
[test.csv]
)。您可能必须为导入的每个文件动态生成 Schema.ini 文件,或者复制 csv 文件并将其重命名到临时位置。It seems like the common way to do this is using a Schema.ini file:
I set up an example using the following files:
test.csv:
Schema.ini
test.vbs
And the output looks like:
Keep in mind that the Schema.ini file must be in the same folder as the text file and must refer to the text file by name (
[test.csv]
in the example above). You may have to dynamically generate the Schema.ini file for each file you import, or make a copy and rename of the csv file to a temporary location.是的,ADO 确实根据第一条记录进行类型强制。破折号“-”是整数类型中的无效字符,因此不会解析它们。您希望整个内容都是一个字符串(实际上,邮政编码是字符串,而不是整数)。
准备数据可能是值得的。编写一个小程序来读取 CSV 并将 -0000 添加到 5 位记录中。那么所有记录都将是 10 位数字,带有 -,并且应该被强制转换为字符串。
Yes, ADO does type coercion based on the first record. Dash '-' is an invalid character in an integer type, so it won't parse them. You want the entire thing to be a string (Really, you do- zip codes are strings, not integers).
It might be worth it to preparse the data. Write a small program to read the CSV and add -0000 to the 5-digit records. Then all records will be 10 digits, with a -, and should be coerced to a string.