繁体   English   中英

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

[英]Python MySQL Insert error must be str, not AttributeError

我有一个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

您正在记录的e不是str ,而是AttributeError 根据您共享的堆栈跟踪信息,看起来'logging.error() takes a str`参数。

您可以更改logging.error()以接受Exception而不是字符串,也可以调用将错误的字符串部分记录下来。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM