gpt4 book ai didi

javascript - 从 postgre 到 mysql 的 npm sql 池

转载 作者:行者123 更新时间:2023-11-29 15:16:10 27 4
gpt4 key购买 nike

我有这个代码:

    console.log("-------------- Connecting to the database...");
const client = await pool.connect();
console.log("-------------- Connection process done.");
console.log("-------------- Now Querying.");
var sql = `SELECT * FROM users WHERE email = '${email}'`;
console.log("-------------- Query done.");
let query = await client.query(sql);
client.release();
pool.release();
if(query.rows[0]){return "Email already exists. Please login."}
return 0;

我想这样做:

    pool.getConnection

我该怎么办?

最佳答案

我假设您在自述文件中使用 npm 包 mysql,您可以找到有关池主题的文档。

https://www.npmjs.com/package/mysql#pooling-connections

const mysql = require('mysql');
const pool = mysql.createPool(...);

const email = "test@example.com" // Users email address variable.

pool.getConnection(function(err, connection) {
if (err) throw err; // not connected!

// Use the connection
connection.query('SELECT * FROM users WHERE email = ?', [email], function (error, results, fields) {
// When done with the connection, release it.
connection.release();

// Handle error after the release.
if (error) throw error;

// Don't use the connection here, it has been returned to the pool.

if(!results.length) {
// No results found.
}else {
// Results found.
}
});
});

我尚未测试此代码,如果您确实遇到问题,请查看文档。

关于javascript - 从 postgre 到 mysql 的 npm sql 池,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59685278/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com