Python MySQL插入错误必须是str,而不是attribute

2024-04-20 00:48:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个Python函数,它提取一个JSON文件,然后读取这个文件并提取某些密钥对以插入数据库。在

def update_vuln_sets():
    try:
        logging.info('Getting latest vulnerability sets from Vulners API...')
        response = requests.get('https://vulners.com/api/v3/search/stats/')
        response.encoding = 'windows-1252'
        vuln_set = json.loads(response.text)
        vuln_type = vuln_set['data']['type_results']
    except Exception as e:
        logging.error(e)
    try:
        logging.info('Processing JSON response')
                for k in vuln_type:
        vuln_bulletinfamily = vuln_set['data']['type_results'][k]['bulletinFamily']
        vuln_name = vuln_set['data']['type_results'][k]['displayName']
        vuln_count = vuln_set['data']['type_results'][k]['count']
        try:
            logging.info('Connecting to the database...')
            con = MySQLdb.connect('5.57.62.97', 'vuln_backend', 'aHv5Gz50cNgR', 'vuln_backend')
            logging.info('Database connected!')
        except FileNotFoundError as fnf:
            logging.error(fnf)
        except MySQLdb.Error as e:
            logging.error(e)
        try:
            logging.info('Inserting vulnerability type ' + k + ' into DB')
            with con:
                cur = con.cursor()
                con.row_factory = MySQLdb.row
                cur.execute("INSERT OR IGNORE INTO vuln_sets (vulntype, displayname, bulletinfamily, vulncount)"
                            " values (?,?,?,?)", (k, vuln_name, vuln_bulletinfamily, vuln_count))
                con.commit()
            logging.info('Vulnerability type ' + k + ' inserted successfully!')
            except Exception as e:
                logging.error('Vulnerability type ' + k + 'not inserted! - Error: ' + e)
        logging.info('Vulnerability sets successfully updated!')
    except Exception as e:
        logging.error(e)

通过我的日志检查,它在Inserting Record阶段被耽搁了,但是它只给出了一个root ERROR must be str, not AttributeError的错误


Tags: infodataresponseloggingastypesetsexception
1条回答
网友
1楼 · 发布于 2024-04-20 00:48:24

您正在记录e,它不是str,而是AttributeError。根据您共享的堆栈跟踪,它看起来像'日志记录.error()takes astr`参数。在

{{6>你可以用一个字符串来代替cd5}的一部分。在

相关问题 更多 >