1 Loading a CSV File from a Website

In the following example, we have:

  • A website with a list of all nations and their 2-digit codes (ISO 3166-2).
  • A download URL in CSV format.
    Name,Code
    Afghanistan,AF
    Åland Islands,AX
    Albania,AL
    Algeria,DZ
    American Samoa,AS
    Andorra,AD
    Angola,AO
    Anguilla,AI
    Antarctica,AQ
    Antigua and Barbuda,AG
    ...
    

In this example, we would like to load remote CSV data in our local database's temporary table.

Copy
<xsql-script>
    <body>
        <drop table='iso_countries' temp='yes' onexception='ignore' />
        <table name='iso_countries' temp='yes'>
            <column name='name' type='char' size='40' />
            <column name='code' type='char' size='2' />
        </table>
        
        <table.load table='iso_countries' delimiter=',' firstRowIsHeader='true'>
            <!-- convert to ASCII to avoid encoding problems with special UTF-8 chars -->
            <string.toASCII>
            <!-- Read data from connection -->
            <http.connection.read>
                <!-- Open URL -->
                <http.connection url='https://raw.githubusercontent.com/datasets/country-list/master/data.csv' />
            </http.connection.read>
            </string.toASCII>
        </table.load>
        
        <print.sql>
            <select>
                <columns>*</columns>
                <from table='iso_countries' />
            </select>
        </print.sql>
   </body>
</xsql-script>