[SQLITE] Select by id sqlite, sqlite3 (ok)

C:\Users\Administrator\Desktop\nextjs\openDB.ts

import { open } from 'sqlite'
import sqlite3 from 'sqlite3'
export default async function openDB () {
  return open({
    filename: 'cars.sqlite',
    driver: sqlite3.Database
  })
}

C:\Users\Administrator\Desktop\nextjs\database-test.js

const sqlite = require('sqlite');
const sqlite3 = require('sqlite3');
async function setup() {
  const db = await sqlite.open({
    filename: 'cars.sqlite',
    driver: sqlite3.Database,
  });
  await db.migrate({ force: 'last' });
  const faq = await db.all('SELECT * FROM FAQ ORDER BY createDate DESC');
  console.log('ALL faq', JSON.stringify(faq, null, 2));
  const cars = await db.all('SELECT * FROM Car');
  console.log('ALL CARS', JSON.stringify(cars, null, 2));
}
setup();

C:\Users\Administrator\Desktop\nextjs\package.json

{
  "name": "nextjs",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start"
  },
  "dependencies": {
    "@material-ui/core": "^4.11.3",
    "@material-ui/icons": "^4.11.2",
    "@material-ui/lab": "^4.0.0-alpha.57",
    "@material-ui/styles": "^4.11.3",
    "axios": "^0.21.1",
    "formik": "^2.2.6",
    "next": "10.0.8",
    "react": "17.0.1",
    "react-dom": "17.0.1",
    "sqlite": "^4.0.19",
    "sqlite3": "^5.0.2",
    "swr": "^0.5.2"
  },
  "devDependencies": {
    "@types/node": "^14.14.33",
    "@types/react": "^17.0.3",
    "babel-plugin-styled-components": "^1.12.0",
    "typescript": "^4.2.3",
    "babel-plugin-module-resolver": "^3.1.1"
  }
}

C:\Users\Administrator\Desktop\nextjs\pages\car[make][brand][id].tsx

export const getServerSideProps: GetServerSideProps<CarDetailsProps> = async (ctx) => {
  const id = ctx.query.id;
  const db = await openDB();
  const car = await db.get<CarModel | undefined>('SELECT * FROM Car where id = ?',id);
  console.log(car);
  return {
    props: {car}
  }
}

Kết quả:

Last updated

Was this helpful?