title: Error: ER_NO_DB_ERROR: No database selected in Node.js?
body: <p>I want to create <strong>sql</strong> table from <strong>node.js</strong> server. However, I am encountering this error:</p>
  
  <p><em>(node:13560) UnhandledPromiseRejectionWarning: Error: ER_NO_DB_ERROR: No database selected</em></p>
  
  <p>I tried to add: <strong>use stan_sehk;</strong>
  where stan_sehk is the name of the schema. However I got another error:
  <em>(node:12468) UnhandledPromiseRejectionWarning: Error: ER_PARSE_ERROR: You have an error in your SQL syntax;</em></p>
  
  <p>The thing is that the select statements work but create statements give errors. The same is true for <em>CREATE VIEW</em> stataments.</p>
  
  <pre><code>//MY db_config.json file
  
  {
  "mysql":{
    "host":  "localhost",
    "port": 3306,
    "acquireTimeout": 30000,
    "user": "appuser",
    "password" :  "abc123",
    "database": ""
  },
    "redis":{
      "port"    : 6379,
      "host"    : "127.0.0.1"
    }
  }
  </code></pre>
  
  <p>//My mysql_access.js file </p>
  
  <pre><code>const mysql = require('mysql');
  const fs = require('fs');
  const json = require('json');
  
  const config_param = fs.readFileSync('./data_access/db_config.json', 'utf-8');
  const config_param_json = JSON.parse(config_param);
  
  const pool = mysql.createPool({
      host: config_param_json.mysql.host,
      port: config_param_json.mysql.port,
      acquireTimeout: config_param_json.mysql.acquireTimeout,
      user: config_param_json.mysql.user,
      password: config_param_json.mysql.password,
      dateStrings: true 
  });
  
  let query = function( sql, values) {
      return new Promise(( resolve, reject ) =&gt; {
          pool.getConnection(function(err, connection) {
              if (err) {
                  reject( err )
              } else {
                  connection.query(sql, values, ( err, rows) =&gt; {
                      if ( err ) {
                          reject( err )
                      } else {
                          resolve(JSON.parse(JSON.stringify(rows)))
                      }
                      connection.release()
                  })
              }
          })
      })
  };
  
  module.exports = {query};
  </code></pre>
  
  <p>Query for error 1:</p>
  
  <pre><code>let sql = `create table if not exists todos(
                        id int primary key auto_increment,
                        title varchar(255)not null,
                        completed tinyint(1) not null default 0
                    )`;
  let sql_call = {
          sql: sql,
          sql_value: null
  };
  
  
  let sql_result = await mysql_access.query(sql_call.sql, sql_call.sql_value);
  console.log(sql_result[11]);
  </code></pre>
  
  <p>Error Message:</p>
  
  <pre><code>(node:10640) UnhandledPromiseRejectionWarning: Error: ER_NO_DB_ERROR: No database selected
  </code></pre>
  
  <p>Query For error 2:</p>
  
  <pre><code>let sql = `use stan_sehk; create table if not exists todos(
                        id int primary key auto_increment,
                        title varchar(255)not null,
                        completed tinyint(1) not null default 0
                    )`;
  
  let sql_call = {
           sql: sql,
           sql_value: null
  };
  let sql_result = await mysql_access.query(sql_call.sql, sql_call.sql_value);
  console.log(sql_result[11]);
  </code></pre>
  
  <p>Error Message:</p>
  
  <pre><code>(node:12468) UnhandledPromiseRejectionWarning: Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'create table if not exists todos(id int primary key a' at line 1
  </code></pre>
  
  <p>Please help me if there is something that I am not doing correctly. And as I said, <em>SELECT</em> queries work but <em>CREATE</em> queries give the above errors. Thanks a lot in advance.</p>