def clear_tables_null(self, table_name, field='价格'): print(table_name,field) #已知可能被注入 query = "delete from %s where %s is null" % (table_name,field) #??? query = "delete from %s where %s is null" self.cursors.execute(query, (table_name,field)) self.conn.commit()这个语句已经是错误的语句,请问我错在哪个格式
时间: 2024-01-01 13:04:13 浏览: 30
这个语句中的第二个参数 `(table_name, field)` 应该是一个元组,用于替换 SQL 语句中的 `%s` 占位符。因此,应该将第二个参数改为 `(field,)`,即使只有一个元素的元组也必须在元素后面加上逗号,否则会被认为是字符串而不是元组。修改后的代码如下:
```python
def clear_tables_null(self, table_name, field='价格'):
print(table_name, field)
# 已知可能被注入
query = "delete from %s where %s is null"
self.cursors.execute(query, (table_name, field,))
self.conn.commit()
```
注意:为了防止 SQL 注入攻击,这个方法最好使用参数化查询,而不是通过字符串拼接构造 SQL 语句。可以将 SQL 语句中的 `%s` 占位符替换为 `?`,然后将参数作为元组传递给 `execute` 方法,例如:
```python
query = "delete from %s where %s is null"
self.cursors.execute(query, (table_name, field,))
```
相关问题
class Student: def __init__(self, sid=None,name=None,height=None,sex=None): self.sid=sid self.name=name self.height=height self.sex=sex def print(): #补充代码,打印本同学的全部信息
补充代码如下:
```
class Student:
def __init__(self, sid=None, name=None, height=None, sex=None):
self.sid = sid
self.name = name
self.height = height
self.sex = sex
def print_info(self):
print("学号:", self.sid)
print("姓名:", self.name)
print("身高:", self.height)
print("性别:", self.sex)
```
在原有的代码基础上,添加了一个 `print_info` 方法,用于打印学生的全部信息。注意,这个方法需要加上 `self` 参数,表示当前实例对象。在方法体内,使用 `print` 函数依次打印各个属性的值即可。
优化这段代码 class Person: def __int__(self, name): self.name = name a = Person() print(a.name)
A:
这个类的构造函数有一个错别字,应该是 __init__ 而不是 __int__。
修复后的代码为:
class Person:
def __init__(self, name):
self.name = name
a = Person("ChitGPT")
print(a.name)
另外,为了创建一个 Person 对象,需要在实例化时传递一个参数来指定名字。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)