📄 ug_ch10.htm
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta name="generator" content="HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org"><meta http-equiv="content-type" content="text/html; charset=us-ascii"><title>db.* User's Guide Chapter 10</title></head><body><h1><a name="Transfer" id="Transfer"></a>Chapter 10<br>File Transfer Utilities (dbimp, dbexp)</h1><h2><a name="Introduction" id="Introduction"></a>10.1Introduction</h2><p><font size="2">The <b><i>db.*</i></b> product stores all itsdata in a special-purpose format. This format is specific to theproduct and all data in a <b><i>db.*</i></b> database must havebeen created by the <b><i>db.*</i></b> runtime functions. In thedevelopment of a system, data often already exists in a formatother than that used by <b><i>db.*</i></b>, the most common beingASCII format. <b><i>db.*</i></b> cannot operate directly on ASCIItext files. To transfer data from an ASCII text file into<b><i>db.*</i></b>, a utility must be used that understands bothformats.</font></p><p>It is possible to write <b><i>db.*</i></b> applications to readdata from a text file and enter it into <b><i>db.*</i></b> records,or vice versa. Entering bulk data from an external source into adatabase is called importing. Reading data from <b><i>db.*</i></b>and writing it in a different format is called exporting. Toaccomplish these tasks, two general-purpose utilities are providedas a part of the <b><i>db.*</i></b> tool set. The utility thatimports data to a <b><i>db.*</i></b> database is called<b>dbimp</b>. The utility that exports data is called<b>dbexp</b>.</p><p>These utilities use ASCII text files as the standard externalrepresentation of data. These files can be created or modified bytext editors or string manipulation programs. Most databasemanagement systems have the ability to export data into ASCII (seeFigure 10-1).</p><p>Both <b>dbexp</b> and <b>dbimp</b> view an ASCII record as oneline of text, terminated by a newline character (and Entercharacter on some computers), with fields separated by a standardseparator character. Our utilities use the comma as a separatorcharacter. . Numeric fields are represented by ASCII digits, inbase 10. Character fields are represented by ASCII alphanumericcharacters, optionally enclosed by double quotation marks. Thefields (or records) are not fixed-length.</p><p align="center"><b><img alt="Fig. 10-1. Importing and Exporting Data" src="dbstar_10-1.gif"><br><br>Fig. 10-1. Importing and Exporting Data</b></p><p>All ASCII records in one file must be of the same type. Thismeans that there are the same number of fields on each line, andthe same sequence of field types is maintained from line to line.For example, if one record has a numeric field followed by twocharacter fields, all records must have this format. One characterfield followed by two numeric fields is not permitted. All outputfrom <b>dbexp</b> will be created in the same format. All inputread by <b>dbimp</b> is expected in this format. The following textshows six records of the same record type, each containing sevenfields. The first two fields and the last field are numeric, whileall the other fields are character strings. (The NULL fieldrepresents no owner pointer for the set <b>article_list</b> asdeclared in the <b>tims.ddl</b>.)</p><pre><font color="#0000FF">2,0,"db001","Database Design","McGraw Hill","1983",06,0,"db002","An Intro...","Addison Wesley","1981",011,0,"db003","Microcomputer ...","McGraw Hill","1985",016,0,"db004","Computer ...","Prentice Hall","1977",019,0,"db005","Fundamental ...","Yourdan","1981",0</font></pre><p><font size="2">The file format above was created using thefollowing:</font></p><pre><font color="#0000FF">dbexp -m tims info</font></pre><p><font size="2">A second record type shown below would be storedin a different text file:</font></p><pre><font color="#0000FF">0:2,"4GL"0:3,"languages"0:4,"reliability"0:5,"debugging"0:6,"testing"0:7,"database design"</font></pre><p><font size="2">The above file format was createdusing:</font></p><pre><font color="#0000FF">dbexp -r tims info</font></pre><h2><a name="Exporting" id="Exporting"></a>10.2 Exporting Data</h2><p><font size="2">The <b><i>db.*</i></b> exporting utility,<b>dbexp</b>, will convert all instances of selected record typesinto ASCII text files. You can select all the record types in thedatabase for export. The resulting text files can in turn be usedby the import utility, <b>dbimp</b>, by a text editor, or by anyother text manipulation software. Since ASCII text is well suitedfor transmission to other platforms, this feature allows databasesto be moved to computers of different types.</font></p><p>An example execution of <b>dbexp</b> might look like the examplebelow:</p><pre><font color="#0000FF">dbexp tims info</font></pre><p><font size="2">This would export data from the <b>info</b>record type of the <b>tims</b> database. The <b>info</b> recordtype looks like the following:</font></p><pre><font color="#0000FF">record info { unique key char id_code[16]; char info_title[80]; char publisher[32]; char pub_date[12]; int info_type;}</font></pre><p><font size="2">The file created by <b>dbexp</b> would be named<b>info.txt</b> and would contain records created from the contentsof the corresponding <b><i>db.*</i></b> data file, asfollows:</font></p><pre><font color="#0000FF">"db001","Database Design","McGraw Hill","1983",0"db002","An Intro...","Addison Wesley","1981",0"db005","Fundamental ...","Yourdan","1981",0</font></pre><p><font size="2">The <b>dbexp</b> utility is invoked asfollows:</font></p><pre><font color="#0000FF">dbexp [-r] [-m] [-n] [-d] [-s "<<i>char</i>>"] [-e "<<i>char</i>>"] [-xml] [-dtd] <i>dbname</i> [<i>rtype</i>...]</font></pre><p><font size="2">When no record types (<i>rtype</i>) are listedfollowing the database name (<i>dbname</i>), <b>dbexp</b> willexport all record types in the database. If one or more recordtypes are listed, only those record types will beexported.</font></p><p>The <b>-s</b> option indicates that the following argument is acharacter, enclosed in quotes, to be used as a separator character.The default is a comma. For example, if a vertical bar characterwere desired, the following command could be used:</p><pre><font color="#0000FF">dbexp -s "|" tims info</font></pre><p><font size="2">The resulting file would contain the databelow:</font></p><pre><font color="#0000FF">"db001"|"Database Design"|"McGraw Hill"|"1983"|0"db002"|"An Intro..."|"Addison Wesley"|"1981"|0"db005"|"Fundamental ..."|"Yourdan"|"1981"|0</font></pre><p><font size="2">The <b>-e</b> option changes the escape character(default is backslash), which is to be used in front of eachquotation mark character contained within a character string, oreach backslash character contained in a string. For example, if afield contains the following string:</font></p><pre><font color="#0000FF">Your "filing cabinet" is called C:\DRAWERS.</font></pre><p><font size="2">With <b>dbexp</b>, the string converts to thefollowing format as it is written into the ASCII file:</font></p><pre><font color="#0000FF">"Your \"filing cabinet\" is called C:\\DRAWERS."</font></pre><p><font size="2">You may wish to change the escape character iftext in your database contains many backslashes. This would preventeach backslash from becoming a double backslash. Note, however,that if the exported data will be used as input to <b>dbimp</b>,the same escape character must be specified in the <b>dbimp</b>command.</font></p><p>The <b>-r</b> option causes <b>dbexp</b> to include the record'sdatabase address (see section 14.2.2, "Data File Organization") asthe first field in each ASCII record. For example:</p><pre><font color="#0000FF">dbexp -r tims info</font></pre><p><font size="2">will cause the records to appear asfollows:</font></p><pre><font color="#0000FF">1:3,"db001","Database Design","McGraw Hill","1983",01:7,"db002","An Intro...","Addison Wesley","1981",01:20,"db005","Fundamental ...","Yourdan","1981",0</font></pre><p><font size="2">Since the database address of each record isunique, this field can be used as a unique identifier of eachrecord. The import utility can take advantage of thisfield.</font></p><p><font size="2">The <b>-m</b> option causes set membershipinformation to be included with each record. This informationconsists of a list of database addresses, which are placed afterthe record's own database address (if <b>-r</b> is specified) andbefore the ASCII data fields. One database address field is createdfor each set for which this record type is a member. The value ofeach field is the database address of the owner record for thatset. If the record is not connected to the set, the field value iszero.</font></p><p><font size="2">The <b>-n</b> option is a silent option; there isno output to <b>stdout</b>. The <b>-d</b> option causes a databaseaddress to be printed as long integers, rather than in the standardformat. Note that a database address may appear as a very largenumber (the maximum is 4,294,967,295) because of the internalformat of a database address.</font></p><p><font size="2">The order of the database address fields is thesame as the order of the sets in the DDL. If a record is a memberof three sets, three database addresses would be included in eachASCII record. The first database address would represent the ownerof the first set in the schema that lists this record type as amember. This ordering information is important when using theimport utility to recreate the set connections, because it can beused to create a logical connection between fields with identicalvalues.</font></p><p><font size="2">The command below:</font></p><pre><font color="#0000FF">dbexp -r -m tims info</font></pre><p><font size="2">would create the following:</font></p><pre><font color="#0000FF">1:18,1:1,NULL,"db001","Fourth-Gen...","Prentice-Hall","1985",01:19,1:1,NULL,"db002","Fourth-Gen...","Prentice-Hall","1986",01:20,1:2,NULL,"sw001","Software Rel...","Wiley-Interscience","1976",0</font></pre><p><font size="2">The <b>info</b> record type is a member of twosets. This data has been connected in the first set(<b>has_published</b>), but not in the second (<b>articles</b>), asindicated by the <b>NULL</b> in the third field.</font></p><h2><a name="ExportingXML" id="ExportingXML"></a>10.2.1 ExportingXML</h2><p><font size="2"><b>dbexp</b> can also be used to produce a singleXML document containing all instances of selected recordtypes, or the entire database. The XML format produces a largerfile than text file export, but it is better able to represent thestructure and and set relationships of the data.</font></p><p><font size="2">The <b>-xml</b> option tells <b>dbexp</b> toexport data in XML format instead of text file format. Asingle XML file named after the database will be produced in thecurrent directory.</font></p><p><font size="2">An XML schema can be generated inDTD format, describing the structure of the database. The XMLschema is dynamically generated from the database schema and can beused to validate the XML file produced by<b>dbexp</b>.</font></p><p><font size="2">To generate a DTD, use the <b>-dtd</b> option of<b>dbexp</b>. A DTD file named after the database will be created.This file need only be regenerated if the database schema ischanged.</font></p><p><font size="2">The structure of the XML document closely followsthe database schema. The following example database schema stores alist of authors and information about the books they havewritten:</font></p><pre><font color="blue">database example {</font><font color="blue"> data file "example.d01" contains system, author, info;</font><font color="blue"> record author {</font><font color="blue"> char name[32];</font><font color="blue"> struct {</font><font color="blue"> short day;</font><font color="blue"> short month;</font><font color="blue"> short year;</font><font color="blue"> } date_of_birth;</font><font color="blue"> }</font><font color="blue"> record info {</font><font color="blue"> char title[80];</font><font color="blue"> }</font><font color="blue"> set author_list {</font><font color="blue"> order ascending;</font><font color="blue"> owner system;</font><font color="blue"> member author by name;</font><font color="blue"> }</font><font color="blue"> set has_published {</font><font color="blue"> order ascending;</font><font color="blue"> owner author;</font><font color="blue"> member info by title;</font><font color="blue"> }</font><font color="blue">}</font></pre><p><font size="2">The following XML document would be produced fromdata stored in such a database:</font></p><pre><font color="blue"><EXAMPLE> <AUTHOR_LIST> <AUTHOR idref="DB_ADDR:0:2"/> </AUTHOR_LIST> <AUTHOR id="DB_ADDR:0:2"> <HAS_PUBLISHED> <INFO idref="DB_ADDR:0:4"/> <INFO idref="DB_ADDR:0:3"/> </HAS_PUBLISHED> <NAME>Shel Silverstein</NAME> <DATE_OF_BIRTH> <DAY>25</DAY> <MONTH>9</MONTH> <YEAR>1930</YEAR> </DATE_OF_BIRTH> </AUTHOR> <INFO id="DB_ADDR:0:3"> <TITLE>Where the Sidewalk Ends</TITLE> </INFO> <INFO id="DB_ADDR:0:4"> <TITLE>The Giving Tree</TITLE> </INFO></EXAMPLE></font></pre><p><font size="2">Each record in the database is listed at thehighest level of the XML document, along with a unique <b>id</b>attribute based on the database address of the record. Fields arerepresented by tags within the record, and structures are nestedappropriately.</font></p><p><font size="2">Each set relationship for which the record is anowner is listed before the record's fields. Inside this is a listof set members in the exact order that they were stored in thedatabase. Because these records can usually be found elsewhere inthe XML file, an <b>idref</b> attribute is used to reference themember record. Set relationships owned by the system record arelisted at the highest level of the XML document, outside any recordtags. The relationship between unique <b>id</b> attributes and<b>idref</b> attributes is enforce by the DTD schema.</font></p><p><font size="2">Fields of the special DB_ADDR field type will
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -