Skip to main content

import TabItem from "@theme/TabItem"; import Tabs from "@theme/Tabs";

Updating data

UPDATE statements are used to update data in a table. If an UPDATE statement is executed without a WHERE condition, it will update all the data in the table. If an ORDER BY condition is specified, the update will be performed in the given order. LIMIT can be used to limit the number of rows to be updated.

[!TIP] We recommend using a select query to retrieve the number of records to be updated before updating data, and then setting a limit during the update.

Using SDK

Both Python and JavaScript versions of the SDK provide an update() method to update data. You need to pass in two maps, one for the fields to be updated along with their corresponding values, and the other for the condition information. For example, to update the inventory of a book in the book table with id equal to 1 and set it to 4, you can execute the following example:

SQL Example

UPDATE bookshop.book SET stock=4 WHERE id=1 LIMIT 1;

Builtin Method

<Tabs defaultValue="Python" values={[ { label: "Python", value: "Python" }, { label: "JavaScript", value: "JavaScript" }, { label: "Golang", value: "Golang" }, ]}

Python

where = {"id=": 1}
update_info = {"stock": 4}
db = client.db(mk)
db.update("bookshop", "book", update_info, where)
const updateInfo = {
"stock": 4,
};
const where = {
"id=": 1,
};
const db = client.db(mk);
await db.update("bookshop", "book", updateInfo, where);
where := map[string]interface{}{"id": id}
update_info := map[string]interface{}{"name": "name_updated"}
resp, err := glitterClient.Update(ctx, "bookshop.book", update_info, where)
### Execute SQL We can also write custom update SQL statements and call the corresponding SDK's execute method.

<Tabs defaultValue="Python" values={[ { label: "Python", value: "Python" }, { label: "JavaScript", value: "JavaScript" }, { label: "Golang", value: "Golang" }, ]}

Python

sql = 'UPDATE bookshop.book SET stock=4 WHERE id=1 LIMIT 1'
db = client.db(mk)
db.sql_exec(sql)
const sql = "UPDATE bookshop.book SET stock=4 WHERE id=1 LIMIT 1";
const db = client.db(mk);
await db.sqlExec(sql);

Golang

sql = 'UPDATE bookshop.book SET stock=4 WHERE id=1 LIMIT 1'
resp, err := glitterClient.SQLExec(ctx, sql, nil)