describe mytable; Field Type Null Key Defaul-6ren">
gpt4 book ai didi

Java/MySQL : Insert date gives "Unknown column ' date'"even though its there (Error 1054 (42s22))

转载 作者:行者123 更新时间:2023-11-29 11:20:32 24 4
gpt4 key购买 nike

有人可以告诉我我在这里做错了什么吗?我有一个这样的表:

mysql> describe mytable;
Field Type Null Key Default Extra
-----------+----------+--------+--------+------------+---------------
id int(11) NO PRI NULL auto_increment
foreignid int(11) NO MUL NULL
date date NO '0000-00-00'
[some more columns]

我正在尝试通过准备好的语句插入值:

String insert = "insert into mytable (foreignid, date) values (?, ?)";
PreparedStatement pst = connection.prepareStatement(insert);
pst.setInt(1, newForeignID); //valid foreign key
pst.setDate(2, newDate); //newDate is a proper java.sql.Date
pst.execute();

这应该相当简单,但我总是收到此错误:

Unknown column 'date' in 'field list'

知道为什么会发生这种情况吗?

最佳答案

date是mysql中的一个关键字。您应该逃避或更好地重命名它:

String insert = "insert into mytable (foreignid, `date`) values (?, ?)";

关于Java/MySQL : Insert date gives "Unknown column ' date'"even though its there (Error 1054 (42s22)),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39001949/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com