[英]java.sql.SQLException: near “ON”: syntax error
Hey guys I have scoured the many other answers and am still unable to figure out this problem. 嗨,我已经搜寻了许多其他答案,但仍然无法弄清楚这个问题。
statement.executeUpdate("CREATE TABLE IF NOT EXISTS recentlyWatched (fileName STRING NOT NULL, fileLocation STRING, viewCount INTEGER, PRIMARY KEY (fileName))");
Class.forName("org.sqlite.JDBC");
connection = DriverManager.getConnection("jdbc:sqlite:media.db");
statement = connection.createStatement();
statement.executeUpdate("INSERT INTO recentlyWatched (fileName, fileLocation, viewCount) VALUES ('"
+ fileName + "', '"
+ fileLocation + "', 0) ON DUPLICATE KEY UPDATE viewCount = '1'");
I have tried numerous ways to resolve this issue and am at a loss whenever i try to insert a record i get "java.sql.SQLException: near "ON": syntax error". 我尝试了多种方法来解决此问题,每当我尝试插入一条记录时,我都会感到茫然不知所措:“ java.sql.SQLException:在“ ON”附近:语法错误”。
Update 更新资料
Final code looked like this 最终代码如下所示
statement.executeUpdate("INSERT OR IGNORE INTO recentlyWatched (fileName, fileLocation, viewCount) VALUES ('"
+ fileName + "', '"
+ fileLocation + "', 0)");
statement.executeUpdate("UPDATE recentlyWatched SET viewCount = viewCount + 1 WHERE '" + fileName + "' LIKE fileName;");
As one can tell from Class.forName("org.sqlite.JDBC")
you are using SQLite database which doesn't support the ON DUPLICATE
clause. 从Class.forName("org.sqlite.JDBC")
可以看出,您正在使用不支持ON DUPLICATE
子句的SQLite数据库。 This is the reason why you get this error. 这就是您收到此错误的原因。
As an alternative I would suggest using it like this 作为替代方案,我建议像这样使用它
("REPLACE INTO recentlyWatched (fileName, fileLocation, viewCount) VALUES ('"
+ fileName + "', '"
+ fileLocation + "', 0)
The difference is that is does a DELETE
and then an INSERT
区别在于先执行DELETE
然后执行INSERT
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.