在 Python 函数中调用字符串时出现“TypeError: must be str, not NoneType”错误

时间:2021-05-05 19:48:33

标签: python-3.x string function typeerror nonetype

这是一个将作业状态写入 MySQL 进程日志的函数:

driver.find_element_by_css_selector("div.flex.flex-column")

我收到错误:

# job status function        
def mysql_write_jobstatus(host, user, password, batch_id, source, status,
                          job_start_dt, job_finish_dt = None, filename = None, error = None):
    if job_finish_dt is None:
        status_sql = "INSERT INTO schema.table (\
        batch_id, run_date, source, status, data_processed, job_start_dt)\
        values ('" + batch_id + "', '" + rundate + "', '" + source + "', '" + status + "', '" + data_processed + \
                "', '" + job_start_dt + "');"
    else:
        status_sql = "UPDATE schema.table SET status = '" + status + "', \
        job_finish_dt = '" + job_finish_dt + "', filename = '" + filename + "', error = '" + error + "' WHERE batch_id = '" + batch_id + "' AND \
        source = '" + source + "';"
    try:
        con = pymysql.connect(host=lol,
                                user=lol,
                                password=lol,
                                autocommit=True,
                                local_infile=1)
        print('Connected to DB: {}'.format(host))
        # Create cursor and execute Load SQL
        cursor = con.cursor()
        cursor.execute(status_sql)
        con.close()
        
    except Exception as e:
        print('Error: {}'.format(str(e)))
        sys.exit(1)

source 是一个字符串变量,它只在代码顶部定义一次,如下所示:

  File "<ipython-input-222-0d62a2bb45ad>", line 75, in mysql_write_jobstatus
    source = '" + source +"';"

TypeError: must be str, not NoneType

我所看到的一切都让我相信这是一个字符串。同一个 SQL 调用中的其他被调用变量按预期工作。

我已从全局环境中删除了所有变量。

有什么想法吗?

easygui.msgbox ('You entered ' + flavour) TypeError: must be str, not NoneType 没有帮助,因为 source 是一个手动定义的字符串,永远不应更改为 NoneType。

0 个答案:

没有答案