Znote (recipes)
  Get Znote  

SQL query on local MySQL

How to make SQL query on your local MySQL

 

Make SQL query

Blog 💡: Use Znote as SQL client

Install NPM packages

Install SQL driver for your database

npm install --save mysql2
npm install --save sequelize

Install tablify to read results as table

npm install --save tablify

Create SQL functions

Create these 2 functions f(x) below to connect to your database and tablify results or just run the block of code to declare functions before running the last block.

// global 1
// custom functions
async function localSQL(sqlQuery) {
  const { Sequelize } = require('sequelize');
  const sequelize = new Sequelize("", "root", "root", {  
    dialect: "mysql"/* one of 'mysql' | 'mariadb' | 'postgres' | 'mssql' */,
    host: "localhost"
  });
  sequelize.authenticate();
  const [results, metadata] = await sequelize.query(sqlQuery);
  
  sequelize.close()
  return results;
}
async function printSQL(sqlQuery) {
  const tablify = require('tablify').tablify
  print(tablify(await localSQL(sqlQuery)));
}

Make SQL query on your database

await printSQL("select * from book.vente_item limit 10");

Related recipes