{"id":147,"date":"2017-01-04T20:14:03","date_gmt":"2017-01-04T20:14:03","guid":{"rendered":"https:\/\/reldb.org\/c\/?page_id=147"},"modified":"2017-01-04T23:48:46","modified_gmt":"2017-01-04T23:48:46","slug":"accessing-external-data-sources","status":"publish","type":"page","link":"https:\/\/reldb.org\/c\/index.php\/read\/accessing-external-data-sources\/","title":{"rendered":"Accessing External Data Sources"},"content":{"rendered":"<p>As of\u00a0<em>Rel<\/em> version 3.003, it&#8217;s easy to read external data sources like CSV files, XLS\/XLSX spreadsheets, tables in Microsoft Access databases, and tables in external databases accessible via JDBC. You can also write to CSV files and XLS\/XLSX spreadsheet files.<\/p>\n<h2>Reading External Data Sources<\/h2>\n<p>Data sources are read by defining\u00a0them as\u00a0<em>external<\/em> relation-valued variables.<\/p>\n<p>External relation-valued variables, or external relvars, are defined via a\u00a0<em>Rel<\/em> extension to\u00a0<strong>Tutorial D<\/strong>. The VAR variable declaration supports the following syntax:<\/p>\n<blockquote><p>VAR &lt;varname&gt; EXTERNAL &lt;kind&gt; &lt;connection_string&gt; [ DUP_REMOVE | DUP_COUNT | AUTOKEY ];<\/p><\/blockquote>\n<p><em>&lt;kind&gt;<\/em> is the kind of external relvar. Built-in are CSV, XLS, JDBC and ACCDB. (Additional kinds, and additional JDBC drivers, can be put in the Extensions directory of a Rel database but are only recognised by the standalone Rel DBMS.)<\/p>\n<p><em>&lt;connection_string&gt;<\/em> is a CHAR specific to the kind of external relvar that describes the path or connection to the data source.<\/p>\n<p><em>DUP_REMOVE<\/em> silently removes duplicate tuples.<\/p>\n<p><em>DUP_COUNT<\/em> adds an INTEGER attribute named _DUP_COUNT that indicates a count of duplicate tuples.<\/p>\n<p><em>AUTOKEY<\/em> adds an INTEGER attribute named _AUTOKEY that is serially numbered. In the absence of DUP_REMOVE, DUP_COUNT, or AUTOKEY, the default is AUTOKEY.<\/p>\n<p>Examples:<\/p>\n<blockquote><p>VAR myvar EXTERNAL CSV &#8220;\/home\/dave\/test.csv&#8221;;<\/p>\n<p>VAR myvar EXTERNAL CSV &#8220;\/home\/dave\/test.csv,HOHEADING&#8221; DUP_REMOVE;<\/p>\n<p>VAR myvar EXTERNAL XLS &#8220;\/home\/dave\/test.xls&#8221; DUP_REMOVE;<\/p>\n<p>VAR myvar EXTERNAL XLS &#8220;\/home\/dave\/test.xlsx&#8221; DUP_COUNT;<\/p>\n<p>VAR myvar EXTERNAL XLS &#8220;\/home\/dave\/test.xlsx,1&#8221; DUP_COUNT;<\/p>\n<p>VAR myvar EXTERNAL XLS &#8220;\/home\/dave\/test.xls,2,NOHEADING&#8221; DUP_REMOVE;<\/p>\n<p>VAR myvar EXTERNAL JDBC &#8220;jdbc:postgresql:\/\/localhost\/database,sqluser,sqluserpw,MyTable&#8221; AUTOKEY;<\/p>\n<p>VAR myvar EXTERNAL ACCDB &#8220;c:\\\\users\\\\me\\\\mydb.accdb,mytable&#8221;;<\/p>\n<p>VAR myvar EXTERNAL ACCDB &#8220;c:\\\\users\\\\me\\\\mydb.accdb,mytable&#8221; DUP_REMOVE;<\/p><\/blockquote>\n<p>NOTE: In the XLS connection string, the number after the optional &#8216;,&#8217; indicates the zero-based tab number.<\/p>\n<p>NOTE: In the XLS &amp; CSV connection string, NOHEADING indicates that the spreadsheet does not have a heading row.<\/p>\n<p>The\u00a0<em>Rel<\/em> command-line interface can help you specify the appropriate file name. Use the &#8220;Get file path&#8221; toolbar item to find and insert the file name. The button is shown, in a highlight box with a red circle, here:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-159\" src=\"https:\/\/reldb.org\/c\/wp-content\/uploads\/2017\/01\/Screenshot-2017-01-04-20.34.45.png\" alt=\"\" width=\"460\" height=\"534\" srcset=\"https:\/\/reldb.org\/c\/wp-content\/uploads\/2017\/01\/Screenshot-2017-01-04-20.34.45.png 460w, https:\/\/reldb.org\/c\/wp-content\/uploads\/2017\/01\/Screenshot-2017-01-04-20.34.45-258x300.png 258w\" sizes=\"auto, (max-width: 460px) 100vw, 460px\" \/><\/p>\n<p>Built-in JDBC support is provided for MySQL, MariaDB, PostgreSQL, Oracle Database and Microsoft SQL Server. E.g.:<\/p>\n<blockquote><p>VAR myvar EXTERNAL JDBC &#8220;jdbc:mysql:\/\/localhost\/database,sqluser,sqluserpw,MyTable&#8221;;<\/p>\n<p>VAR myvar EXTERNAL JDBC &#8220;jdbc:mariadb:\/\/localhost\/database,sqluser,sqluserpw,MyTable&#8221;;<\/p>\n<p>VAR myvar EXTERNAL JDBC &#8220;jdbc:postgresql:\/\/localhost\/database,sqluser,sqluserpw,MyTable&#8221;;<\/p>\n<p>VAR myvar EXTERNAL JDBC &#8220;jdbc:oracle:thin:@localhost:1521:database,sqluser,sqluserpw,MyTable&#8221;;<\/p>\n<p>VAR myvar EXTERNAL JDBC &#8220;jdbc:sqlserver:\/\/localhost:1433;databaseName=database,sqluser,sqluserpw,MyTable&#8221;;<\/p><\/blockquote>\n<h2>Writing to CSV and Spreadsheet Files<\/h2>\n<p>The\u00a0<em>Rel<\/em> user interface supports exporting relations from variables (relvars), views, and queries to CSV (comma separated value) and XLS or XLSX spreadsheet files.<\/p>\n<p>To export from variables and views, right-click on the variable or view name to bring up a menu, as shown here:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-150\" src=\"https:\/\/reldb.org\/c\/wp-content\/uploads\/2017\/01\/ExportMenuItem.png\" alt=\"\" width=\"579\" height=\"530\" srcset=\"https:\/\/reldb.org\/c\/wp-content\/uploads\/2017\/01\/ExportMenuItem.png 579w, https:\/\/reldb.org\/c\/wp-content\/uploads\/2017\/01\/ExportMenuItem-300x275.png 300w\" sizes=\"auto, (max-width: 579px) 100vw, 579px\" \/><\/p>\n<p>Select the Export menu item. The following dialog box will appear:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-149\" src=\"https:\/\/reldb.org\/c\/wp-content\/uploads\/2017\/01\/ExportChoices.png\" alt=\"\" width=\"737\" height=\"338\" srcset=\"https:\/\/reldb.org\/c\/wp-content\/uploads\/2017\/01\/ExportChoices.png 737w, https:\/\/reldb.org\/c\/wp-content\/uploads\/2017\/01\/ExportChoices-300x138.png 300w\" sizes=\"auto, (max-width: 737px) 100vw, 737px\" \/><\/p>\n<p>Once you&#8217;ve chosen the appropriate file format, press the Export button to choose a filename and export the file.<\/p>\n<p>You can also export CSV and XLS\/XLSX files from the Rev Visual Query Language. Select the &#8220;Rev&#8221; mode (note that the Rev extensions must be installed) and load or define a query like the following and click on the &#8216;?&#8217; of any query node, as shown with a red circle here:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-153\" src=\"https:\/\/reldb.org\/c\/wp-content\/uploads\/2017\/01\/RevVQLShowButton.png\" alt=\"\" width=\"823\" height=\"602\" srcset=\"https:\/\/reldb.org\/c\/wp-content\/uploads\/2017\/01\/RevVQLShowButton.png 823w, https:\/\/reldb.org\/c\/wp-content\/uploads\/2017\/01\/RevVQLShowButton-300x219.png 300w, https:\/\/reldb.org\/c\/wp-content\/uploads\/2017\/01\/RevVQLShowButton-768x562.png 768w\" sizes=\"auto, (max-width: 823px) 100vw, 823px\" \/><\/p>\n<p>That will pop up a dialog box that shows you the\u00a0<strong>Tutorial D<\/strong> code for the query, along with four option buttons, as shown here:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-151\" src=\"https:\/\/reldb.org\/c\/wp-content\/uploads\/2017\/01\/RevVQLDetail.png\" alt=\"\" width=\"671\" height=\"459\" srcset=\"https:\/\/reldb.org\/c\/wp-content\/uploads\/2017\/01\/RevVQLDetail.png 671w, https:\/\/reldb.org\/c\/wp-content\/uploads\/2017\/01\/RevVQLDetail-300x205.png 300w\" sizes=\"auto, (max-width: 671px) 100vw, 671px\" \/><\/p>\n<p>You can then click the &#8220;Export query results to file&#8221; button&#8230;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-152\" src=\"https:\/\/reldb.org\/c\/wp-content\/uploads\/2017\/01\/RevVQLDetailExport.png\" alt=\"\" width=\"671\" height=\"459\" srcset=\"https:\/\/reldb.org\/c\/wp-content\/uploads\/2017\/01\/RevVQLDetailExport.png 671w, https:\/\/reldb.org\/c\/wp-content\/uploads\/2017\/01\/RevVQLDetailExport-300x205.png 300w\" sizes=\"auto, (max-width: 671px) 100vw, 671px\" \/><\/p>\n<p>&#8230;to choose a file format and export the results of evaluating the query.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As of\u00a0Rel version 3.003, it&#8217;s easy to read external data sources like CSV files, XLS\/XLSX spreadsheets, tables in Microsoft Access databases, and tables in external databases accessible via JDBC. You can also write to CSV files and XLS\/XLSX spreadsheet files. Reading External Data Sources Data sources are read by defining\u00a0them as\u00a0external relation-valued variables. External relation-valued <a href=\"https:\/\/reldb.org\/c\/index.php\/read\/accessing-external-data-sources\/\" rel=\"nofollow\"><span class=\"sr-only\">Read more about Accessing External Data Sources<\/span>[&hellip;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":149,"parent":17,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":"","_links_to":"","_links_to_target":""},"class_list":["post-147","page","type-page","status-publish","has-post-thumbnail","hentry"],"_links":{"self":[{"href":"https:\/\/reldb.org\/c\/index.php\/wp-json\/wp\/v2\/pages\/147","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/reldb.org\/c\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/reldb.org\/c\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/reldb.org\/c\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/reldb.org\/c\/index.php\/wp-json\/wp\/v2\/comments?post=147"}],"version-history":[{"count":7,"href":"https:\/\/reldb.org\/c\/index.php\/wp-json\/wp\/v2\/pages\/147\/revisions"}],"predecessor-version":[{"id":238,"href":"https:\/\/reldb.org\/c\/index.php\/wp-json\/wp\/v2\/pages\/147\/revisions\/238"}],"up":[{"embeddable":true,"href":"https:\/\/reldb.org\/c\/index.php\/wp-json\/wp\/v2\/pages\/17"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/reldb.org\/c\/index.php\/wp-json\/wp\/v2\/media\/149"}],"wp:attachment":[{"href":"https:\/\/reldb.org\/c\/index.php\/wp-json\/wp\/v2\/media?parent=147"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}