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

GridDB Python API插入数据无报错但容器为空,求排查方案

问题分析与解决方案

核心问题:put方法参数格式错误

你当前传入put方法的是顺序列表(比如[1, "John"]),但GridDB的COLLECTION类型容器要求传入字典,需要明确映射字段名和对应的值。单纯的列表无法和容器定义的id、name字段关联,导致数据没有被正确写入,且不会触发报错。

额外注意:事务提交

GridDB默认开启自动提交,但显式调用commit()能确保数据立即持久化,避免因连接关闭前未完成提交导致的数据丢失。


修正后的代码

import griddb_python as griddb

factory = griddb.StoreFactory.get_instance()
store = factory.get_store(
    host='localhost',
    port=2406,
    cluster_name='mycluster',
    username='admin',
    password='admin'
)

conInfo = griddb.ContainerInfo("mycontainer",
                               [["id", griddb.Type.INTEGER],
                                ["name", griddb.Type.STRING]],
                               griddb.ContainerType.COLLECTION, True)

con = store.get_container(conInfo)

# 改用字典格式传入数据,匹配容器字段名
con.put({"id": 1, "name": "John"})
con.put({"id": 2, "name": "Jane"})

# 显式提交事务,确保数据持久化
store.commit()

批量插入优化

如果需要插入多条数据,推荐使用multi_put方法,效率更高:

# 批量传入字典列表
con.multi_put([{"id": 1, "name": "John"}, {"id": 2, "name": "Jane"}])
store.commit()

验证数据是否插入成功

可以添加以下代码查询容器数据:

query = con.query("select * from mycontainer")
rs = query.fetch(False)
while rs.has_next():
    print(rs.next())

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 18:52:44