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

zh-CN Chinese (Simplified)
X
free_banner