mysql createPool
This commit is contained in:
@@ -1,40 +1,19 @@
|
||||
// pages/api/talas_v5/area.js
|
||||
// Lesen von talas_v5 MySQL-Datenbank -> area Tabelle enthält DAUZ Geräte
|
||||
// Wenn gebraucht wird, dann nutzen ansonsten löschen
|
||||
|
||||
import mysql from "mysql";
|
||||
import getPool from "../../utils/mysqlPool"; // Verwende den Singleton-Pool
|
||||
|
||||
const dbConfig = {
|
||||
host: process.env.DB_HOST,
|
||||
user: process.env.DB_USER,
|
||||
password: process.env.DB_PASSWORD,
|
||||
database: process.env.DB_NAME,
|
||||
port: process.env.DB_PORT,
|
||||
};
|
||||
//console.log("my dbconfig: ", dbConfig);
|
||||
export default function handler(req, res) {
|
||||
const connection = mysql.createConnection(dbConfig);
|
||||
export default async function handler(req, res) {
|
||||
try {
|
||||
const pool = getPool(); // Hole den Pool
|
||||
|
||||
connection.connect((err) => {
|
||||
if (err) {
|
||||
console.error("Fehler beim Verbinden:", err.stack);
|
||||
res.status(500).json({ error: "Verbindungsfehler zur Datenbank" });
|
||||
return;
|
||||
}
|
||||
// Führe die Abfrage aus
|
||||
const [results] = await pool.query("SELECT id, name FROM area WHERE id = ?", [req.query.id]);
|
||||
|
||||
//console.log("Verbunden als ID", connection.threadId);
|
||||
//Fehler weil, existiertdie Tabelle auf localhost:3000 nicht
|
||||
connection.query("SELECT ..., ..., ..., ... FROM ... WHERE ... = ...", (error, results) => {
|
||||
if (error) {
|
||||
console.error("Fehler beim Abrufen der API", error);
|
||||
res.status(500).json({ error: "Fehler bei der Abfrage" });
|
||||
return;
|
||||
}
|
||||
|
||||
// Wichtig: Senden Sie die Antwort zurück
|
||||
res.status(200).json(results);
|
||||
|
||||
connection.end();
|
||||
});
|
||||
});
|
||||
// Sende die Antwort zurück
|
||||
res.status(200).json(results);
|
||||
} catch (error) {
|
||||
console.error("Fehler beim Abrufen der API", error);
|
||||
res.status(500).json({ error: "Fehler bei der Abfrage" });
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user