Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 为什么我会得到“的语法错误?”;插入到“;?_Sql_Postgresql_Jdbc - Fatal编程技术网

Sql 为什么我会得到“的语法错误?”;插入到“;?

Sql 为什么我会得到“的语法错误?”;插入到“;?,sql,postgresql,jdbc,Sql,Postgresql,Jdbc,我得到: String pName = getStrFromUser("Product name: "); int price = getIntFromUser("Price: ", false); String category = getStrFromUser("Category: "); String description = getStrFromUser("Description: "); PreparedStatement statement = connection.prepar

我得到:

String pName = getStrFromUser("Product name: ");
int price = getIntFromUser("Price: ", false);
String category = getStrFromUser("Category: ");
String description = getStrFromUser("Description: ");

PreparedStatement statement = connection.prepareStatement("INSERT INTO ws.products (name, price, cid, description) VALUES (?, ?, (SELECT ws.categories.cid FROM ws.categories WHERE ws.categories.name LIKE ?), ?)");

statement.setString(1, pName);
statement.setInt(2, price);
statement.setString(3, category);
statement.setString(4, description);
statement.executeUpdate();

可能是什么问题?

子句内的子查询看起来可疑。尝试将
插入到。。。选择

Error encountered: ERROR: syntax error at or near "INSERT INTO ws

VALUES
子句中的子查询看起来可疑。尝试将
插入到。。。选择

Error encountered: ERROR: syntax error at or near "INSERT INTO ws

我建议
插入。选择

String sql = "INSERT INTO ws.products (name, price, cid, description) ";
sql += "SELECT ?, ?, cid, ? FROM ws.categories WHERE name LIKE ?";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1, pName);
statement.setInt(2, price);
statement.setString(3, description);
statement.setString(4, category);
statement.executeUpdate();
这不会解决
INSERT
的问题,但会防止子查询返回多行的下一个问题


我对这个问题的最佳猜测是,您正在使用的库只支持
SELECT
语句。这将是非典型的<代码>插入通常是允许的。

我建议
插入。选择

String sql = "INSERT INTO ws.products (name, price, cid, description) ";
sql += "SELECT ?, ?, cid, ? FROM ws.categories WHERE name LIKE ?";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1, pName);
statement.setInt(2, price);
statement.setString(3, description);
statement.setString(4, category);
statement.executeUpdate();
这不会解决
INSERT
的问题,但会防止子查询返回多行的下一个问题


我对这个问题的最佳猜测是,您正在使用的库只支持
SELECT
语句。这将是非典型的<代码>插入通常是允许的。

你能分享完整的堆栈跟踪吗请你使用哪个RDB?它只打印“遇到的错误:错误:语法错误在或接近”插入到ws“位置:1”我假设它的语法接近,因为like的语法是
like'
。。它需要有引号I use postgresql您可以共享完整的stacktrace吗请确定您使用的是哪种RDB?它只打印“遇到的错误:错误:语法错误在或接近”INSERT to ws“位置:1”我假设它接近,因为like的语法是
like'
。。它需要有quotesI使用PostgreSQL