try: cursor.execute(aa) data = cursor.fetchall() db.commit() print("执行成功") return data except: print("操作失误,输入格式不正确") db.rollback() finally: print("关闭db") db.close() except里的print不输出是什么问题
时间: 2024-02-14 14:04:42 浏览: 32
如果 except 块中的 print 语句没有输出,有可能是因为程序在执行 except 块时出现了异常,导致程序无法正常执行到该语句。为了找到问题所在,你可以尝试在 except 块中加入更多的调试信息,例如:
```python
except Exception as e:
print("操作失误,发生异常:{}".format(e))
db.rollback()
```
这样可以输出更详细的错误信息,帮助你找到程序中的问题。同时,你也可以在外层再加一个 try-except 语句,用于捕捉 except 块中的异常,例如:
```python
try:
# 执行数据库操作
except Exception as e:
try:
print("操作失误,发生异常:{}".format(e))
except:
pass
db.rollback()
finally:
print("关闭db")
db.close()
```
这样可以确保无论程序发生什么异常,都能够输出正确的信息并关闭数据库连接。
相关问题
try: cursor.execute('SELECT * FROM %s WHERE content= "%s"' % (cfg.TABLE_NAME, ip)) q_result = cursor.fetchall()
这段代码看起来是使用 Python 的 MySQLdb 模块来执行 SQL 查询语句,查询某个表中特定内容的记录。其中 %s 是占位符,可以在后面的参数中传入具体值。在这里,第一个 %s 会被替换成表名,第二个 %s 会被替换成 IP 地址。然后使用 cursor.fetchall() 方法获取查询结果。需要注意的是,这种字符串拼接 SQL 语句的方式存在 SQL 注入漏洞,建议使用参数化查询来避免此类漏洞。
import pymysqlclass MySQL: def __init__(self, host='localhost', port=3306, user='root', password='', database=None): self.host = host self.port = port self.user = user self.password = password self.database = database def __enter__(self): self.conn = pymysql.connect(host=self.host, port=self.port, user=self.user, password=self.password, database=self.database) return self def __exit__(self, exc_type, exc_val, exc_tb): self.conn.close() def select_all(self, table): with self.conn.cursor() as cursor: cursor.execute(f'select * from {table}') result = cursor.fetchall() return result def update(self, table, data, condition): with self.conn.cursor() as cursor: set_clause = ', '.join([f'{k}="{v}"' for k, v in data.items()]) cursor.execute(f'update {table} set {set_clause} where {condition}') self.conn.commit() return cursor.rowcount def delete(self, table, condition): with self.conn.cursor() as cursor: cursor.execute(f'delete from {table} where {condition}') self.conn.commit() return cursor.rowcount def insert(self, table, data): with self.conn.cursor() as cursor: keys = ', '.join(data.keys()) values = ', '.join([f'"{v}"' for v in data.values()]) cursor.execute(f'insert into {table} ({keys}) values ({values})') self.conn.commit() return cursor.lastrowid
好的,这是一个MySQL数据库操作类,具有基本的增删改查功能。您可以按照以下步骤进行测试:
1. 首先,您需要连接到MySQL数据库,可以使用以下代码:
```
with MySQL(host='localhost', user='root', password='your_password', database='your_database') as db:
# do something with db object
```
请将`your_password`和`your_database`替换为您的实际密码和数据库名称。
2. 接下来,您可以使用以下代码进行查询:
```
result = db.select_all('your_table_name')
print(result)
```
请将`your_table_name`替换为您的实际数据表名称。
3. 您可以使用以下代码进行更新操作:
```
data = {'column1': 'new_value1', 'column2': 'new_value2'}
condition = 'id=1'
rows_affected = db.update('your_table_name', data, condition)
print(f'{rows_affected} rows affected')
```
请将`column1`, `new_value1`, `column2`, `new_value2`和`your_table_name`替换为您的实际列名、新值和数据表名称。`condition`参数用于指定更新条件。
4. 您可以使用以下代码进行删除操作:
```
condition = 'id=1'
rows_affected = db.delete('your_table_name', condition)
print(f'{rows_affected} rows affected')
```
请将`your_table_name`替换为您的实际数据表名称。`condition`参数用于指定删除条件。
5. 最后,您可以使用以下代码进行插入操作:
```
data = {'column1': 'value1', 'column2': 'value2'}
last_insert_id = db.insert('your_table_name', data)
print(f'Last insert id: {last_insert_id}')
```
请将`column1`, `value1`, `column2`, `value2`和`your_table_name`替换为您的实际列名和值。插入操作将返回最后插入的行的ID。
相关推荐
![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)
![](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)