Skip to content

examples-mysql

git: https://github.com/WuChenDi/Front-End/tree/master/07-nodejs/examples-mysql

ts
import { Hono } from 'hono'
import mysql from 'mysql2/promise'

const app = new Hono()

app.get('/', (c) => {
  return c.text('Hello Hono!')
})

// Create the connection to database
const connection = await mysql.createConnection({
  host: 'localhost',
  user: 'root',
  password: '123456',
  database: 'dd',
})

// Execute sql statement
const sql = 'SELECT * FROM `tasks`'
// const sql = 'SELECT id, title, description FROM `tasks`'
// const sql = `SELECT * FROM tasks WHERE id='1'`
// const sql = `UPDATE tasks SET description='sql description' WHERE id=1`
// const sql = `UPDATE tasks SET title='test title' where title='title 12332111111'`
// const sql = `INSERT INTO tasks (title, description, createdAt, updatedAt) values ('test title', 'test description', '${new Date().toISOString().slice(0, 19).replace('T', ' ')}', '${new Date().toISOString().slice(0, 19).replace('T', ' ')}')`

try {
  const [results, fields] = await connection.query(sql)
  // results contains rows returned by server
  console.log(results)
  // fields contains extra meta data about results, if available
  console.log(fields)
} catch (err) {
  console.log(err)
}

export default app
json
{
  "name": "examples-mysql",
  "version": "1.0.0",
  "author": "wudi",
  "license": "MIT",
  "scripts": {
    "dev": "bun run --hot src/index.ts"
  },
  "dependencies": {
    "hono": "^4.2.6",
    "mysql2": "^3.9.7"
  },
  "devDependencies": {
    "@types/bun": "latest",
    "@types/node": "^20.12.7"
  }
}