線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:3716
推到 Plurk!
推到 Facebook!

使用文字檔當作資料表的小範例

 
timhuang
尊榮會員


發表:78
回覆:1815
積分:1608
註冊:2002-07-15

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-07-25 23:27:02 IP:61.62.xxx.xxx 未訂閱
Borland BDE 其實也可以利用文字檔來當作 table的功能. 這個小範例為解決問題: http://delphi.ktop.com.tw/topic.php?TOPIC_ID=34591    也順便介紹文字檔資料表的作法: 1. 純文字檔: 就是你的資料表(.txt) 2. table schema檔: 要自行建立的欄位表(.sch) 這兩個檔案要同名, 放在同目錄下, 就可以利用 BDE 來進行 table 的讀取. .sch 內容說明如下:
[scan] 表格名
FILETYPE=FIXED 檔案格式為固定欄寬
CHARSET=ASCII 使用的字元集為ascii
FIELD1=SNO,FLOAT,10,0,0 欄位1 (要從1開始)=欄位名,資料型態,長度,小數點位數,起始位置(注意是從0開始)
FIELD2=SNO2,FLOAT,10,0,10
FIELD3=CNO,CHAR,8,0,20
FIELD4=YN,CHAR,1,0,28
FIELD5=RS,CHAR,2,0,29
FIELD6=SN,CHAR,11,0,31
FIELD7=ENDS,CHAR,1,0,42
其中欄位名稱為自定的, 資料型態有: bool 布林值(T,F), CHAR 字元, NUMBER 16位元整數, LONGINT 32位元整數, FLOAT 64位元浮點數, DATE 日期格式, TIME 時間格式, TIMESTAMP 日期時間格式(即時間戳). 執行的畫面如下: 發表人 - timhuang 於 2003/07/25 23:36:46
附加檔案:34655_text_db.zip
dllee
站務副站長


發表:321
回覆:2519
積分:1711
註冊:2002-04-15

發送簡訊給我
#2 引用回覆 回覆 發表時間:2003-07-26 15:02:03 IP:61.224.xxx.xxx 未訂閱
真是棒  原來用文字檔就可以了。 請問一下,使用這種方式有何限制(如筆數或檔案大小)? 我用 >已另開主題,請到此回答,謝謝。 ■【問題】使用文字檔當作資料表的限制 >> 相關的資料,放在一起比較好參考: ■ >< face="Verdana, Arial, Helvetica">引言:
Using The ASCII Driver With Comma-delimited Files - by Borland Developer Support Staff     Technical Information Database    TI892D.txt   Using The ASCII Driver With Comma-delimited Files
Category   :Database Programming
Platform    :All
Product    :Delphi  All    Description:
Delphi (and the BDE) has the capability to use ASCII files to a limited
degree as tables. The ASCII driver has the capability to translate the
data values in an ASCII fixed-length field or a comma-delimted file into
fields and values that can be displayed through a TTable component. How
this translation of the ASCII file takes place depends on an accompanying
schema file. The schema file for an ASCII data file defines various attri-
butes necessary for parsing the ASCII data file into individual field
values. The field definitions for an ASCII fixed-length field file is
relatively straightforward, the offsets of various fields in the ASCII
file being consistent across all rows in the file. However, for comma-
delimited files, this process is slightly more complicated due to the
fact that not all data values in such a file may be the same length for
all rows in the file. This article, then, concentrates on this more
difficult task of reading data from comma-delimited, or varying-length
field, files.    The Schema File
===============    The schema file for an ASCII data file contains information that defines
both the file type (comma-delimited versus fixed-length field), as well as
defining the fields that are represented by the data values in each row of
the ASCII data file. (All of the settings used in a schema file are case
insensitive, so "ascii" is just as valid as "ASCII".) In order that a
schema file be recognized as such, it must have the same filename as the
ASCII data file for which it provides definitions, but with the filename
extension .SCH (for SCHema). The attributes that describe the file are:      File name: Enclosed in square brackets, this setting specifies the
             name of the ASCII data file (sans the filename extension,
             which must be .TXT).      Filetype:  Specifies whether the ASCII data file is structured as a
             fixed-length field file (use a setting of FIXED) or a comma-
             delimited file (with data values of potentially varying
             length (use a setting of VARYING).      Delimiter: Specifies the character that surrounds String type data val-
             ues (typically, the double quotation mark, ASCII decimal 34).      Separator: Specifies the character that is used to separate individual
             data values (typically, a comma). This character must be a
             visible character, i.e., cannot be a space (ASCII decimal
             32).      CharSet:   Specifies the language driver (use a setting of ASCII).    Following the file definition settings are field definitions, one for each
data value on each row of the ASCII data file. These field definitions
supply the information Delphi and the BDE will need to create a virtual
field in memory to hold the data value, that virtual field's data type
which will affect how the value is translated after being read from the
ASCII file, and size and positioning attributes. The various settings that
will appear in each field definition are:      Field:              Virtual field name, will always be "Field" followed
                      by an integer number representing that field's ord-
                      inal position in respect to the other fields in the
                      ASCII data file. E.G., the first field is Field1,
                      the second Field2, and so on.      Field name:         Specifies the display name for the field, which
                      appears as the column header in a TDBGrid. Naming
                      convention for ASCII table fields follows that for
                      Paradox tables.      Field type:         Specifies the data tyoe BDE is to use in translating
                      the data value for each field and tells Delphi what
                      type of virtual field to create.
                      
                      Use the setting For values of type
                      --------------- ---------------------
                      CHAR            Character
                      FLOAT           64-bit floating point
                      NUMBER          16-bit integer
                      BOOL            Boolean (T or F)
                      LONGINT         32-bit long integer
                      DATE            Date field.
                      TIME            Time field.
                      TIMESTAMP       Date   Time field.
                      
                      (The actual format for date and time data values
                      will be determined by the current setting in the BDE
                      configuration, Date tab page.)      Data value length:  Maximum length of a field's corresponding data
                      value. This setting determines the length of the
                      virtual field that Delphi creates to receive values
                      read from the ASCII file.      Number of decimals: Applicable to FLOAT type fields; specifies the
                      number of digit positions to the right of the deci-
                      mal place to include in the virtual field defini-
                      tion.      Offset:             Offset from the left that represents the starting
                      position for the field in relation to all of the
                      fields that preceed it.    For example, the field definition below is for the first field in the
ASCII table. It defines a String type data value with a name of "Text",
a maximum data value length of three characters (and the field will
appear as only three characters long in Delphi data-aware components such
as the TDBGrid), no decimal places (a String data value will never have
any decimal places), and an offset of zero (because it is the first field
and there would not be any preceeding fields).      Field1=Text,Char,3,00,00    Here is an example of a schema file with three fields, the first of String
type and the second and third of type date. This schema file would be
contained in a file named DATES.SCH to provide file and field definitions
for an ASCII data file named DATES.TXT.      [DATES]
  Filetype=VARYING
  Delimiter="
  Separator=,
  CharSet=ascii
  Field1=Text,Char,3,00,00
  Field2=First Contact,Date,10,00,03
  Field3=Second,Date,10,00,13    This schema defines a comma-delimited field where all String type data
values can be recognized as being surrounded by the double quotation mark
and where distinct data values are separated by commas (excepting any
commas that may appear within the specified delimiter, inside individual
String data values). The character field has a length of three characters,
no decimal places, and an offset of zero. The first date field has a
length of 10, no decimals, and an offset of three. And the second date
field has a length of 10, no decimals, and an offset of 13.    For reading ASCII comma-delimited files, the length and offset parameters
for the field definitions do not apply to data values in the ASCII files
(as is the case for fixed-length field files), but to the virtual fields,
defined in the application, into which the values read will be placed. The
length parameter will need to reflect the maximum length of the data value
for each field -- not counting the delimiting quotation marks or the comma
separators. This is most difficult to estimate for String type data values
as the actual length of such a data value may vary greatly from row to row
in the ASCII data file. The offset parameter for each field will not be
the position of the data value in the ASCII file (as is the case for
fixed-length field files), but the offset as represented by the cumulative
length of all preceding fields (again, the defined fields in memory, not
the data values in the ASCII file.    Here is a data file that would correspond to the schema file described
above, in a file named DATES.TXT:      "A",08/01/1995,08/11/1995 
  "BB",08/02/1995,08/12/1995 
  "CCC",08/03/1995,08/13/1995    The maximum length of an actual data value in the first field is three
("CCC"). because this is the first field and there are no preceding
fields, the offset for this field is zero. The length of this first field
(3) is used as the offset for the second field. The length of the second
field, a date value, is 10, reflecting the maximum length of a data value
for that field. The accumulated length of the first and second fields are
then used as the offset for the third field (3   10 = 13) .    It is only when the proper length for the data values in the ASCII file
are used and each fields length added to any preceding fields to produce
offset values for succeeding fields that this process will correctly read
the data. If data is misread because of improper length settings in the
schema file, most values will suffer adverse translation effects, such
as truncation of character data or numeric values being interpreted as
zeros. Data will usually still be displayed, but no error should occur.
However, values that must be in a specific format in order to be trans-
lated into the appropriate data type will cause errors if the value read
includes characters not valid in a date value. This would include a date
data value which, when incorrectly read may contain extraneous characters
from other surrounding fields. Such a condition will result in a data
translation exception requiring an adjustment of the field length and
offset settings in the schema file.    Reference:    7/16/98 4:33:56 PM
■ http://www.href.com/pub/Source/D1AsciiDriver.txt
引言:
Using ASCII Tables with Delphi
-------------------------------
Delphi supports using ASCII text files as tables, with limited 
functionality.  ASCII tables are intended for exporting
and importing data to and from other formats--in general, they are 
not recommended for use as data sources for applications.    Each ASCII table requires a data file (generally with .TXT 
extension) and a schema file (with the same base file name 
and .SCH extension). The schema file contains information about 
the structure of the table and the datatypes of its columns (fields).      ASCII tables are always opened for exclusive access.  In other
words, no more than one application (session) can access an 
ASCII table at one time.  If you have opened an ASCII table at
design time, you must close it before running your application
that opens the table at run time.    Copying a table to ASCII format with Database Desktop or a TBatchmove
component will automatically create a schema file as well as the 
ASCII data file.  ASCII tables are always created with FIXED filetype.
When creating an ASCII table with Delphi (for example with BatchMove 
or CreateTable methods), you must specify TableType as ttASCII.  You 
should not leave TableType as ttDefault.    ASCII tables do not support the following:
* Indexes (and therefore, any methods or functions that require an
  index, such as GoToKey, SetRange, etc.)
* TQuery components (SQL)
* Deleting records
* Inserting records.  Insert always appends the record to the end 
  of the table.
* Referential Integrity
* Blob datatypes
* DELIMITED tables do not allow modification (editing) of records    The Schema File
---------------
All information in the schema file is case-insensitive.
Here is a sample of what a schema file looks like:    [CUSTOMER]                                // File name with no extension.
FILETYPE = VARYING                 // Format: VARYING or FIXED
CHARSET = ascii                        // Language driver name.
DELIMITER = "                        // Delimiter for char fields.
SEPARATOR = ,                             // Separator character 
Field1 = Name,CHAR,12,0,0        // Field information
Field2 = Salary,FLOAT,8,2,12             The schema file has a format similar to Windows INI files.
The file begins with the name of the table in brackets.
The second line specifies the file format following the 
keyword FILETYPE: FIXED or VARYING.
* In a FIXED format file, each field always takes up a fixed
  number of characters in the file, and the data is padded with
  blanks as needed. 
* In a VARYING file, each field takes a variable number of characters,
  each character field is enclosed by DELIMITER characters, and the 
  fields are separated by a SEPARATOR character.  The DELIMITER and 
  SEPARATOR must be specified for a VARYING format file, but
  not for a FIXED format file.    The CHARSET attribute specifies the name of the language
driver to use.  This is the base file name of the .LD
file used for localization purposes.    The remaining lines specify the attributes of the table's 
fields (columns). Each line must begin with "Fieldx = ", 
where x is the field number (i.e. Field1, Field2, and so on).
Then comes a comma-delimited list specifying:
* Field name - Same restrictions as Paradox field names.
* Datatype - The field data type.  See below.
* Number of characters or units.  Must be <= 20 for numeric
  data types.  Total maximum number of characters for date/time 
  datatypes (including / and : separators).
* Number of digits after the decimal (FLOAT only).
* Offset - Number of characters from the beginning of the line
  that the field begins.  Used for FIXED format only.    The following data types are supported:
CHAR - Character
FLOAT - 64-bit floating point
NUMBER - 16-bit integer
BOOL - Boolean (T or F)
LONGINT - 32-bit long integer
DATE - Date field.  Format specified by IDAPI.CFG
TIME - Time field. Format specified by IDAPI.CFG
TIMESTAMP - Date   Time field. Format specified by IDAPI.CFG    NOTE: You can specify Date and time formats in the BDE configuration 
utility      Example 1 - VARYING format file 
-------------------------------
CUSTOMER.SCH:    [CUSTOMER]
Filetype=VARYING
Delimiter="
Separator=,
CharSet=ascii
Field1=Customer No,Float,20,04,00
Field2=Name,Char,30,00,20
Field3=Phone,Char,15,00,145
Field4=First Contact,Date,11,00,160    CUSTOMER.TXT:    1221.0000,"Kauai Dive Shoppe","808-555-0269",04/03/1994
1231.0000,"Unisco","809-555-3915",02/28/1994
1351.0000,"Sight Diver","357-6-876708",04/12/1994
1354.0000,"Cayman Divers World Unlimited","809-555-8576",04/17/1994
1356.0000,"Tom Sawyer Diving Centre","809-555-7281",04/20/1994    Example 2 - FIXED format file
-----------------------------
CUSTOMER.SCH:    [CUSTOMER]
Filetype=Fixed
CharSet=ascii
Field1=Customer No,Float,20,04,00
Field2=Name,Char,30,00,20
Field3=Phone,Char,15,00,145
Field4=First Contact,Date,08,00,160    CUSTOMER.TXT:               1221.0000Kauai Dive Shoppe             808-555-0269   04/03/94
           1231.0000Unisco                        809-555-3915   02/28/94
           1351.0000Sight Diver                   357-6-876708   04/12/94
           1354.0000Cayman Divers World Unlimited 809-555-8576   04/17/94
           1356.0000Tom Sawyer Diving Centre      809-555-7281   04/20/94
沒空更新的網頁... http://dllee.ktop.com.tw C及指標教學,計算機概論,資訊管理導論... http://dllee.adsldns.org 介紹Shells,LiteStep,GeoShell.... 發表人 - dllee 於 2003/07/27 16:10:33
------
http://www.ViewMove.com
cashxin2002
版主


發表:231
回覆:2555
積分:1937
註冊:2003-03-28

發送簡訊給我
#3 引用回覆 回覆 發表時間:2003-07-26 18:47:35 IP:63.84.xxx.xxx 未訂閱
Thanks for your help... >
------
忻晟
jeffco
一般會員


發表:9
回覆:19
積分:5
註冊:2003-11-15

發送簡訊給我
#4 引用回覆 回覆 發表時間:2004-03-17 20:50:06 IP:211.76.xxx.xxx 未訂閱
HI,timhuang 請問若文字檔為中文時是否會變亂碼 因為我文字檔有部分是中文的
系統時間:2024-11-22 17:35:34
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!