0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

nodeを使ってmysqlに接続する

Posted at

nodeでmysqlに接続するには

// requireの設定
const mysql = require('mysql');
 
// MySQLとのコネクションの作成
const connection = mysql.createConnection({
  host : 'localhost',
  user : 'root',
  database: 'testdatabase'
});
 
// 接続
connection.connect();
 
// userdataの取得
connection.query('SELECT * from userdata;', function (err, rows, fields) {
  if (err) { console.log('err: ' + err); } 
 
  console.log('name: ' + rows[0].name);
  console.log('id: ' + rows[0].id);
 
});
 
// userdataのカラムを取得
connection.query('SHOW COLUMNS FROM userdata;', function (err, rows, fields) {
  if (err) { console.log('err: ' + err); }
 
  console.log(rows[0].Field);
  console.log(rows[1].Field);
});
 
// 接続終了
connection.end();
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?