home *** CD-ROM | disk | FTP | other *** search
/ APDL Public Domain 1 / APDL_PD1A.iso / database / powerbase / docs / csvfiles < prev    next >
Encoding:
Text File  |  1995-08-21  |  12.7 KB  |  241 lines

  1. Ch 8 − Using CSV files
  2.  
  3. 8.1  What are CSV files?
  4.  
  5. CSV stands for “comma-separated values” and is the name given to data files
  6. in which each item of data is separated from the next by a comma. Such files
  7. are widely used to transfer data from one application to another, e.g. from
  8. a database to a spreadsheet or from a RISC OS database to a PC database. The
  9. following points should be noted:− 
  10.  
  11. (1)     Data items which are non-numeric (e.g. plain text items such as
  12. names and addresses) are often enclosed in double quotes (“”) whereas
  13. numeric items are not. This makes it possible for an application reading a
  14. CSV file to distinguish between numbers and strings (which might of course
  15. contain numerals) and also allows a comma to be used as a character within a
  16. string without being mistaken for a data separator. (Addresses often contain
  17. commas e.g. 112, Keighley Road). For many purposes the quotes aren’t
  18. necessary and may be omitted.
  19.  
  20. (2)     Null data items are usually included and can be located by looking
  21. for two commas with either nothing in between them or with only two
  22. quotation marks between them, i.e. ,, or ,“”,. Each line of data (record) in
  23. such a file will always contain the same number of data items (fields).
  24. Powerbase does, however, allow nulls to be omitted entirely when creating a
  25. file in which case the number of fields per record will vary. 
  26.  
  27. (3)     The way in which each record is terminated varies from one system to
  28. another. The last item of data in a record is not followed by a comma but by
  29. a line terminator. On RISC OS systems this is the line-feed character (ASCII
  30. value 10) and on PCs it is usually the carriage-return character (ASCII
  31. value 13). You might, however, encounter CSV files in which both these
  32. characters are used, i.e. LF CR or CR LF. 
  33.  
  34. (4)     Separator other than commas are sometimes used. The Tab character
  35. (ASCII value 9) is often used and such files are called TSV files. Powerbase
  36. allows you to separate data items and terminate lines (see (3) above) with
  37. any character or character pair.
  38.  
  39. (5)     Some applications which accept a CSV file as input expect the first
  40. line to contain the names of the fields which comprise the subsequent
  41. records, e.g. if each record consists of a name and a four-part address this
  42. header record might read:−
  43.  
  44.  “NAME”,“STREET”,“TOWN”,“COUNTY”,“POSTCODE” 
  45.  
  46. Powerbase is able to save data in the form of true CSV files or files using
  47. another data separator. If the separator is a comma then the file created is
  48. of type &dfe and its icon displays the letters CSV. If another separator,
  49. eg. Tab, is used (see (4) above) then the file is of the ordinary Text type,
  50. i.e. &fff. Files of these types may also be used to enter data into a
  51. Powerbase database. In what follows we will, for convenience, refer to them
  52. all as “CSV files” whether or not the separator is a comma. The choices on
  53. the main menu which control these actions are Export CSV and CSV options. 
  54.  
  55. 8.2  Setting the CSV options
  56.  
  57. The main-menu choice CSV options leads to a dialogue box which lets you
  58. specify all the file characteristics described earlier. Selection of the
  59. first three option buttons causes a saved file to have, respectively:−
  60.  
  61. •       quotes round non-numeric fields (see (1) above)
  62.  
  63. •       a header record specifying the field names (see (5) above).
  64.  
  65. •       null fields included (see (2) above).
  66.  
  67. Note that the field names referred to in (b) are, by default, the tags of
  68. the corresponding Powerbase fields, but may be changed to the descriptors by
  69. altering the setting in the Print options window. The field separator and
  70. record terminator may be changed by means of menus which appear when you
  71. click on the pop-up menu icons. Each of these menus includes a writable
  72. entry allowing you to define your own separator and/or terminator of one or
  73. two printing characters. 
  74.  
  75. 8.3  Exporting data as a CSV file
  76.  
  77. Having set up your options as described above, creating a CSV file is very
  78. like printing a list. First highlight the fields to be exported by clicking
  79. on each with ADJUST. Remember that the order in which the fields are
  80. highlighted is important. Next choose Export CSV from the main menu. A
  81. window appears featuring the Query panel. Type in the search formula to
  82. determine which records are exported. Finally, enter the name of the file
  83. and drag the file icon to a filer window. By default the file is saved in
  84. PrintJobs as usual, and you may simply click on Export or type Return.
  85. Experiment with saving CSV files with different settings of the CSV options
  86. and then loading the resulting files into Edit to examine them. 
  87.  
  88. 8.4  Using CSV files to import data
  89.  
  90. If you drag a CSV file onto the record display of an open database the
  91. options window (see CSV options) appears with a changed title and some
  92. additional icons, one of which displays the pathname of the CSV file. 
  93. Clicking on Import will make Powerbase try to create new database records
  94. from the file. If you decide not to do this you should click on Cancel. If
  95. you do wish to import the data there are some important consideration which
  96. will now be explained.
  97.  
  98. 8.4.1 Ensuring that the correct options are selected
  99.  
  100. If the CSV file originally came from a Powerbase application the settings in
  101. the CSV options window should be exactly the same as they were when the file
  102. was exported. The exception to this is the Quotes button which is shaded on
  103. import because Powerbase doesn’t need it. If the CSV file came from a PC or
  104. another RISC OS application you might have to load it into Edit to find out
  105. what separator and terminator are used.
  106.  
  107. There are two more option switches at the bottom of the window. One causes
  108. each record to be displayed as it is imported. The import process is slower
  109. with this turned on but much more informative if you like to know how things
  110. are progressing. 
  111.  
  112. The other option strips any trailing spaces in the imported data-fields.
  113. Some database programs pad all fields to their maximum length by adding
  114. spaces to the end of the data where necessary.  If you import such a file
  115. into Powerbase you will find that the caret will always be at the far right
  116. of the field even though visible characters do not fill the field, and some
  117. queries won’t work properly. Setting the Strip spaces switch before
  118. importing the file overcomes the problem. 
  119.  
  120. 8.4.2 Directing imported data to the correct fields
  121.  
  122. If no fields on the record screen are highlighted (i.e. with ADJUST) and the
  123. CSV file does not contain a header record (see 8.1) then the import process
  124. proceeds according to the following rules:− 
  125.  
  126. •       The first CSV field will be read into the first Powerbase field for
  127. which importing is allowed (Graphics fields, Buttons or fields which are
  128. merely labels will be ignored). The next CSV field will be read into the
  129. second Powerbase field and so on. 
  130.  
  131. •       If the end of the CSV record is reached before all the relevant
  132. fields have been filled (data underflow) then the next CSV record will start
  133. a new Powerbase record, i.e. the reading won’t get out of step. It does not
  134. matter, therefore, if the CSV file omits null items at the end of a line. 
  135.  
  136. •       If all relevant fields are filled before reaching the end of the CSV
  137. record (data overflow) Powerbase ignores the remainder of the line and skips
  138. to the beginning of the next CSV record before starting a new Powerbase
  139. record. This is also to keep the operation in step. 
  140.  
  141. There will be occasions when you don’t want to fill the Powerbase fields
  142. sequentially as just described. There are two ways of making the process
  143. more specific:− 
  144.  
  145. •       Highlight the required Powerbase fields with ADJUST before starting
  146. the import.  Data will then be read only into the highlighted fields, all
  147. other fields being ignored. The order in which the fields are filled is the
  148. order in which you highlighted them.  The rules given above about underflow
  149. and overflow of data still apply.
  150.  
  151. •       Give the file a header record containing the tags or descriptors of
  152. required fields in the Powerbase record.  (There is nothing to prevent you
  153. manually adding such a header to a CSV file which did not originate from a
  154. Powerbase application.) Importing then occurs just as if those fields were
  155. highlighted. The Print options window must reflect whether the tags or
  156. descriptors of fields are used. 
  157.  
  158. Do not use both a header and highlighting. 
  159.  
  160. 8.4.3 Importing plain text files
  161.  
  162. It was previously explained that files created with Export CSV can have
  163. separators other than a comma. Such files will be of type &fff (plain text)
  164. instead of &dfe (CSV).  They can still be imported into a Powerbase database
  165. but a certain amount of caution is needed because there are other
  166. circumstances in which a text file might be dropped on the record window. A
  167. properly-written script file (see Ch 9) would be recognised as such and
  168. therefore cause no problem, but any text file dropped onto the appropriate
  169. type of External field (i.e. a Text or Text Block) field would become linked
  170. to that field instead of being treated like a CSV file. If you are importing
  171. data from a plain text file and your record contains fields of the
  172. aforementioned types be sure to drop the file on the window background, not
  173. on the External field. You are strongly advised to use proper CSV files if
  174. at all possible. 
  175.  
  176. 8.4.4 What if the imported data won’t fit?
  177.  
  178. There are two situations in which this can happen. The database might not
  179. contain enough free records to hold all the imported data and so you get a
  180. “Database full when reading CSV file” error. To avoid this either make sure
  181. the database is big enough before you start or place a suitable value in the
  182. Increment for expansion icon in the Change length window.  The latter is
  183. accessible from the Utilities submenu of the icon-bar menu.
  184.  
  185. The second situation is where an item is too long for the destined database
  186. field. When importing data Powerbase maintains a file called TooBig inside
  187. the database’s PrintJobs directory. Anything which won’t fit in the target
  188. field is written to this file together with information about where it was
  189. intended to go. No writable Powerbase field may be longer than 246
  190. characters and if an item of imported data exceeds this a note will be made
  191. in the TooBig file advising you to define an External field (Text Block or
  192. Text) for such data. 
  193.  
  194. 8.4.5 Advanced features of CSV import
  195.  
  196. By selecting a switch in the CSV options window you can make each record of
  197. a saved CSV file include the primary key of the Powerbase record. Try
  198. creating CSV files with and without this option set and compare the files
  199. using Edit. If such a file is dragged onto a record window Powerbase will
  200. attempt to locate records with the same primary keys as the records in the
  201. CSV file. If a matching key is not found the CSV record is simply ignored.
  202. If it is found then the CSV data goes into the same record, overwriting any
  203. data which the target fields already contain.  In other words it is possible
  204. to use CSV importing to modify existing records as opposed to creating new
  205. ones.
  206.  
  207. The operation described should be used with care. It is only useful for
  208. transferring data between Powerbase databases which have the same primary
  209. key field. It can lead to trouble if the primary key isn’t unique, since the
  210. record located by Powerbase, and into which the CSV data is read, might not
  211. be the correct one. You have been warned!
  212.  
  213. There is an option button in the CSV options window called With field data
  214. which only becomes active when the With header button is selected. With the
  215. switch selected the header record of a saved CSV file contains not only the
  216. field names but also the field lengths and types. Again it is suggested that
  217. you create a file and look at it in Edit. Each item in the header begins
  218. with a number. This is the field length in characters, i.e. the maximum
  219. allowed length in the Powerbase record field. Next comes a ¤ character which
  220. separates the length from the field name. Another ¤ separates the field name
  221. from the concluding number which determines the field type. (The type
  222. numbers may be inspected by looking at the ValStrings file in the Powerbase
  223. directory.) 
  224.  
  225. A file created in this form is not meant to be dropped onto the window of an
  226. open database. It should be dropped onto the Powerbase icon on the icon-bar
  227. when no database is open. Powerbase will attempt to convert the file into a
  228. functioning database. All the fields will be ranged on the left of the
  229. record window, one beneath another and the primary key will consist of the
  230. first four characters of the first field. Don’t try to create databases
  231. containing Check box, Button or External fields using this method; it’s only
  232. meant for the most basic type of database.
  233.  
  234. 8.5  CSV files and validation tables
  235.  
  236. You can save the contents of a validation table as a CSV file (see 5.6). 
  237. You can also import data into a validation table by dropping a CSV file onto
  238. it, the procedure being just the same as that described for importing into
  239. database records.
  240.  
  241.