forked from yanlele/node-index
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
51 lines (44 loc) · 1.32 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
const mysql = require('mysql');
const pool = mysql.createPool({
host: '127.0.0.1',
database: '001_koa_demo',
user: 'yanle',
password: '123456'
});
let query = function (sql, values) {
return new Promise((resolve, reject) => {
pool.getConnection(function (err, connection) {
if (err) {
resolve(err)
} else {
connection.query(sql, values, (err, rows) => {
if (err) {
reject(err)
} else {
resolve(rows)
}
connection.release()
})
}
})
})
};
const queryField = ['name', 'id'];
const userId = 1;
query(`select ?? from user_info where id>?`, [['name', 'email'], userId]).then(function(data) {
console.log(JSON.stringify(data));
});
query('select ?? from user_info where password=?', [queryField, '123456']).then(function(data) {
console.log(JSON.stringify(data))
});
query(`select ?? from user_info where id = ?`, [['name'], userId]).then(function(data) {
console.log(JSON.stringify(data));
});
const result = query(`insert into user_info set ?`, {
name: 'yanle',
password: '123123123',
email: '[email protected]'
});
async function getResult() {
console.log(await result);
}