ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • node.js mysql query
    카테고리 없음 2022. 1. 19. 01:49

    npm i mysql

     

    Performing queries

    The most basic way to perform a query is to call the .query() method on an object (like a Connection, Pool, or PoolNamespace instance).

    The simplest form of .query() is .query(sqlString, callback), where a SQL string is the first argument and the second is a callback:

    connection.query('SELECT * FROM `books` WHERE `author` = "David"', function (error, results, fields) {
      // error will be an Error if one occurred during the query
      // results will contain the results of the query
      // fields will contain information about the returned results fields (if any)
    });

    The second form .query(sqlString, values, callback) comes when using placeholder values (see escaping query values):

    connection.query('SELECT * FROM `books` WHERE `author` = ?', ['David'], function (error, results, fields) {
      // error will be an Error if one occurred during the query
      // results will contain the results of the query
      // fields will contain information about the returned results fields (if any)
    });

    he third form .query(options, callback) comes when using various advanced options on the query, like escaping query values, joins with overlapping column names, timeouts, and type casting.

    connection.query({
      sql: 'SELECT * FROM `books` WHERE `author` = ?',
      timeout: 40000, // 40s
      values: ['David']
    }, function (error, results, fields) {
      // error will be an Error if one occurred during the query
      // results will contain the results of the query
      // fields will contain information about the returned results fields (if any)
    });

    Note that a combination of the second and third forms can be used where the placeholder values are passed as an argument and not in the options object. The values argument will override the values in the option object.

    connection.query({
        sql: 'SELECT * FROM `books` WHERE `author` = ?',
        timeout: 40000, // 40s
      },
      ['David'],
      function (error, results, fields) {
        // error will be an Error if one occurred during the query
        // results will contain the results of the query
        // fields will contain information about the returned results fields (if any)
      }
    );

    If the query only has a single replacement character (?), and the value is not null, undefined, or an array, it can be passed directly as the second argument to .query:

    connection.query(
      'SELECT * FROM `books` WHERE `author` = ?',
      'David',
      function (error, results, fields) {
        // error will be an Error if one occurred during the query
        // results will contain the results of the query
        // fields will contain information about the returned results fields (if any)
      }
    );

     

     

     

     

     

     

     

    참고:https://www.npmjs.com/package/mysql#establishing-connections

Designed by Tistory.