gpt4 book ai didi

python - 尝试了其他方法,还是报错TypeError : 'str' object is not callable

转载 作者:行者123 更新时间:2023-11-29 05:01:28 25 4
gpt4 key购买 nike

我尝试改变数据库的字段,修改了'sql'语句,但是还是报错。

#!/usr/bin/env python3

import pymysql
from log import *
#连接数据库
db = pymysql.connect('localhost', 'root', '123456', "test")
cursor = db.cursor()
def save_get_xici_log(log_data):
#抓取日志存入数据库
for data in log_data:
status = log_data[0]
thread_name = log_data[1]
url = log_data[2]
code = log_data[3]
proxy = log_data[4]
speedtime = log_data[5]
memo = log_data[6]

#status级别
if status == 'sucess':
status = 1
elif status == 'failed':
status = 0
elif status == 'error':
status = -1
else:
pass


sql = "INSERT INTO get_xici(thread_name, url, proxy, status, code, speedtime, memo) VALUES('%s', '%s', '%s', '%s', '%d', '%f', '%s')" (thread_name, url, proxy, status, code, speedtime, memo)
try:
cursor.execute(sql)
db.commit()
log.logger.info('save_get_xici_log is sucess')
except Exception as e:
db.rollback()
log.logger.debug('save_get_xici_log is error'+ ' '+ str(e))
db.close()


if __name__ == '__main__':
log = Logger(r'/root/Project/freeProxy/log/save_mysql.log',level='debug')
log_data = 'sucess', 'thread-1', 'baidu.com', 200, '1.1.1.1', 0.823132, '1'
save_get_xici_log(log_data)


######### MYSQL desc get_xici; #######################
mysql> desc get_xici;
+-------------+------------------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------------+------+-----+-------------------+-----------------------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| thread_name | varchar(20) | NO | | NULL | |
| url | varchar(20) | NO | | NULL | |
| proxy | varchar(20) | NO | | NULL | |
| status | tinyint(2) | NO | | NULL | |
| code | int(3) | NO | | NULL | |
| speedtime | float(3,2) | NO | | NULL | |
| memo | varchar(500) | NO | | NULL | |
| nowtime | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+-------------+------------------+------+-----+-------------------+-----------------------------+

#

错误信息:

Traceback (most recent call last): File "save_mysql.py", line 106, in save_get_xici_log(log_data) File "save_mysql.py", line 92, in save_get_xici_log sql = "INSERT INTO get_xici(thread_name, url, proxy, status, code, speedtime, memo) VALUES('%s', '%s', '%s', '%s', '%d', '%f', '%s')" (thread_name, url, proxy, status, code, speedtime, memo) TypeError: 'str' object is not callable

最佳答案

我认为您需要在这一行的字符串和参数之间添加一个 %:

sql = "INSERT INTO get_xici(thread_name, url, proxy, status, code, speedtime, memo) VALUES('%s', '%s', '%s', '%s', '%d', '%f', '%s')" (thread_name, url, proxy, status, code, speedtime, memo)

到:

sql = "INSERT INTO get_xici(thread_name, url, proxy, status, code, speedtime, memo) VALUES('%s', '%s', '%s', '%s', '%d', '%f', '%s')" % (thread_name, url, proxy, status, code, speedtime, memo)      

但一般来说,您应该使用 prepared statements出于安全原因(防止 SQL 注入(inject)),而不是用于生成 SQL 查询的字符串插值。

关于python - 尝试了其他方法,还是报错TypeError : 'str' object is not callable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58164435/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com