首页 > 编程问答 >尝试更新 GridDB 中的列,但无法弄清楚为什么它没有更新

尝试更新 GridDB 中的列,但无法弄清楚为什么它没有更新

时间:2024-08-08 04:01:09浏览次数:5  
标签:python sql griddb

我正在尝试使用 Python 客户端更新 GridDB 容器中的数据。我已经设置了环境并编写了以下代码来执行更新。更新操作似乎不起作用,因为数据保持不变。

关于我在这里可能做错了什么有什么想法吗?这是我正在阅读的文档: https://docs.griddb.net/gettingstarted/python/

import griddb_python as griddb

factory = griddb.StoreFactory.get_instance()

try:
    store = factory.get_store(
        notification_member="239.0.0.1",
        notification_port=31999,
        cluster_name="myCluster",
        username="admin",
        password="admin"
    )
    container = store.get_container("myContainer")

    query = container.query("SELECT * FROM myContainer WHERE id = 1")
    rs = query.fetch()

    if rs.has_next():
        data = rs.next()
        data[2] = 35  # Update the age field

        # Put the updated row back into the container
        container.put(data)
        store.commit()
except griddb.GSException as e:
    for i in range(e.get_error_stack_size()):
        print("[", i, "]")
        print(e.get_error_code(i))
        print(e.get_message(i))

The issue in your code lies in how you are trying to update the row. The container.put(data) method doesn't work in-place for updates. Instead, it tries to insert a new row using the provided data , which might be causing a constraint violation if your id column is a primary key.

Here are two ways to correctly update a row in GridDB using the Python client:

Method 1: Using update_by_row_key()

This method is more efficient for updating a single row identified by its row key.

```python import griddb_python as griddb

# ... (connection and container setup from the original code)

try: # Assuming 'id' is your row key row_key = 1

   # Fetch the row for update
   row = container.get(row_key)  
   if row:
       row[2] = 35  # Update the 'age' field
       container.update_by_row_key(row)
       store.commit()
       print("Row updated successfully!")
   else:
       print("Row not found.")

except griddb.GSException as e: # ... (error handling) ```

Method 2: Using a SQL UPDATE statement

This method allows for more flexible updates based on conditions.

```python import griddb_python as griddb

# ... (connection and container setup)

try: query = "UPDATE myContainer SET age = 35 WHERE id = 1" container.update(query) store.commit() print("Row updated successfully!")

except griddb.GSException as e: # ... (error handling) ```

Explanation:

  • Method 1: container.get(row_key) retrieves the entire row data for the specified key. After modifying the desired field ( row[2] = 35 ), container.update_by_row_key(row) directly updates that row in the container.
  • Method 2: The SQL UPDATE statement provides a standard way to modify data based on specific criteria. This approach is useful for updating multiple rows or using more complex conditions.

Important Note: Ensure your id column (or the column you use for row_key ) is defined as a primary key or unique key for efficient updates and to avoid conflicts.

标签:python,sql,griddb
From: 78845456

相关文章

  • python joblib.load 发生错误:协议 0 中的持久 ID 必须是 ASCII 字符串 在 GCP 云运行
    总体而言:我尝试使用Cloudbuild和Cloudrun构建BERT模型。我将模型(参数)和元数据(标签)保存在GCPCloudStorage中。但是,我遇到了通过joblib.load()加载metadata.bin文件的错误。我的metadata.bin文件包含UTF-8字符,但joblib.load需要ASCII字符。在......
  • Python + Svelte,如何使用本地文件系统
    总结一下,我有一个用python编写的应用程序。它在输入时需要一堆视频文件。使用一些魔法并生成合并的视频文件输出。我没有找到一个好的GUI解决方案(tkinter,QT,TUI等),所以我选择Svelte框架。但是出现了一个问题,我如何使用本地文件系统。在GUI(svelte)上,我必须上......
  • 如何在Python中绘制伪球面
    目标是使用meshgrid和numpy库生成伪球体的三维图形,但我使用下面的代码生成的图形不完整u=np.linspace(0,np.pi,50)v=np.linspace(0,2*np.pi,100)x,y=np.meshgrid(u,v)X=np.arccos(x)*np.cos(y)Y=np.arccos(x)*np.sin(y)Z=x-np.tan(x)fig=plt.f......
  • 掌握MySQL查询优化:理论与实践全解析
    1.MySQL查询优化器概述MySQL查询优化器的主要功能是优化和执行SELECT语句,确保在正确执行的前提下提升执行效率。它利用关系代数、启发式规则和代价估算模型等技术进行优化,主要针对SPJ(选择-投影-连接)类型和非SPJ类型的查询语句进行优化。1.1主要功能关系代数:将SQL语......
  • MySQL优化攻略:利用常量表提升数据库性能
    1.常量表概述常量表在MySQL中的意义与编程语言中的常量不同。在MySQL中,常量表指的是那些读取表时行数明确为零或一行的数据表。常量表可以分为以下两种类型:1.1System表定义:System表是只包含一行数据的表。特点:这种表通常用于优化查询,因为其数据是固定的,因此对查......
  • 18:Python集合属性
    #Python3集合#集合(set)是一个无序的不重复元素序列。#集合中的元素不会重复,并且可以进行交集、并集、差集等常见的集合操作。#集合中元素必须是不可变类型,也就说里面不能是列表和字典#可以使用大括号{}创建集合,元素之间用逗号,分隔,或者也可以使用set()函数创建集合。s......
  • Python爬虫案例与实战:爬取源代码练习评测结果
    Python爬虫案例与实战:爬取源代码练习评测结果本章案例将介绍用Python编写程序实现简单网站的模拟登录,然后保持登录后的网页会话,并在会话中模拟网页表单提交,之后使用Requests库的高级特性爬取提交之后的返回结果。在HTTP网页中,如登录、提交和上传等操作一般通过向网页发送......
  • Python爬虫案例与实战:爬取豆瓣电影简介
    Python爬虫案例与实战:爬取豆瓣电影简介本章案例将介绍如何爬取豆瓣电影简介,以此帮助读者学习如何通过编写爬虫程序来批量地从互联网中获取信息。本案例中将借助两个第三方库----Requests库和BeautifulSoup库。通过Requests库获取相关的网页信息,通过BeautifulSoup库解析大......
  • 趣味Python游戏编程:第3章 递归函数的威力:扫雷
    趣味Python游戏编程:第3章递归函数的威力:扫雷在第2章中,我们制作了一个拼图游戏,玩家通过鼠标操作图片块移动。本章设计一款扫雷游戏,玩法是在一个方块阵列中随机埋设一定数量的地雷,然后由玩家逐个打开方块,并以排除所有地雷为最终游戏目标。如果玩家打开的方块中有地雷,则游戏......
  • Python 依赖管理神器 Poetry 深入指南
    Poetry依赖管理详细教程引言在现代软件开发中,依赖管理是项目成功的关键。Python开发者现在有了一个新的选择:Poetry。Poetry是一个现代的Python包管理工具,旨在简化Python项目的创建、打包和发布。它提供了一种更直观和高效的方式来管理项目的依赖关系,相较于传统的p......