我创建了一个sequelize raw query helper函数,它将插入mysql数据库中的数据。
const addBudget = function(name, amount) {
sequelize.query(
'INSERT INTO budget (name, amount) VALUES(' + name + ',' + amount + ')', {
type: Sequelize.QueryTypes.INSERT
}
).then(function (data) {
console.log('inserted STEAMER data---> ', data); });
};
然后,当我调用函数尝试在mysql数据库中插入一些数据以查看它是否将my数据插入到记录中时:
addBudget('Book', 100);
我有个错误:
Executing (default): SELECT 1+1 AS result
Executing (default): INSERT INTO budget (name, amount) VALUES(Book,100)
Unhandled rejection SequelizeDatabaseError: Unknown column 'Book' in 'field list'
at Query.formatError (/Users/mac/Documents/dudes/moira/node_modules/sequelize/lib/dialects/mysql/query.js:247:16)
at Query.handler [as onResult] (/Users/mac/Documents/dudes/moira/node_modules/sequelize/lib/dialects/mysql/query.js:68:23)
at Query.Command.execute (/Users/mac/Documents/dudes/moira/node_modules/mysql2/lib/commands/command.js:30:12)
at Connection.handlePacket (/Users/mac/Documents/dudes/moira/node_modules/mysql2/lib/connection.js:502:28)
at PacketParser.onPacket (/Users/mac/Documents/dudes/moira/node_modules/mysql2/lib/connection.js:81:16)
at PacketParser.executeStart (/Users/mac/Documents/dudes/moira/node_modules/mysql2/lib/packet_parser.js:77:14)
at Socket.<anonymous> (/Users/mac/Documents/dudes/moira/node_modules/mysql2/lib/connection.js:89:29)
at Socket.emit (events.js:180:13)
at addChunk (_stream_readable.js:274:12)
at readableAddChunk (_stream_readable.js:261:11)
at Socket.Readable.push (_stream_readable.js:218:10)
at TCP.onread (net.js:581:20)
你知道当我把数据插入数据库的时候我做错了什么吗?
3条答案
按热度按时间ifmq2ha21#
你试过这个吗:
执行insert和updatesql并获取更多信息
wmvff8tz2#
sql只允许对值使用单引号字符串。所以,这是行不通的:
这也不会:
假设您指示sql将金额读取为整数,这是插入值的正确方法:
如果book只是一个已经是字符串的变量,也可以这样做以确保保留单引号规则:
zengzsys3#
这里的问题是,您的值应该包括b/w单引号或双引号,但与您的代码生成
VALUES(Book,100);
,应该是VALUES('Book','100');
,为此只需更改以下行:
有了这个: