본문으로 바로가기

[Node.js] SQLite3 : run() 예외 처리

category 프로그래밍/JS+Node.js 2019. 6. 12. 19:47

test db의 test 테이블을 생성할 때 예외 처리방법

//db명 'test.db', table명 'test'
let sqlite3 = require('sqlite3').verbose();
let db = new sqlite3.Database('./db/test.db');
db.run("CREATE TABLE test (name TEXT);", function(err, row) {
	if(err) {
   		console.log(err); // err는 err.errno, err.code 두 개의 키를 가진다
	}
});

 

close()도 비슷하다. 둘 다 콜백함수다.

db.close(function(err) {
        if(!err) callback(true); //에러가 없으면 true 반환
});

 

 

SQLite의 에러코드 : https://www.sqlite.org/c3ref/c_abort.html

 

Result Codes

Many SQLite functions return an integer result code from the set shown here in order to indicate success or failure. New error codes may be added in future versions of SQLite.

www.sqlite.org