expression cannot be assignment target
时间: 2023-04-27 16:01:57 浏览: 207
这句话的意思是“表达式不能被赋值为目标”。它通常出现在代码中,表示在赋值语句中,左侧的表达式不是一个合法的赋值目标。可能是因为它是一个常量、一个函数调用、一个表达式的结果等等。这时需要检查代码,找出错误并进行修正。
相关问题
SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
这个错误是因为在 Python 中,赋值语句不能被用作表达式。也就是说,你不能在一个条件语句中使用赋值语句。
例如,下面的代码会导致 `SyntaxError: expression cannot contain assignment` 错误:
```
if x = 5:
print("x is 5")
```
正确的写法应该是使用相等运算符 `==` 来比较值,像这样:
```
if x == 5:
print("x is 5")
```
在这个例子中,我们使用相等运算符 `==` 来检查 `x` 是否等于 `5`。这个条件语句可以正常运行,因为它不包含赋值语句。
我使用了你上面的代码 但是出现了如下报错expression '<null>' cannot be used as an assignment target
非常抱歉,我之前的代码有一个错误。在代码中,我使用了一个叫做 `getDatabaseChangeRegistration()` 的方法,但是实际上这个方法应该是不存在的。这可能导致了你遇到的报错。
为了解决这个问题,你可以按照以下方法修改代码:
1. 将 `getDatabaseChangeRegistration()` 方法的调用改为直接使用 `dcr` 对象:
```java
// 注册Oracle数据库变化监听器
OracleConnection connection = registerDatabaseChangeListener();
// 在连接上执行SQL语句以触发数据库变化事件
executeSQL(connection);
// 关闭连接和监听器
connection.unregisterDatabaseChangeNotification(dcr);
connection.close();
```
2. 将 `registerDatabaseChangeListener()` 方法中的 `getDatabaseChangeRegistration()` 方法删除掉,并直接返回 `dcr` 对象:
```java
private static OracleConnection registerDatabaseChangeListener() throws SQLException {
OracleConnection connection = null;
DatabaseChangeRegistration dcr = null;
try {
// 创建数据库连接
connection = (OracleConnection) DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
// 创建数据库变化注册
dcr = connection.registerDatabaseChangeNotification(getDCNProperties());
// 添加监听器
DatabaseChangeListener listener = new DatabaseChangeListener() {
public void onDatabaseChangeNotification(DatabaseChangeEvent e) {
if (e.getTableChangeDescription()[0].getRowChangeDescription()[0].getRowOperation() == RowOperation.INSERT) {
System.out.println("有新行插入!");
} else if (e.getTableChangeDescription()[0].getRowChangeDescription()[0].getRowOperation() == RowOperation.UPDATE) {
System.out.println("有行更新!");
} else if (e.getTableChangeDescription()[0].getRowChangeDescription()[0].getRowOperation() == RowOperation.DELETE) {
System.out.println("有行删除!");
}
}
};
dcr.addListener(listener);
// 开启监听
connection.setAutoCommit(true);
return connection;
} catch (Exception e) {
if (connection != null) {
connection.unregisterDatabaseChangeNotification(dcr);
connection.close();
}
throw e;
}
}
```
这样修改后,应该可以解决你遇到的报错问题。非常抱歉给你带来了困扰。如果还有其他问题,请随时提问。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)