找回密码
 加入
搜索
查看: 8256|回复: 7

[AU3基础] 如何提高txt文件数据导入到(oracle或access)数据库?

  [复制链接]
发表于 2012-5-15 21:20:38 | 显示全部楼层 |阅读模式
本帖最后由 auto 于 2012-5-15 21:26 编辑

如何提高txt文件数据导入到(oracle)数据库?有没有什么更好办法

采用ADO连接方式。。采用OraOLEDB.Oracle驱动。
目前是读取文本,读一行,按分隔符进行拆分字段,insert into 数据库,重复上面动作
直到一定数量(5000),commit数据库
数据库有字段约10个左右。。另外一个表有几十个字段,导入速度只在1000条/S左右
400M txt文档,全导入超过1000S
oracle 11 G 机器也不算差,但速度就是提不上去。
发表于 2012-5-17 14:32:10 | 显示全部楼层
本帖最后由 kodin 于 2012-5-17 14:38 编辑

用schema.ini文件可以达到一次性导入目的,导入速度具体怎样真没测试过,不过肯定会比一条条导入的速度快。
简单打个比方:
数据库文件:DB1.mdb
需要导入的表:Table2
需要导入的TXT文件:Table2.txt

数据库表架构
id    name    pass
schema.ini文件内容
[Table2.txt]
ColNameHeader=True
CharacterSet=936
Format=TabDelimited
Col1=id Integer
Col2=name Char Width 255
Col3=pass Char Width 255
Au3代码:
$adSource = @ScriptDir & "\DB1.mdb"
$adTable = "Table2"
$addfld = ObjCreate("ADODB.Connection")
$addfld.Open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & $adSource)
$addfld.Execute("insert into Table2 SELECT * FROM [Text;FMT=Delimited;HDR=Yes;DATABASE=C:\Users\Kodin\Desktop\].[Table2#txt]")
$addfld.close
发表于 2012-5-17 22:23:22 | 显示全部楼层
第一次知道有这种用法,学习了,百度下:
Schema.ini用于提供文本文件中记录的构架信息。每个 Schema.ini 项都用于指定表的五个特征之一:
1、文本文件名
2、文件格式
3、字段名、字段长度、字段类型
4、字符集
5、特别数据类型转换

指定文件名
文件名要用方括号括起来,例如如果要对 Sample.txt 使用数据构架信息文件,那么它的对应的项应该是[Sample.txt]

指定文件格式
格式说明
[/td]

[td=1,1,50%]

表格式
[/td]

[td=1,1,35%]

Schema.ini 格式描述
Tab 制表符分隔        文件中的字段用制表符分隔        Format=TabDelimited
CSV 分隔        文件中的字段用逗号来分隔        Format=CSVDelimited
自定义分隔        文件中的字段可以用任何字符来分隔,所有的字符都可以用来分隔,包括空格,但是双引号 ( " ) 除外        Format=Delimited(自定义分隔符)
- 或者没有分隔符 -
Format=Delimited( )
固定宽度        文件中的字段为固定长度        cg1留言,经测试,参数为:Format=FixedLength

指定字段
你可以有两种方法在一个字符分隔的文本文件中指定字段名
1、在文本文件中的第一行包含字段名,并且设置 ColNameHeader 为 True 。
2、用数字编号指定每一列并且指定每一列的名字以及数据类型
你必须用数字编号指定每一列并且指定每一列的名字、数据类型以及长度(在固定长度分隔的文本文件中需要指定长度)
注意,设定了 ColNameHeader 选项,在 Schema.ini 中 Windows 注册时会忽略 FirstRowHasNames 选项。
你也可以指定字段的数据类型,使用 MaxScanRows 选项用来指定在确定列的数据类型时要扫描多少行数据。设置 MaxScanRows 为 0 将扫描整个文件。
如果文本文件第一行包含字段名,并且要扫描整个文件,改项目就要定义如下:ColNameHeader=True
MaxScanRows=0


接下来的项目用来指定表中的字段,使用列编号(Coln)选项来指定列。字段长度在“固定分隔文本文件中”是必填项目,在“字符分隔文本文件”中是可选项目。
示例:定义 2 个字段,CustomerNumber 是长度为 10 的文本字段、CustomerName 是长度为 30 的文本字段。Col1=CustomerNumber Text Width 10
Col2=CustomerName Text Width 30

语法如下:
Coln=ColumnName type [Width #]
参数解释如下:参数        说明
ColumnName        文本,标识字段名,如果包含空格要用双引号括起来
type        数据类型包括:
Microsoft Jet 数据类型:Bit Byte Short Long Currency Single Double DateTime Text Memo
ODBC 数据类型: Char (same as Text) Float (same as Double) Integer (same as Short) LongChar (same as Memo) Date date format
其中date format 是日期的格式字符串例如:Date YYYY-MM-DD
Width        字符串的长度,后面的数字用来指定字段的长度(“固定分隔文本文件”为必填,“文字分隔文本文件”为可选)
#        整形数字,标识字段长度


指定字符集
CharacterSet 项有两个选择:ANSI | OEM
选择 ANSI 字符集用如下方法:CharacterSet=ANSI

特别数据类型转换
特别数据类型转换主要是定义比如日期、货币型数据如何转换或者如何显示的,你可以参考下面这张表:选项        说明
DateTimeFormat        Can be set to a format string indicating dates and times. You should specify this entry if all date/time fields in the import/export are handled with the same format. All Microsoft Jet formats except A.M. and P.M. are supported. In the absence of a format string, the Windows Control Panel short date picture and time options are used.
DecimalSymbol        Can be set to any single character that is used to separate the integer from the fractional part of a number.
NumberDigits        Indicates the number of decimal digits in the fractional portion of a number.
NumberLeadingZeros        Specifies whether a decimal value less than 1 and greater than –1 should contain leading zeros; this value can either be False (no leading zeros) or True.
CurrencySymbol        Indicates the currency symbol to be used for currency values in the text file. Examples include the dollar sign ($) and Dm.
CurrencyPosFormat        Can be set to any of the following values: · Currency symbol prefix with no separation ($1)
· Currency symbol suffix with no separation (1$)
· Currency symbol prefix with one character separation ($ 1)
· Currency symbol suffix with one character separation (1 $)
CurrencyDigits        Specifies the number of digits used for the fractional part of a currency amount.
CurrencyNegFormat        Can be one of the following values: · ($1)
· –$1
· $–1
· $1–
· (1$)
· –1$
· 1–$
· 1$–
· –1 $
· –$ 1
· 1 $–
· $ 1–
· $ –1
· 1– $
· ($ 1)
· (1 $)
This example shows the dollar sign, but you should replace it with the appropriate CurrencySymbol value in the actual program.
CurrencyThousandSymbol        Indicates the single-character symbol to be used for separating currency values in the text file by thousands.
CurrencyDecimalSymbol        Can be set to any single character that is used to separate the whole from the fractional part of a currency amount.

下面给出一个简单的例子,假设有一个表Contacts.txt类似下面:姓名 单位 联系日期
王海 上海有机化学研究所 2002-1-1
罗炙 数字化机床研究院 2004-1-1

导入 access 应该类似下面表格:
姓名
[/td]

[td=1,1,30%]单位[/td]
[td=1,1,30%]

联系日期
王海        上海有机化学研究所        2002-1-1
罗炙        数字化机床研究院        2004-1-1

那么 Schema.ini 则是类似下面的INI文件:[Contacts.txt]
ColNameHeader=True
format=Delimited(" ")
MaxScanRows=0
CharacterSet=ANSI
Col1="姓名" Char Width 10
Col2="单位" Char Width 9
Col3="联系日期" Date Width 8


注释如下:[Contacts.txt] ///文本文件名
ColNameHeader=True ///带有表头
format=Delimited( ) ///空格作为分隔符,如果是分号,请用format=Delimited(;) 来解决
MaxScanRows=0 ///扫描整个文件
CharacterSet=ANSI ///ANSI 字符集
Col1="姓名" Char Width 10 ///字段1
Col2="单位" Char Width 9 ///字段2
Col3="联系日期" Date Width 8 ///字段3
///如果有更多字段可 Col4 .... ColN


注意,Schema.ini 必须和需要导入的文本文件在同一目录。
此后,我们就可以利用下面的语句来导入数据了:CurrentProject.Connection.Execute "SELECT * INTO NewContact FROM [Text;FMT=Delimited;HDR=Yes;DATABASE=C:\;].[Contacts#txt];"
 楼主| 发表于 2012-5-21 09:25:39 | 显示全部楼层
回复 3# kodin


    这个支持oracle 吗??测试了一下ORACLE导入文本文件,显示语句错误。
ACCESS在网上搜索了一下,好像是支持。但是没有提到支持ORACLE,有没有其它方法
支持AU3连接ORACLE,可以数组导入数据库或文件导入数据库?
发表于 2012-5-21 11:10:27 | 显示全部楼层
本帖最后由 menfan1 于 2012-5-21 11:11 编辑

DTS导入,OCI导出飞快哈
发表于 2012-5-22 01:19:12 | 显示全部楼层
没用过ORACLE,帮不上忙了。
 楼主| 发表于 2012-5-22 13:18:52 | 显示全部楼层
回复 6# menfan1


    DTS是什么,ORACLE自带的工具?
OCI编程太复杂了?有没有AU3调用OCI接口例子?
 楼主| 发表于 2012-6-18 20:28:48 | 显示全部楼层
回复 6# menfan1


    大侠。。给个例子看看。。是否可以用远程?
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-9-20 11:47 , Processed in 0.076524 second(s), 23 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表