[NODEJS]DATABASE INSERT,SELECT,DELETE(SQLITE)

SQLITE๋Š” ๋กœ๊ทธ์ธ์—†์ด ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ€๋ฒผ์šด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์ž…๋‹ˆ๋‹ค.
๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์„œ๋ฒ„๋ถ€๋ถ„์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— dbํŒŒ์ผ์— ๋ฐ”๋กœ ์ ‘๊ทผํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
์„œ๋ฒ„์ ‘์† ์—†์ด ์›น์•ฑ์„ ๋งŒ๋“ค๊ฑฐ๋‚˜ ๋ชจ๋ฐ”์ผ ์•ฑ์„ ๋งŒ๋“ค ๋•Œ ๋งŽ์ด ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
์˜คํ”ˆ ์†Œ์Šค์ด๋ฉฐ ์ž์„ธํ•œ ๋‚ด์šฉ์€ https://sqlite.org ์—์„œ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

SQLITE is a lightweight database that can be used without logging in.
Since there is no database server part, you can directly access and use the db file.
It is often used when creating web apps or mobile phone apps without server access.
It is open source and more information can be found at https://sqlite.org.

1.SQLITE ํ•จ์ˆ˜(SQLITE FUNCTION)
– ์•„๋ž˜ ์ฝ”๋“œ์—์„œ ์‚ฌ์šฉ๋œ SQLITEํ•จ์ˆ˜ ์ž…๋‹ˆ๋‹ค.
– This is the SQLITE function used in the code below.

.exec() : ์ผ๋ฐ˜์ ์ธ SQL ๊ตฌ๋ฌธ์„ ์‹คํ–‰ํ•˜๋ฉฐ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ง์ ‘์ ์œผ๋กœ ์กฐ์ž‘(ํ•ธ๋“ค๋ง) ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.
.run() : ์ด ๋ฐฉ๋ฒ•์€ ์ผ๋ฐ˜์ ์œผ๋กœ INSERT, UPDATE, DELETE ๋“ฑ๊ณผ ๊ฐ™์ด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์ˆ˜์ •ํ•˜๋Š” SQL ์ฟผ๋ฆฌ๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. SQL ์ธ์ ์…˜ ๊ณต๊ฒฉ์„ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ๋งค๊ฐœ๋ณ€์ˆ˜ํ™”๋œ ์ฟผ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
.get() : SQLLITE์˜ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ 1ํ–‰์˜ ๋ฐ์ดํ„ฐ๋งŒ ๊ฐ€์ง€๊ณ  ์˜ต๋‹ˆ๋‹ค.
.all() : ํŠน์ • ๊ธฐ์ค€์— ๋”ฐ๋ผ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ์—ฌ๋Ÿฌ ๋ ˆ์ฝ”๋“œ๋ฅผ ๊ฒ€์ƒ‰ํ•˜๋ ค๊ณ  ํ•  ๋•Œ ์ผ๋ฐ˜์ ์œผ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
.each(): SELECT ์ฟผ๋ฆฌ๋ฅผ ์‹คํ–‰ํ•˜์—ฌ ๋ฐ์ดํ„ฐ ํ…Œ์ด๋ธ”์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฒ€์ƒ‰ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ๊ฒ€์ƒ‰๋œ ๋ฐ์ดํ„ฐ๋ฅผ ํ–‰๋‹จ์œ„๋กœ ๋ฐ˜๋ณตํ•ด์„œ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.

.exec() : General SQL statements are executed and parameters cannot be directly manipulated (handled).
.run():This method is typically used to execute SQL queries that modify the database, such as INSERT, UPDATE, DELETE, etc.It uses parameterized queries to prevent SQL injection attacks.
.get():Only one row of data is imported from the SQLLITE database.
.all():It’s commonly used when you expect to retrieve multiple records from the database based on certain criteria.
.each : Used to retrieve data from a data table by executing a SELECT query. Retrieves the searched data row by row repeatedly.

2.์„ค์น˜(install)
๊ฐ„๋‹จํžˆ npm๋ช…๋ น์–ด๋กœ ์„ค์น˜ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
You can simply install it with the npm command.

#npm install sqlite sqlite3

3.๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์ ‘์†(Database connection)
๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ ‘์†์„ ์œ„ํ•ด์„œ openํ•จ์ˆ˜ ๋˜๋Š” new sqlite3.Database()๋ฅผ ์‚ฌ์šฉํ•  ์žˆ์Šต๋‹ˆ๋‹ค.
To connect to the database, you can use the open function or new sqlite3.Database().

1)๋ชจ๋“ˆ ์ž„ํฌํŠธ(module import)

const sqlite3 = require('sqlite3').verbose();
const { open } = require('sqlite');
const dbname = "member.db";

2)open()ํ•จ์ˆ˜ ์‚ฌ์šฉ(Using the open() function) or new sqlite3.Database()

  const db = await open({
        filename: 'member.db',
        driver: sqlite3.Database
  });
const db = new sqlite3.Database('member.db');

4.๋ฐ์ดํ„ฐ์ž…๋ ฅ(Data input) [ insertDB() ]
– membersํ…Œ์ด๋ธ”์ด ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
– insert๊ตฌ๋ฌธ์„ ์ด์šฉํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค.
– (?,?,?,) ์ด ๋ถ€๋ถ„์— id,username,room์ด ์œ„์น˜ํ•ฉ๋‹ˆ๋‹ค.

– If the members table does not exist, it is created.
– Enter data using the insert statement.
– The id, username, and room are located in this part. (?,?,?,)

....      
     await db.exec(`
        CREATE TABLE IF NOT EXISTS members (
            idx INTEGER PRIMARY KEY AUTOINCREMENT,
            id TEXT,
            username TEXT,
            room TEXT
        );
      `);
    
      await db.run("INSERT INTO members (id, username, room) VALUES (?, ?, ?)", id, username,room);
      db.close();
....

5.๋ชจ๋“  ๋ฐ์ดํ„ฐ ๋ณด๊ธฐ( View all data )[ showAllDB() ]
– membersํ…Œ์ด๋ธ”์˜ ๋ชจ๋“ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฒ€์ƒ‰ํ•˜๊ณ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.
– All data in the members table is searched and output.

  ...
   await db.each("SELECT * FROM members", (err, row) => {
    if (err) {
      console.error(err.message);
    }
    console.log(row.id, row.username, row.room);
  });
  
  db.close();
...

6.๋ชจ๋“  ๋ฐ์ดํ„ฐ ์‚ญ์ œ(delete all data)[ deleteAllDB() ]
– membersํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค.
– Delete all data in the members table.

...
  await db.run('DELETE FROM members');
  await db.close();
...

7.ํ•จ์ˆ˜ ์‹คํ–‰(Function excute)
– ์ฝ”๋ฉ˜ํŠธ์ฒ˜๋ฆฌ ๋ถ€๋ถ„(//)์„ ์‚ญ์ œํ•ด์„œ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.
– Execute it by deleting the comment processing part ( // ).

//insertDB("240502","helloworld1","worldclass");
//showAllDB();
//deleteAllDB();

8.์ „์ฒด์ฝ”๋“œ(Full code)

const sqlite3 = require('sqlite3').verbose();
const { open } = require('sqlite');
const dbname = "member.db";
//const db = new sqlite3.Database('member.db');

async function insertDB(id,username,room) {
/*
  const db = await open({
        filename: 'member.db',
        driver: sqlite3.Database
    });
*/   
const db = new sqlite3.Database(dbname);

      await db.exec(`
        CREATE TABLE IF NOT EXISTS members (
            idx INTEGER PRIMARY KEY AUTOINCREMENT,
            id TEXT,
            username TEXT,
            room TEXT
        );
      `);
    
      await db.run("INSERT INTO members (id, username, room) VALUES (?, ?, ?)", id, username,room);
      db.close();
}

async function showAllDB(){
  
  const db = new sqlite3.Database(dbname);
  await db.each("SELECT * FROM members", (err, row) => {
    if (err) {
      console.error(err.message);
    }
    console.log(row.id, row.username, row.room);
  });
  
  db.close();

}

async function deleteAllDB(){

  const db = new sqlite3.Database(dbname);
  await db.run('DELETE FROM members');
  await db.close();

}

//insertDB("240502","helloworld1","worldclass");
//showAllDB();
//deleteAllDB();


Leave a Reply