導入文本文件時如何指定字段類型?(二)
2024-07-21 02:08:23
供稿:網友
網站運營seo文章大全提供全面的站長運營經驗及seo技術!
導入文本文件時如何指定字段類型?
問題:
我有一個文本文件需要導入 access ,但是文本文件中有一列數據原本是文本,但是導入數據庫后自動變成了“雙精度”類型,我該如何讓各個字段按我需要的數據類型生成哪?
如何讓 access 按照我規定的構架、規格從文本文件、xls文檔中導入數據?
方法二:
用 schema.ini 文件也可以達到要求。
作用:schema.ini用于提供文本文件中記錄的構架信息。每個 schema.ini 項都用于指定表的五個特征之一:
1、文本文件名
2、文件格式
3、字段名、字段長度、字段類型
4、字符集
5、特別數據類型轉換
指定文件名
文件名要用方括號括起來,例如如果要對 sample.txt 使用數據構架信息文件,那么它的對應的項應該是
[sample.txt]
指定文件格式
格式說明表格式schema.ini 格式描述tab 制表符分隔文件中的字段用制表符分隔format=tabdelimitedcsv 分隔文件中的字段用逗號來分隔format=csvdelimited自定義分隔文件中的字段可以用任何字符來分隔,所有的字符都可以用來分隔,包括空格,但是雙引號 ( " ) 除外
format=delimited(自定義分隔符)
- 或者沒有分隔符 -
format=delimited( )
固定寬度文件中的字段為固定長度
指定字段
你可以有兩種方法在一個字符分隔的文本文件中指定字段名
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 應該類似下面表格:
姓名單位聯系日期王海上海有機化學研究所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];"
注意,到 2000 格式的 mdb 為止,以下語句都會導致導入失敗,應該是 access 本身的問題:
到 access xp / access 2003 出現不知道是否已經解決該問題,大家可以在“評論”中告訴我測試結果。
docmd.transfertext acimportfixed, , "contacts", "c:/contacts.txt"
或者
docmd.transfertext acimportfixed, "c:/.ini", "contacts", "c:/documents.txt"
錯誤消息為:
運行時錯誤 '3625':
文本文件規范 'c:schema.ini' 不存在。不能使用規范進行導入、導出或者鏈接。
或者
運行時錯誤 '2511':
這個操作或方法需要一個 specification name 參數。
另外,請參考:
http://support.microsoft.com/default.aspx?scid=kb;en-us;241477
http://www.access911.net 站長收藏