Node.js MySQL创建表

使用”CREATE TABLE”创建表

首先我们创建一个customers的表


var mysql = require('mysql');

var con = mysql.createConnection({
host: "localhost",
user: "yourusername",
password: "yourpassword",
database: "mydb"
});

con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
var sql = "CREATE TABLE customers (name VARCHAR(255), address VARCHAR(255))";
con.query(sql, function (err, result) {
if (err) throw err;
console.log("Table created");
});
});

保存文件名”demo_create_table.js” 然后再运行


node demo_create_table.js

如果正确会出现


Connected!
Table created

Leave a Comment

您的邮箱地址不会被公开。 必填项已用 * 标注

🚀

RedGate VPN

免费节点太挤太慢?
升级高速稳定专线

立即体验 →

告别卡顿

RedGate VPN
全球高速节点

免费下载 →
Scroll to Top