优化conn.execute("update feedback set time='%s',Feedbacksort='%s',good='%s',bad='%s',popular='%s',unwelcome='%s' where ID='%s'"%(time,Feedbacksort,good,bad,popular,unwelcome)) TypeError: not enough arguments for format string
时间: 2023-07-31 14:13:31 浏览: 43
这个错误是因为你在字符串格式化时使用了不正确的格式。在字符串中使用 %s 表示插入一个字符串类型的变量,但是在你的字符串中使用了6个 %s,但是你只提供了7个变量值,缺少了一个变量。
你可以尝试使用如下代码:
conn.execute("update feedback set time='%s', Feedbacksort='%s', good='%s', bad='%s', popular='%s', unwelcome='%s' where ID='%s'" % (time, Feedbacksort, good, bad, popular, unwelcome, ID))
这里,我们在字符串末尾添加了一个 %s,用来表示 ID 这个变量的值。
相关问题
conn.execute("update financeinfo set monthoftheyear='%s',Grossincome='%d', where ID='%s'"%(monthoftheyear,Monthlyincome,Grossincome,ID)) TypeError: not all arguments converted during string formatting
这错误通常是由于字符串格式化时格式化字符串中的占位符与传入的参数数量或类型不匹配导致的。在你的代码中,可能是因为你在 `Grossincome='%d',` 语句中漏掉了一个参数,应该是 `Grossincome='%d',具体看一下你的代码,看看有没有传入足够的参数或者参数类型是否正确。另外,你也可以考虑使用参数化查询来避免这种错误。例如:
```
conn.execute("update financeinfo set monthoftheyear=?, Grossincome=? where ID=?", (monthoftheyear, Monthlyincome, ID))
```
優化conn.execute("update Incoming set supplie='%s', time='%s', address='%s', phone='%s', Purchaseprice='%s', Incominggoods='%s', Importedvariety='%s' where ID='%s'" % (,supplie,time, address, phone, Purchaseprice, Incominggoods, Importedvariety, ID))
这段代码中存在 SQL 注入的风险,可以使用参数化查询来避免。例如:
```
conn.execute("UPDATE Incoming SET supplie=?, time=?, address=?, phone=?, Purchaseprice=?, Incominggoods=?, Importedvariety=? WHERE ID=?",
(supplie, time, address, phone, Purchaseprice, Incominggoods, Importedvariety, ID))
```
这样可以将变量作为参数传递给 SQL 语句,避免了字符串拼接导致的注入问题,并且可以提高执行效率。
相关推荐
![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)