12169

Express query for SQL

Question:

<h2>I am dynamically trying to pass the name of the table to SQL query in the part of express code below</h2>

<strong>Background Information::</strong>

<ul><li>What i am passing as a (key,value) is the string which will be the name of a table in sql database</li> <li>why am i doing is to dynamically select the table based on a dynamic client request</li> </ul>

<strong>Problem I am facing::</strong>

<ul><li>Clearly i am not sunig the sql query correctly</li> <li>How to solve this</li> </ul>

<strong>[ExpressCode]</strong>

app.get('/RestaurantDesc/:Key',function(request,response,next){ var keyName=request.params.Key; var name_of_restaurants, RestaurantTimings; async.series( [ // Get the first table contents function ( callback ) { connection.query('SELECT * FROM keyName', function(err, rows, fields) { console.log('Connection result error '+err); name_of_restaurants = rows; callback(); }); }, // Get the second table contents function ( callback ) { connection.query('SELECT * FROM RestaurantTimings', function(err, rows, fields) { console.log('Connection result error '+err); RestaurantTimings = rows; callback(); }); } // Send the response ], function ( error, results ) { response.json({ 'restaurants' : name_of_restaurants, 'RestaurantTimings' : RestaurantTimings }); } ); } );

Answer1:

Considering keyname and RestaurantTimings as the table name. Try this:-

app.get('/RestaurantDesc/:Key',function(request,response,next){ var keyName=request.params.Key; var name_of_restaurants, RestaurantTimings; async.series( [ // Get the first table contents function ( callback ) { connection.query('SELECT * FROM ', keyName, function(err, rows, fields) { console.log('Connection result error '+err); name_of_restaurants = rows; callback(); }); }, // Get the second table contents function ( callback ) { connection.query('SELECT * FROM ', RestaurantTimings, function(err, rows, fields) { console.log('Connection result error '+err); RestaurantTimings = rows; callback(); }); } // Send the response ], function ( error, results ) { response.json({ 'restaurants' : name_of_restaurants, 'RestaurantTimings' : RestaurantTimings }); } ); } );

Recommend

  • How to escalate top-most supervisors in Akka?
  • Custom domains in a Rails App
  • How do I resolve an ImageSource IRandomAccessStream seeking not supported error?
  • angular 2 group validation
  • Using android opencv apps without downloading opencv sdk manager
  • Get Currently Active User in Android
  • What is the difference between a “service account” and an “installed application”?
  • how to populate a SQLite database and use that database in phonegap?
  • how to solve invalid conversion specifier warning in iphone app
  • Universal Image Loader reuse images
  • PushKit for VOIP iOS apps
  • Magento site down due to mysql error General error: 1030 Got error -1 from storage engine
  • Getting media player state in windows phone 7
  • Lock Horizontal View
  • Is there a way to save the selected text and highlight it again once the page is refreshed?
  • Debugging VB6 Code From Visual Studio 2010
  • Build Successful but not running on simulator
  • pyodbc doesn't report sql server error
  • Sequential (transactional) API calls in angular 4 with state management
  • Button text different than value submitted in query string
  • Get history of file changes from TFS to implement custom “blame”-behaviour of exceptions
  • iOS: Detect app start via notification press
  • Use of this Javascript
  • Meteor helpers not available in Angular template
  • Exception “firebase.functions() takes … no argument …” when specifying a region for a Cloud Function
  • C++ Partial template specialization - design simplification
  • PHPUnit_Framework_TestCase class is not available. Fix… - Makegood , Eclipse
  • Is calc() supported in html email?
  • PHP - How to update data to MySQL when click a radio button
  • req.body is undefined - nodejs
  • Counter field in MS Access, how to generate?
  • Symfony2: How to get request parameter
  • Display Images one by one with next and previous functionality
  • ORA-29908: missing primary invocation for ancillary operator
  • How to get next/previous record number?
  • Akka Routing: Reply's send to router ends up as dead letters
  • Is there a mandatory requirement to switch app.yaml?
  • Python: how to group similar lists together in a list of lists?
  • How do you join a server to an Active Directory (domain)?
  • How does Linux kernel interrupt the application?