简体   繁体   English

如何利用此MySQL javascript代码创建数据库/编写查询

[英]How to utilize this MySQL javascript code to create a database / write a query

Just started to learn using nodejs and javascript to make some basic servers and databases to look at and we have this code to utilize: 刚开始学习使用nodejs和javascript制作一些基本的服务器和数据库,我们可以使用以下代码:

var mysql = require('mysql');

var connection = mysql.createConnection(
    {
      host     : 'stusql.dcs.shef.ac.uk',
      port     : '3306',
      user     : 'USERNAME',
      password : '********',
      database : 'DBNAME'
    }
);
connection.connect();
var query = connection.query('SELECT * FROM device');

query.on('error', function(err) {
    throw err;
});

query.on('fields', function(fields) {
    console.log(fields);
});

query.on('result', function(row) {
    console.log(row);
});
connection.end();

And from this we are supposed to now put our login and password in this code (which I have done on my version) and then create a small database and write a query . 然后我们应该将登录名和密码放入此代码中(在我的版本中已完成),然后创建一个小型数据库并编写查询 How do i do this part? 我该怎么做?

Do I go to the host and how do use this code to make a small database and write a query... There's no explanation and I ran this code on webstorm IDE and it runs but after a short while it throws the error from the code. 我是否要去主机,以及如何使用此代码创建一个小型数据库并编写查询...没有解释,我在webstorm IDE上运行了此代码,它运行了,但过了一会儿,它抛出了代码错误。 Any help would be greatly appreciated! 任何帮助将不胜感激!

Though you can perform all the query you want, i don't see a need to create database or table dynamically. 尽管您可以执行所需的所有查询,但我认为不需要动态创建数据库或表。 I use connection.query() for CRUD Operations only. 我仅将connection.query()用于CRUD操作

But still you can do that.See the code. 但是仍然可以做到。请参见代码。

I have added setTimeout() so that you can clearly see what is it happening while running the code. 我添加了setTimeout()以便您在运行代码时可以清楚地看到发生了什么。

var mysql = require('mysql');
var config={
      host     : 'localhost',
      port     : '3306',
      user     : 'root',
      password : 'root',
    }
var connection = mysql.createConnection();
connection.connect(config);
connection.query('drop database if exists mydb',function(err){
  if(err)
    return console.log(err);
  console.log('deleted db');

  setTimeout(function(){
    connection.query('create database if not exists mydb',function(err){
      if(err)
        return console.log(err);
      console.log('DB Created successfully');

      setTimeout(function(){
        var qstring='CREATE TABLE mydb.table1 '+
        '(id INT NOT NULL,name VARCHAR(45)'+
        ' NULL,PRIMARY KEY (id));'
        connection.query(qstring,function(err){
          if(err)
            return console.log(err);
          console.log('table created successfully');

          setTimeout(function(){
            var qstring="INSERT INTO mydb.table1 (`id`, `name`) VALUES ('1', 'abc'),('2', 'bcd'),('3','cde')"
            connection.query(qstring,function(err){
              if(err)
                return console.log(err);
              console.log('Rows added successfully');

              setTimeout(function(){
                var query = connection.query('SELECT * FROM mydb.table1');

                query.on('error', function(err) {
                  throw err;
                });

                query.on('fields', function(fields) {
                  console.log(fields);
                });

                query.on('result', function(row) {
                  console.log(row);
                });
              },2000);
            });
          },2000);
        });
      },2000);
    })
  },2000);
});

So i guess that was what you wanted. 所以我想那是您想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM