AttributeError: 'Cursor' object has no attribute 'commit'
时间: 2023-10-05 15:15:04 浏览: 41
这个错误通常发生在尝试使用`commit()`方法来提交数据库事务时。根据错误消息,它看起来是在一个`Cursor`对象上调用了`commit()`方法,但是`Cursor`对象没有这个属性。
要解决这个问题,你需要确保你调用`commit()`方法的是一个数据库连接对象(如`connection.commit()`),而不是一个游标对象。请确保你的代码中正确地实例化和使用了数据库连接对象。如果你能提供更多的代码细节,我可以给你更具体的建议。
相关问题
AttributeError: 'pymssql.Cursor' object has no attribute 'commit'
对不起,我之前给出的回答中有误。实际上,`pymssql.Cursor` 对象没有 `commit` 方法。在 `pymssql` 中,提交事务的操作是通过数据库连接对象来完成的。
以下是使用 `pymssql` 将数据插入数据库并提交事务的正确示例:
```python
import pymssql
conn = pymssql.connect(server='服务器名称', user='用户名', password='密码', database='数据库名称')
cursor = conn.cursor()
sql = "INSERT INTO 表名 (列1, 列2, 列3) VALUES (%s, %s, %s)"
values = ('值1', '值2', '值3')
cursor.execute(sql, values)
conn.commit()
cursor.close()
conn.close()
```
请注意,事务的提交是通过调用数据库连接对象 `conn` 的 `commit()` 方法实现的,而不是 `cursor` 对象。在执行完所有数据库操作后,使用 `conn.commit()` 来提交事务。
对于 `pymssql`,另外一个事务的自动提交设置是可选的,默认情况下事务会自动提交。如果你希望手动控制事务的提交和回滚,可以将 `autocommit` 属性设置为 `False`:
```python
conn.autocommit(False)
```
这样,你需要在适当的时候调用 `conn.commit()` 来手动提交事务,并通过 `conn.rollback()` 来回滚事务。
AttributeError: 'MysqlPipeline' object has no attribute 'cursor'
个错误通常是由于代码中使用了错误的属性或方法名称导致的。在这个例子中,错误信息指出'MysqlPipeline'对象没有'cursor'属性。这可能是因为代码中使用了错误的对象名称或方法名称。要解决这个问题,你可以尝试以下几个步骤:
1.检查代码中是否存在拼写错误或语法错误。
2.检查代码中是否使用了正确的对象名称和方法名称。
3.检查代码中是否正确导入了所需的模块和库。
4.检查代码中是否正确初始化了数据库连接。
5.检查代码中是否正确执行了查询或插入操作。
下面是一个可能的解决方案:
```python
import pymysql
class MysqlPipeline:
def __init__(self):
self.conn = pymysql.connect(host="localhost", user="root", password="password", database="test")
self.cursor = self.conn.cursor()
def process_item(self, item, spider):
sql = "INSERT INTO table_name (column1, column2, column3) VALUES (%s, %s, %s)"
self.cursor.execute(sql, (item['value1'], item['value2'], item['value3']))
self.conn.commit()
return item
def close_spider(self, spider):
self.cursor.close()
self.conn.close()
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![png](https://img-home.csdnimg.cn/images/20210720083516.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)