sql nodejs code example

Example 1: mssql query node js

const sql = require('mssql')
const query=`select * from table`;
config = {
    database: "dbname",
  // can keep these option required with latest version of mssql npm package
    options: {
      enableArithAbort: true,
      encrypt: true,
      rowCollectionOnRequestCompletion: false,
    },
    parseJSON: true,
    password: "password",
    port: port, // if youe database running on difrent port then 1433
    server: "server ip or name",
    user: "usenme",
  };

 
sql.on('error', err => {
    // ... error handler
})
 
sql.connect(config).then(pool => {
    return pool.request()
        .query(query)
}).then(result => {
    console.log(result.recordset)
}).catch(err => {
    console.log(err)
});

Example 2: js read mysql database

// Use https://github.com/JackChilds/jsmysqldb

// Then once it's setup you can just do this:

// Config that will be sent to server
const config = {
  "table" : "customers",
  "limit" : "50",
  "offset" : "0"
};

// Use the _POST_REQUEST function to send data to the server and get a response
_POST_REQUEST("link-to-server/database.php", 'config=' + JSON.stringify(config), (response) => {
  // Convert the response into an array of rows:
  var data = JSON.parse(response);
  // Now do what you want with data
  // data[row index][column name]
});

Tags:

Css Example