-- Up
CREATE TABLE Person (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
email TEXT
);
CREATE TABLE Vehicle (
id INTEGER PRIMARY KEY AUTOINCREMENT,
brand TEXT,
model TEXT,
ownerId INTEGER REFERENCES Person(id)
);
INSERT INTO Person (name, email) values ('bruno', 'tuong1805@gmail.com');
INSERT INTO Person (name, email) values ('jack', 'jack@antunes.pt');
INSERT INTO Vehicle (brand, model, ownerId) values('audi', 'R8', 1);
INSERT INTO Vehicle (brand, model, ownerId) values('audi', 'R6', 1);
INSERT INTO Vehicle (brand, model, ownerId) values('mercedes', 'benz', 2);
-- Down
DROP TABLE Person;
DROP TABLE Vehicle;
import { NextApiRequest, NextApiResponse } from 'next';
import sqlite from 'sqlite';
export default async function getPersonById (req: NextApiRequest, res: NextApiResponse) {
const db = await sqlite.open('./mydb.sqlite');
const person = await db.get('select * from person where id = ?', req.query.id);
res.json(person);
}
Cập nhật lại
pages\api\person[id]\index.ts
import { NextApiRequest, NextApiResponse } from'next';constsqlite=require('sqlite');constsqlite3=require('sqlite3');asyncfunctionopenDb() {returnsqlite.open({ filename:'./pages/data/mydb.sqlite', driver:sqlite3.Database, });};exportdefaultasyncfunctiongetPersonById(req:NextApiRequest, res:NextApiResponse) {constdb=awaitopenDb();constperson=awaitdb.get('select * from person where id = ?',req.query.id);res.json(person);}
import { NextApiRequest, NextApiResponse } from 'next';
import sqlite from 'sqlite';
export default async function getPersonById (req: NextApiRequest, res: NextApiResponse) {
const db = await sqlite.open('./mydb.sqlite');
if (req.method === 'PUT') {
const statement = await db.prepare('UPDATE person SET name= ?, email = ? where id = ?');
const result = await statement.run(req.body.name,req.body.email,req.query.id);
result.finalize();
}
const person = await db.get('select * from person where id = ?', req.query.id);
res.json(person);
}
Cập nhật lại
pages\api\person[id]\index.ts
import { NextApiRequest, NextApiResponse } from'next';constsqlite=require('sqlite');constsqlite3=require('sqlite3');asyncfunctionopenDb() {returnsqlite.open({ filename:'./pages/data/mydb.sqlite', driver:sqlite3.Database, });};exportdefaultasyncfunctiongetPersonById(req:NextApiRequest, res:NextApiResponse) {constdb=awaitopenDb();if (req.method ==='PUT') {conststatement=awaitdb.prepare('UPDATE person SET name= ?, email = ? where id = ?');awaitstatement.run(req.body.name,req.body.email,req.query.id); }constperson=awaitdb.get('select * from person where id = ?',req.query.id);res.json(person);}