0

我正在动态尝试在下面的快速代码部分中将表的名称传递给 SQL 查询

背景资料::

  • 我作为(键,值)传递的是字符串,它将是 sql 数据库中表的名称
  • 我为什么要根据动态客户端请求动态选择表

我面临的问题::

  • 显然我没有正确地使用 sql 查询
  • 如何解决这个问题

[快递代码]

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
    });
} );
} );
4

1 回答 1

0

考虑keynameRestaurantTimings作为表名。尝试这个:-

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
    });
} );
} );
于 2013-08-28T19:03:32.760 回答