https://stackoverflow.com/questions/62851973/using-mysql-db-functions-with-sqlite-node-js
Using MySQL db functions (?) with SQLite (Node.js)
I'm using a tutorial to do JWT/bcryptjs auth and then INSERT into a SQlite table. Thing is the tutorial is for MySQL and I get errors like db.query is not a function and db.escape is not a function...
stackoverflow.com
const sqlite3 = require('sqlite3').verbose();
// open the database
let db = new sqlite3.Database('./db/chinook.db');
let sql = `SELECT * FROM users WHERE LOWER(username) = LOWER(?)`;
db.all(sql, [req.body.username], (err, rows) => {
if (err) {
throw err;
}
rows.forEach((row) => {
console.log(row.name);
});
});
// close the database connection
db.close();
이렇게 쓰인다
나도 query -> all로 고치니까 대부분 해결됨
exports.home = function (req, res, queryData) {
db.all(`SELECT * FROM users`, function(err, users) {
console.log(users);
var title = 'Welcome';
var description = 'Hello, users';
console.log(users);
var list = template.list(users);
var html = template.HTML(title, list,
`<h2>${title}</h2>${description}`,
`<a href="/create">create</a>`
);
res.writeHead(200);
res.end(html);
});
}
query -> all로 고치기만 했는데 잘 된다
근데 sqlite에서 주의할 점이 몇가지 있음
1. on duplicate key가 잘 안된다
http://jason-heo.github.io/mysql/2014/03/05/manage-dup-key2.html
MySQL 중복 레코드 관리 방법 (INSERT 시 중복 키 관리 방법 (INSERT IGNORE, REPLACE INTO, ON DUPLICATE UPDATE))
Test에 사용된 MySQL 버전 목차 1. 개요 MySQL에는 아래 3가지 방법을 이용하여 중복 레코드를 관리할 수 있다. INSERT IGNORE ... REPLACE INTO ... INSERT INTO ... ON DUPLICATE UPDATE 각 방법의 특징을 요약하면 다음
jason-heo.github.io
위는 mysql 중복 레코드 관리 방법
sqlite 할 수 없어서 많이 애먹음 ㅜㅜ
그래서 아이디어를 참고한 곳
https://stackoverflow.com/questions/2717590/sqlite-insert-on-duplicate-key-update-upsert
SQLite INSERT - ON DUPLICATE KEY UPDATE (UPSERT)
MySQL has something like this: INSERT INTO visits (ip, hits) VALUES ('127.0.0.1', 1) ON DUPLICATE KEY UPDATE hits = hits + 1; As far as I know this feature doesn't exist in SQLite, what I want to...
stackoverflow.com
insert or replace into를 이용해서 키를 업데이트 시켰다
2. int -> integer
https://www.sqlitetutorial.net/sqlite-unique-constraint/
SQLite UNIQUE Constraint
In this tutorial, you will learn how to use the SQLite UNIQUE constraint to ensure all values in a column or a group of columns are unique
www.sqlitetutorial.net
https://www.sqlite.org/datatype3.html
Datatypes In SQLite Version 3
INTEGER. The value is a signed integer, stored in 1, 2, 3, 4, 6, or 8 bytes depending on the magnitude of the value.
www.sqlite.org
sqlite 3 에서 다양한 자료형들
'웹 프로그래밍 > SQL' 카테고리의 다른 글
redirect 하려면 res.end()하기 전에 302 location 고치기 (0) | 2021.09.24 |
---|---|
데이터베이스 설정할 때 중복 체크하기 위해서 if exists 문구 넣어주기 (0) | 2021.09.24 |
ERROR 2002 (HY000): socket '/tmp/mysql.sock' (2) 해결하려고 brew services start mysql 함 (3) | 2021.09.24 |
[sql] 우유와 요거트 모두 산 사람 찾기 with tmp를 이용해서 품 (0) | 2021.09.15 |
[sql] 해비유저 IN () (0) | 2021.09.15 |
[sql] 프로그래머스 마지막 string, date 풀기 (0) | 2021.09.15 |
[sql] 프로그래머스 이제 join을 공부하자 (0) | 2021.09.15 |
[sql] 프로그래머스 IFNULL(name, "No name") (0) | 2021.09.15 |