IT

Electron sqlite3 설치

복리마법사끝판대장 2023. 8. 5. 16:00
반응형

SQLite3는 기본 Node.js 모듈이므로 Electron을 대상으로 재구성하지 않고는 Electron과 함께 직접 사용할 수 없습니다.

아래 가이드를 참고하세요.

 

https://www.electronjs.org/docs/latest/tutorial/using-native-node-modules/

 

Native Node Modules | Electron

Native Node.js modules are supported by Electron, but since Electron has a different application binary interface (ABI) from a given Node.js binary (due to differences such as using Chromium's BoringSSL instead of OpenSSL), the native modules you use will

www.electronjs.org

 

electron-rebuild 설치

npm install --save-dev electron-rebuild

sqlite3 모듈 설치

npm install sqlite3 --save

rebuild sqlite3

./node_modules/.bin/electron-rebuild  -f -w sqlite3

SQLite3 사용하기

이제 SQLite3를 사용하는 방법에 대한 간단한 예를 보겠습니다. 먼저 SQLite3 모듈을 가져와야 합니다

const sqlite3 = require('sqlite3');

다음 메모리 내 데이터베이스에 연결할 데이터베이스 개체를 만듭니다.

var db = new sqlite3.Database(':memory:');

파일 경로를 대신 지정하여 파일 기반 데이터베이스를 사용할 수도 있습니다.

var db = new sqlite3.Database('/path/to/database/file.db');

다음으로 테이블을 만들고 일부 값을 삽입한 후 쿼리한 다음 결과를 콘솔에 출력합니다.

db.serialize(function () {
  db.run("CREATE TABLE Products (name, barcode, quantity)");

  db.run("INSERT INTO Products VALUES (?, ?, ?)", ['product001', 'xxxxx', 20]);
  db.run("INSERT INTO Products VALUES (?, ?, ?)", ['product002', 'xxxxx', 40]);
  db.run("INSERT INTO Products VALUES (?, ?, ?)", ['product003', 'xxxxx', 60]);

  db.each("SELECT * FROM Products", function (err, row) {
    console.log(row);
  });
});

마지막으로, 데이터베이스를 끝낸 후 close 합니다.

db.close();

 

 

반응형