简体   繁体   English

java.sql.SQLException:接近“ ON”:语法错误

[英]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. 这就是您收到此错误的原因。

Check this question for a workaround. 检查问题以解决。

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.

相关问题 java.sql.SQLException:在“。”附近:语法错误 - java.sql.SQLException: near “.”: syntax error java.sql.SQLException:在“。”附近:语法错误 - java.sql.SQLException: near “.”: syntax error 错误java.sql.SQLException:接近“ - ”:语法错误 - ERROR java.sql.SQLException: near “-”: syntax error java.sql.SQLException:在“ on”附近:语法错误问题 - java.sql.SQLException: near “on”: syntax error problem java.sql.SQLException:[SQLITE_ERROR] SQL错误或缺少数据库(“ ID”附近:语法错误) - java.sql.SQLException:[SQLITE_ERROR] SQL error or missing database (near “ID”:syntax error) java.sql.SQLException:[SQLITE_ERROR] SQL错误或缺少数据库(“名称”附近:语法错误) - java.sql.SQLException: [SQLITE_ERROR] SQL error or missing database (near “Name”: syntax error) java.sql.SQLException:[SQLITE_ERROR] SQL错误或缺少数据库(接近“=”:语法错误) - java.sql.SQLException:[SQLITE_ERROR] SQL error or missing database (near “=”:syntax error) java.sql.SQLException:语法错误 - java.sql.SQLException: Syntax Error “嵌套异常是java.sql.SQLException:'?'附近的语法不正确 ” - “ nested exception is java.sql.SQLException: Incorrect syntax near '?' ” java.sql.SQLException:关键字“ top”附近的语法不正确 - java.sql.SQLException: Incorrect syntax near the keyword 'top'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM