* 질문 등록 시 다음의 내용을 꼭 기입하여 주세요.
|
Window10 64bit, Linux 64bit 등 |
|
10.2.0 |
|
[도움말]-[버전정보] 확인 |
|
nodejs |
* CUBRID 응용 오류, SQL 오류 또는 SQL 튜닝 관련된 문의는 반드시 다음의 내용을 추가해 주세요. 비밀글이나 비밀 댓글도 가능합니다.
* 저희가 상황을 이해하고, 재현이 가능해야 알 수 있는 문제들이 많습니다. 가능한 정보/정황들을 부탁합니다.
에러 내용 및 재현 방법 | 재현 가능한 Source와 SQL |
관련 테이블(인덱스, 키정보 포함) 정보 | CUBRID 홈 디렉토리 아래 log 디렉토리 압축 |
-------------- 아래에 질문 사항을 기입해 주세요. ------------------------------------------------------------------------
client.execute('INSERT INTO tbl (name) VALUES(?)', 'cubrid', function (err) {
// Handle the error;
});
인서트 작업 후 콜백함수로 새로 추가된 자동증가된 ID값을 얻고 싶은데, 콜백함수는 에러만 처리하는거 같은데 좋은 방법이 없을까요?
mysql의 경우 콜백 함수에 결과값도 지원되어서 그 값을 알수가 있습니다.
1) 큐브리드 NODE 드라이브에서는 mysql 드라이브에서 지원하는 ".insertId" 함수가 없습니다.
2) 따라서, 아래 예시와 같이 SELECT LAST_INSERT_ID() 쿼리를 수행해 원하시는 "자동증가된 ID값"을 얻을 수 있습니다.
* LAST_INSERT_ID 함수에 대한 자세한 내용은 아래 온라인 매뉴얼을 참조해주세요.
* 온라인 매뉴얼 URL : https://www.cubrid.org/manual/ko/10.2/sql/function/information_fn.html?highlight=last_insert_id#last-insert-id
예제)
코드 :
const client = CUBRID.createConnection(dbConf); // cubrid DB connection
var sql = "INSERT INTO t1 VALUES(null,?)";
var sql2 = "SELECT LAST_INSERT_ID()";
client.execute(sql, 'cubrid2', function(err){
if (err) throw err;
//mysql에서 지원하는 함수
//console.log(result.insertId);
//cubrid 에서는 아래 쿼리를 한 번 더 수행
client.query(sql2, function(err, result, queryHandle){
const rows = result.ColumnValues;
console.log("insertID : " + rows);
console.log("");
console.log(result);
client.closeQuery(queryHandle);
});
});
콘솔 결과 :
insertID : 18
{ ColumnDataTypes: [ 'Numeric' ],
ColumnNames: [ 'last_insert_id()' ],
ColumnValues: [ [ 18 ] ],
RowsCount: 1 }
DB 결과 :
csql> create table t1 (col1 int auto_increment, col2 varchar);
csql> select * from t1;
=== <Result of SELECT Command in Line 1> ===
col1 col2
===================================
1 'cubrid1'
2 'cubrid1'
3 'cubrid1'
...
17 'cubrid1'
18 'cubrid2'