MySQL 插入错误:ER_BAD_FIELD_ERROR:“字段列表”中的未知列“2525”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23020806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MySQL insert error : ER_BAD_FIELD_ERROR: Unknown column '2525' in 'field list'
提问by Dan P.
var convID = 2525;
var contactUsername = 'blabla';
var userId = 100;
var contactId = 200;
var sql = "INSERT INTO contacts (FK_OWNERID, FK_USERID, FC_CONTACTNAME, `CONVERSATION_ID`)
VALUES (" + mysql.escape(userId) + "," + mysql.escape(contactId) + "," + mysql.escape(contactUsername) + "," + convID + "),
(" + mysql.escape(contactId) + "," + mysql.escape(userId) + "," + mysql.escape(username) + "," + convID + ")";
`Error: ER_BAD_FIELD_ERROR: Unknown column '2525' in 'field list' Fields are varchar(32) for both CONVERSATION_ID and FC_CONTACTNAME and ints for the 2 others.
`错误:ER_BAD_FIELD_ERROR:“字段列表”中的未知列“2525”字段是 CONVERSATION_ID 和 FC_CONTACTNAME 的 varchar(32) 以及其他 2 个字段的 int。
What's wrong with this query? I even put the backticks around CONVERSATION_ID
and it thinks the convID
variable is a column...
这个查询有什么问题?我什至把反引号放在周围CONVERSATION_ID
,它认为convID
变量是一列......
Edit:
编辑:
If my query is just that, then it works:
如果我的查询就是这样,那么它的工作原理是:
var sql = "INSERT INTO contacts (`FK_OWNERID`, `FK_USERID`, `FC_CONTACTNAME`)
VALUES (" + mysql.escape(userId) + "," + mysql.escape(contactId) + "," + mysql.escape(contactUsername) + "),
(" + mysql.escape(contactId) + "," + mysql.escape(userId) + "," + mysql.escape(username) + ")";
回答by Abhik Chakraborty
make
制作
" + convID + "
to
到
'" + convID + "'
Most likely its a string and the data type is varchar so u need to enclose within single quote.
很可能它是一个字符串,数据类型是 varchar,所以你需要用单引号括起来。
回答by Brian Russell
I had a similar problem myself, I also didn't have all of my column names specified correctly. here is the code that finally worked.
我自己也遇到了类似的问题,我也没有正确指定所有列名。这是最终有效的代码。
var queryString = "INSERT INTO " + table + " (columnText, columnBool) VALUES ('" + val + "', FALSE);"
回答by Arqam.Rafay
var _name = req.body.name
var _age = req.body.age
var _table = 'people'
const _query = "INSERT INTO " + _table + " (name, age) VALUES ('" + _name + "', '" + _age + "' );"
This is work for me, main issue occur in query.
这对我有用,主要问题发生在查询中。
回答by Henri De Boever
I recently encountered a similar issue while trying to pass a parameter into a string in Node JS.
我最近在尝试将参数传递到 Node JS 中的字符串时遇到了类似的问题。
In the end, I was able to get a dynamic insertion into my MySQL database by using this line :
最后,我能够使用以下行动态插入到我的 MySQL 数据库中:
insertIntoNodeDirectory( '2', '"' + testMacAddress[0] + '"', '1');
insertIntoNodeDirectory('2', '"' + testMacAddress[0] + '"', '1');
This would enter the integer value 2, the string value contained in the testMacAddress array at index 0, and the integer value 1.
这将输入整数值 2、包含在索引 0 处的 testMacAddress 数组中的字符串值以及整数值 1。
If i used this line: insertIntoNodeDirectory( '2', '" + testMacAddress[0] + "', '1');
如果我使用这一行: insertIntoNodeDirectory( '2', '" + testMacAddress[0] + "', '1');
without the additional single quotes around testMacAddress, the value inserted into the database was the string 'testMacAddress[0]'. Not what I wanted.
如果没有围绕 testMacAddress 的额外单引号,插入到数据库中的值是字符串 'testMacAddress[0]'。不是我想要的。