home *** CD-ROM | disk | FTP | other *** search
/ RISC DISC 1 / RISC_DISC_1.iso / pd_share / apps / powerbase / Docs / CSVFiles < prev    next >
Encoding:
Text File  |  1994-10-01  |  13.6 KB  |  248 lines

  1.                                 CSV Files
  2.                                 =========
  3. What are CSV files? 
  4. =================== 
  5. CSV stands for "comma-separated value" 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. standard for such files does not appear to be very rigorously defined and
  10. the following variants may be encountered:-
  11.  
  12. (1) Data which is non-numeric (e.g. ordinary text items such as names and
  13. addresses) are usually enclosed in quotation marks ("") whereas numeric
  14. items are not. This makes it possible for an application which is reading a
  15. CSV file to distinguish between numbers and strings (which might of course
  16. contain numerals) and also allows the comma to be used as a character within
  17. a string without being mistaken for a data separator. (Addresses often have
  18. commas in them, e.g. 112, Keighley Road.)
  19.  
  20. (2) Null data items are usually included and can be located by looking for
  21. two commas with either nothing in between them or with only two quotation
  22. marks between them, i.e. ,, or ,"",. Each line of data (record) in such a
  23. file will always contain the same number of data items (fields). Nulls may,
  24. however, be omitted entirely in which case the number of fields per record
  25. will vary.
  26.  
  27. (3) The way in which each record is terminated tends to vary from one system
  28. to another. The last item of data in a record is not followed by a comma but
  29. by an invisible character or pair of characters called the line terminator.
  30. On RISC OS systems this is the "line-feed" character (ASCII value 10) and on
  31. PCs it is usually the "carriage-return" character (ASCII value 13). You
  32. might, however, encounter CSV files in which BOTH these characters are used,
  33. i.e. LF CR or CR LF.
  34.  
  35. (4) The separator isn't always even a comma. The "TAB" character (ASCII
  36. value 9) is often used and such files are called TSV files, but characters
  37. such as the semicolon are sometimes used too.
  38.  
  39. (5) Some applications which accept a CSV file as input expect the first
  40. record 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 "NAME","STREET","TOWN","COUNTY","POSTCODE".
  43.  
  44. How does Powerbase use CSV files?
  45. =================================
  46. Powerbase is able to save data in the form of true CSV files or files using
  47. some other data separator. If the separator is indeed a comma then the file
  48. created is of type &dfe and its icon displays the letters "CSV". If some
  49. other separator, such as TAB, is used (see (4) above) then the file is of
  50. the ordinary "text" type, i.e. &fff. Files of these types may also be used
  51. to enter data into a Powerbase database. In what follows we will, for
  52. convenience, refer to them as "CSV files" whether or not the separator is a
  53. comma. The choices on the main menu which control these actions are 'Export
  54. CSV' and 'CSV options'.
  55.  
  56. CSV options
  57. ===========
  58. This leads to a dialogue box which allows you to specify all the file
  59. characteristics described in the previous section. Thus, selection of the
  60. first three option buttons causes a saved file to have, respectively:-
  61.  
  62.         (a) quotes round non-numeric fields (see (1) above)
  63.         (b) a header record specifying the field names (see (5) above)
  64.         (c) null fields included (see (2) above).
  65.  
  66. Note that the "field names" referred to in (b) are, by default, the TAGS of
  67. the corresponding Powerbase fields, but may be changed to the DESCRIPTORS by
  68. altering the settings in the 'Print options' dialogue box.
  69.  
  70. The field separator and record terminator may be changed by means of menus
  71. which appear when you click on the right-pointing arrows. Each of these
  72. menus also allows you to define your own separator and/or terminator of one
  73. or two printing characters.
  74.  
  75. Two buttons in this dialogue box have not yet been referred to. Their
  76. function is described later (Using CSV files to import data).
  77.  
  78. Export CSV
  79. ==========
  80. Having set up your options as described above, creating a CSV file is very
  81. like printing a list (see 'Printing'). First highlight the fields to be
  82. exported by clicking on each with ADJUST. Remember that the order in which
  83. the fields are highlighted is important. Next follow the 'Export CSV' option
  84. to the dialogue box and type in the search formula to determine which
  85. records are exported. Finally, enter the name of the file and drag the file
  86. icon to a filer window. The hourglass appears showing the progress of the
  87. operation.
  88.  
  89. Experiment with saving CSV files with different settings of the CSV options
  90. and then loading the resulting files into Edit to examine them.
  91.  
  92. Using CSV files to import data
  93. ==============================
  94. If you drag a CSV file onto the record display of an open database the
  95. options window (see CSV options) appears with a changed title and some
  96. additional icons, one of which displays the pathname of the CSV file. The
  97. same thing happens with a suitably-constituted text file (see above: 'How
  98. does Powerbase use CSV files'). Please note, however, that there are other
  99. circumstances under which a text file might be dropped on the record window.
  100. A properly-written script file (see 'ScriptFile') would be recognised as
  101. such, but a text file dropped onto the appropriate type of External field
  102. (i.e. a 'Text' or 'Text Block') field would become linked to that field
  103. instead of being treated like a CSV file. If you are importing data from a
  104. plain text file and your record contains fields of the aforementioned types
  105. be sure to drop the file on the window background, not on the external
  106. field. You are strongly advised to use proper CSV files if at all possible.
  107.  
  108. There is an option to display each record as it is imported. The process is
  109. faster with this option turned off but much less informative. Most people
  110. like to know how things are progressing. 
  111.  
  112. Another option causes any trailing spaces in the imported data-fields to be
  113. stripped. Some database programs appear to pad all fields to their maximum
  114. length by adding spaces to the end of the data where necessary. If the
  115. program doesn't allow you to get rid of these spaces when exporting data in
  116. CSV format they can be a nuisance when the file is imported into Powerbase.
  117. Queries, for instance, may not work properly unless you use the "contains"
  118. comparator ({) rather than "equals". You can easily recognise this situation
  119. since the caret will always be at the far right of the field even though the
  120. visible characters nowhere near fill the field. Setting the "Strip spaces"
  121. switch before importing the file overcomes the problem.
  122.  
  123. Clicking on 'GO' will make Powerbase try to create new Powerbase records
  124. from the data. If you decide not to do this you should click on 'Cancel'. If
  125. you do wish to import the data there are some important points to consider:-
  126.  
  127. (1) The options need to be correctly set up. This means that if the CSV file
  128. originally came from a Powerbase application the settings in the options
  129. window should be exactly the same as they were when the file was exported.
  130. The exception to this is the 'Quotes' button which is greyed out on import
  131. because Powerbase doesn't need it. If the CSV file came from a PC or another
  132. RISC OS application you might have to load it into Edit to find out what
  133. separator and terminator are used, whether it uses quotes etc.
  134.  
  135. (2) Will the data be read into the correct Powerbase fields? If no fields on
  136. the record screen are highlighted (i.e. with ADJUST) and the CSV file does
  137. not contain a header record then the answer is as follows:-
  138.  
  139.         (a) The first CSV field will be read into the first WRITABLE
  140.         Powerbase field or Check box field. Non-writable fields such as
  141.         Text blocks, Pictures, Buttons or fields which are merely labels
  142.         will be ignored. The next CSV field will be read into the second
  143.         Powerbase field and so on.
  144.  
  145.         (b) If the end of the CSV record is reached before all the writable
  146.         fields have been filled then the next CSV record will start a new
  147.         Powerbase record, i.e. the reading won't get out of step. It does not
  148.         matter, therefore, if the CSV file omits null items.
  149.  
  150.         (c) If all writable fields are filled in before reaching the end of
  151.         the CSV record Powerbase ignores the remainder of the latter and
  152.         skips to the beginning of the next CSV record before starting a new
  153.         Powerbase record. This is also to keep the operation in step.
  154.  
  155. There will be occasions when you don't want to fill the Powerbase fields
  156. sequentially as just described. There are two ways of making the process
  157. more specific:-
  158.  
  159.         (a) If you highlight Powerbase fields with ADJUST before starting the
  160.         import the data will be read only into the highlighted fields. The
  161.         order in which the fields are filled is the order in which you
  162.         highlighted them. Paragraphs (b) and (c) above apply in cases where
  163.         the CSV record has more or fewer fields than are highlighted on the
  164.         Powerbase record. In other words the operation proceeds as if the
  165.         highlighted fields were the only ones in the record and as if they
  166.         actually occurred in the order in which they were highlighted.
  167.  
  168.         (b) If the CSV file has a header which contains the tags or
  169.         descriptors of fields in the Powerbase record then the import behaves
  170.         exactly as if those fields were highlighted. The 'Print options'
  171.         dialogue box must correctly reflect whether the tags or descriptors
  172.         of fields are used. Note that you should not use both a header AND
  173.         highlighting. Note also that there is nothing to prevent you adding
  174.         a header manually to a CSV file which did not originate from a
  175.         Powerbase application.
  176.  
  177. What if the data won't fit?
  178. ===========================
  179. This is really two questions. If the database doesn't contain enough free
  180. records to hold all the imported CSV data you may get a "Database full when
  181. reading CSV file" error. There are two ways of avoiding this. The obvious
  182. one is to make sure the database is big enough before you start. If it isn't
  183. you can expand it via the 'Change length' dialogue box accessible from the
  184. 'Utilities' submenu on the icon bar menu. That dialogue box also has an
  185. 'Increment for expansion' which, if greater than zero, causes the database
  186. to expand automatically whenever it becomes full. Only if the database
  187. becomes full when the increment is set to zero should the aforementioned
  188. error occur.
  189.  
  190. The second sense in which data might not fit is if an item of data is too
  191. long for the database field into which you are trying to import it. When a
  192. CSV file is being read Powerbase maintains a file called 'TooBig' inside the
  193. database's 'PrintJobs' directory. Anything read from the CSV file which
  194. won't fit in the target field is written to this file together with
  195. information about where it was intended to go. No writable Powerbase field
  196. may be longer than 246 characters and if an item of imported data exceeds
  197. this a note will be made in the 'TooBig' file advising you to define an
  198. External field (Text Block or Text) for such data.
  199.  
  200. Advanced features of CSV import
  201. ===============================
  202. When you import a CSV file a new Powerbase record is normally created to
  203. accommodate each CSV record read from the file. You may also, subject to
  204. certain restrictions, use a CSV file to enter data into EXISTING Powerbase
  205. records. 
  206.  
  207. The CSV options window contains a button which, if selected, causes each
  208. record of a saved CSV file to include the primary key of the Powerbase
  209. record. The key is placed at the beginning of the record. Try creating CSV
  210. files with and without this option set and compare the files using Edit. If
  211. a file containing primary keys is dragged into a Powerbase record window
  212. Powerbase will attempt to locate the records with the same primary keys as
  213. the records in the CSV file. If the key is not found the CSV record is
  214. simply ignored. If it is found then the CSV data goes into the same record,
  215. overwriting any data which the target fields already contain.
  216.  
  217. The operation described needs using with care. It is only really useful for
  218. transferring data between Powerbase databases which have the same primary
  219. key field. It can also lead to trouble if the primary key isn't unique,
  220. since the record located by Powerbase and into which the CSV data is read
  221. might not be the correct one. You have been warned!
  222.  
  223. Finally, there is one option button in the CSV options window which only
  224. becomes active when the 'Header' button is selected. This is called 'With
  225. field data'. When selected the header of a saved CSV file contains not only
  226. the field names but also the field lengths and types. Again it is suggested
  227. that you create such a file and look at it in Edit. Each item in the header
  228. begins with a number. This is the defined field length in characters, i.e.
  229. the maximum allowed length in the Powerbase record field. Next comes a ¤
  230. character which separates the length from the field name. Another ¤
  231. separates the field name from the concluding number which is the field type.
  232. (The type numbers may be inspected by looking at the 'ValStrings' file in
  233. the Powerbase directory.)
  234.  
  235. A file created in this form is NOT meant to be dropped on the window of an
  236. open database. It should be dropped on the Powerbase icon on the icon bar
  237. when no database is open. Powerbase will attempt to convert the file into a
  238. functioning database. All the fields will be ranged on the left of the
  239. record window, one beneath another and the primary key will consist of the
  240. first four characters of the first field. Don't try to create databases
  241. containing Check box, Button or External fields using this method; it's only
  242. meant for the most basic type of database. 
  243.  
  244.  
  245.       
  246.  
  247.  
  248.