Skip to content

Instantly share code, notes, and snippets.

@Leask
Created January 18, 2020 21:26
Show Gist options
  • Save Leask/0537aa132778df93b8780d84607cb38a to your computer and use it in GitHub Desktop.
Save Leask/0537aa132778df93b8780d84607cb38a to your computer and use it in GitHub Desktop.
'use strict';
// CREATE TABLE `test`(
// `id` BIGINT(20) NOT NULL AUTO_INCREMENT,
// `content` TEXT NOT NULL,
// PRIMARY KEY(`id`)
// ) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4;
const util = require('util');
const mysql = require('mysql2').createPool({
host: '**********',
port: 3306,
database: '**********',,
user: '**********',,
password: '**********',,
charset: 'utf8mb4',
});
const query = util.promisify(mysql.query.bind(mysql));
const execute = util.promisify(mysql.execute.bind(mysql));
(async () => {
// 准备数据
await execute('DELETE FROM `test`');
await execute('INSERT INTO `test` SET `id` = 1, `content` = "anything"');
await execute('INSERT INTO `test` SET `id` = 2, `content` = "anything"');
await execute('INSERT INTO `test` SET `id` = 3, `content` = "anything"');
await execute('INSERT INTO `test` SET `id` = 4, `content` = "anything"');
await execute('INSERT INTO `test` SET `id` = 5, `content` = "anything"');
await execute('INSERT INTO `test` SET `id` = 6, `content` = "anything"');
await execute('INSERT INTO `test` SET `id` = 7, `content` = "anything"');
await execute('INSERT INTO `test` SET `id` = 8, `content` = "anything"');
await execute('INSERT INTO `test` SET `id` = 9, `content` = "anything"');
// 测试前检查
const a = await query('SELECT * FROM `test`');
console.log('ALL: ', a);
// 测试直接删除
await execute('DELETE FROM `test` WHERE `id` IN (?)', [[5, 6, 7, 8, 9]]);
const b = await query('SELECT * FROM `test`');
console.log('TEST DEL 1: ', b);
// // 测试直接删除
// await query('DELETE FROM `test` WHERE `id` IN (?)', [[5, 6, 7, 8, 9]]);
// const e = await query('SELECT * FROM `test`');
// console.log('TEST DEL 3: ', e);
// // 测试先格式化SQL再删除
// await execute(mysql.format('DELETE FROM `test` WHERE `id` IN (?)', [[5, 6, 7, 8, 9]]));
// const c = await query('SELECT * FROM `test`');
// console.log('TEST DEL 2: ', c);
process.exit();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment