You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用None插入Google BigQuery DB-API时出现意外类型错误的原因?

问题:BigQuery插入NULL值时遇ProgrammingError错误

尝试使用Python将MySQL表数据迁移至Google BigQuery表时,插入NULL值触发以下错误:

google.cloud.bigquery.dbapi.exceptions.ProgrammingError: Encountered parameter None with value None of unexpected type.

相关代码如下:

from dotenv import load_dotenv
load_dotenv()  ##GOOGLE_APPLICATION_CREDENTIALS = 'api/google.json'
from google.cloud import bigquery
from google.cloud.bigquery import dbapi
import datetime

client = bigquery.Client()
connection = dbapi.Connection(client)

def write(query, data):
    cursor = connection.cursor()
    cursor.execute(query, data)
    connection.commit()
    return 'OK!'

data = (9, '857', '11013', None, datetime.datetime(2022, 9, 28, 15, 33, 13), datetime.datetime(2022, 9, 28, 15, 33, 13))

query = """
INSERT INTO `<project>.<dataset>.tmp_accountContacts`
(
id,account,contact,jobTitle,createdTimestamp,updatedTimestamp)
VALUES (%s,%s,%s,%s,%s,%s);
"""

write(query, data)
原因分析

BigQuery的dbapi驱动不支持直接传入Python原生的None作为NULL值参数。当你在参数中传递None时,驱动无法自动识别其对应的BigQuery数据类型,因此抛出类型不匹配的错误。

解决时需要用google.cloud.bigquery.dbapi.types.NULL替代None,或者通过明确指定参数类型的方式,让驱动能正确将空值转换为BigQuery可识别的NULL格式。

内容的提问来源于stack exchange,提问作者croder

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 18:22:13