0

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

Background Information::

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

Problem I am facing::

  • Clearly i am not sunig the sql query correctly
  • How to solve this

[ExpressCode]

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
    });
} );
} );
3
  • 1
    I'm assuming the variable keyname is intended to contain the text name of the table. If this is right then keyname needs to be outside the string and concatenated onto the string I would think. Commented Aug 28, 2013 at 18:15
  • Yes, Your assumption is 100% correct to what i am trying to tell .... please can you put your solution in code by editing my code i posted ..... It would help a newbie like me ... thanks ! Commented Aug 28, 2013 at 18:18
  • 1
    What mysql driver are you using? This one? Commented Aug 28, 2013 at 18:34

1 Answer 1

0

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
    });
} );
} );
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.