如何在mysql节点js中创建表?

时间:2018-08-15 04:47:48

标签: javascript mysql node.js knex.js

能否请您告诉我为什么未在mysql数据库中创建我的表。我想创建具有列名table player的{​​{1}}名称idname出生日期(DOB)。 这是我的代码 https://repl.it/repls/BurlywoodComplicatedParameters

lastname

还有其他方法可以在var express = require('express'), app = express(), chalk = require('chalk'), debug = require('debug')('app'), morgan = require('morgan'), path = require('path'), mysql= require('mysql'), PORT = process.env.PORT || 3000; var knex = require('knex')({ client: 'mysql', connection: { host : 'myhost', user : 'myuser', password : 'xxx', database : 'db' } }); knex.schema.withSchema('sql12252060').createTable('player', function (table) { console.log('table') }) app.use(morgan('tiny')); app.get('/', (req, res) => { res.send('dd') }); app.listen(PORT, () => { debug(`listing to ${chalk.red(PORT)}`); }); 内创建模式或表吗?我使用了mysql

1 个答案:

答案 0 :(得分:0)

还有其他方法可以在mysql内部创建架构或表吗?

是的,您可以将Sequelize与Express框架一起用于nodejs。从文档中粘贴一个示例。

NPM链接:https://www.npmjs.com/package/sequelize

const Sequelize = require('sequelize');
const sequelize = new Sequelize('database', 'username', 'password', {
  host: 'localhost',
  dialect: 'mysql'|'sqlite'|'postgres'|'mssql',

  pool: {
    max: 5,
    min: 0,
    acquire: 30000,
    idle: 10000
  },

  // SQLite only
  storage: 'path/to/database.sqlite',

  // http://docs.sequelizejs.com/manual/tutorial/querying.html#operators
  operatorsAliases: false
});

const User = sequelize.define('user', {
  username: Sequelize.STRING,
  birthday: Sequelize.DATE
});

sequelize.sync()
  .then(() => User.create({
    username: 'janedoe',
    birthday: new Date(1980, 6, 20)
  }))
  .then(jane => {
    console.log(jane.toJSON());
  });