백엔드 Back-end/노드 Node.js

Node.js mysql 모듈, 데이터베이스에 insert 데이터 입력 뒤 id 값 확인하기

Tap to restart 2020. 10. 23. 22:00
반응형

mysql 모듈

Node.js에서 mysql에 접속해서 데이터를 입출력할 때 mysql 모듈을 쓰고 있다.

데이터를 입력한 뒤 id값은 어떻게 확인할까?

 

results.insertId로 확인할 수 있다.

 

코드

(출처: www.npmjs.com/package/mysql)

If you are inserting a row into a table with an auto increment primary key, you can retrieve the insert id like this:
connection.query('INSERT INTO posts SET ?', {title: 'test'}, function (error, results, fields) {
  if (error) throw error;
  console.log(results.insertId);
});

 

 

반응형