Demo entry 5862484

mypythoncode

   

Submitted by anonymous on Aug 07, 2016 at 09:35
Language: JavaScript. Code size: 2.5 kB.

   //  To select the data from a given table id, 
   //  first the table id is requested with the "getElementById" function
   //  and then a query function is build and send to the next function as an argument
      function selectData() {
        var select_tableid = document.getElementById('select_tableid').value;
        console.log("select_tableid %s",select_tableid);
        if(select_tableid) {
          tableId = select_tableid;
        }
        query('SELECT * FROM ' + tableId);
      }
            

       // This function handles the whole query mojo
      function query(query) {
        var lowerCaseQuery = query.toLowerCase();
        var path = '/fusiontables/v2/query';
        var body = 'sql=' + encodeURIComponent(query);
        // function to handle the response and displaying it on the website
        var callback = function(element) {
          return function(resp) {
            var output = JSON.stringify(resp);
            document.getElementById(element).innerHTML = output;
            updateInnerHTMLFields();
          };
        }

       // in this branch any reading and writing requests are handled
        if (lowerCaseQuery.indexOf('select') == 0) {

         // in this else branch only reading requests "GET" are handled
          runClientRequest({
            path: path,
            params: {
              'sql': query
            }
          }, callback('select-data-output'));
          
        } else {
        
          // after creating an URI object from the string
          // the next auxiliary function is called  together with a writing request "POST"
          runClientRequest({
            path: path,
            body: body,
            headers: {
              'Content-Type': 'application/x-www-form-urlencoded'
            },
            method: 'POST'
          }, callback('insert-data-output'));
          
        }
        
      } // query()

      
      // Finally in this function the query request is send to Fusion Tables API
      // and the response is then handled in the callback function defined previously 
      function runClientRequest(args, callback) {
        gapi.client.request(args).execute(callback);
      }
      
      // This auxiliary function simply updates the text for the table id.
      function updateInnerHTMLFields(){
        document.getElementById('table-id-select').innerHTML = tableId;
        document.getElementById('table-id-insert').innerHTML = tableId;
      }

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).