import TabItem from "@theme/TabItem"; import Tabs from "@theme/Tabs";
Data Insertion
Data insertion is done using the INSERT
statement, which is used to write data into an existing table. We have already
created a database called "bookshop" and a table called "book" under the database. Now we can write book information
into our "book" table.
Insert Single Example
We provide flexible methods for data insertion. You can store the data to be inserted in a map, and we will automatically generate the SQL for data insertion and execute it.
SQL Example
INSERT INTO bookshop.book (
id, name, category, author, public_at,
stock, price
) value (
1, 'Token Economy: How the Web3 reinvents the Internet',
'web3', 'Shermin Voshmgir', 1592120979,
5, 70
)
Builtin Method
Both Python and JavaScript versions of the SDK provide the insert()
method for inserting data.
<Tabs defaultValue="Python" values={[ { label: "Python", value: "Python" }, { label: "JavaScript", value: "JavaScript" }, { label: "Golang", value: "Golang" }, ]}
item = {
"id": 1,
"name": "Token Economy: How the Web3 reinvents the Internet",
"category": "web3",
"author": "Shermin Voshmgir",
"public_at": 1592120979,
"stock": 5,
"price": 70
}
db = client.db(mk)
db.insert("bookshop.book", item)
const item = {
id: 1,
name: "Token Economy: How the Web3 reinvents the Internet",
category: "web3",
author: "Shermin Voshmgir",
public_at: 1592120979,
stock: 5,
price: 70,
};
const db = client.db(mk);
db.insert("bookshop.book", item);
resp, err := glitterClient.Insert(ctx, "bookshop.book", map[string]interface{}{
"id": 1,
"name": "Token Economy: How the Web3 reinvents the Internet",
"category": "web3",
"author": "Shermin Voshmgir",
"public_at": 1592120979,
"stock": 5,
"price": 70,
})
Execute SQL
Of course, if you prefer to write your own SQL for execution, you can directly call the corresponding SDK's SQL execution method with the prepared SQL statement for data insertion.
<Tabs defaultValue="Python" values={[ { label: "Python", value: "Python" }, { label: "JavaScript", value: "JavaScript" }, { label: "Golang", value: "Golang" }, ]}
sql = "INSERT INTO bookshop.book (id, name, category, author, public_at, stock, price) value (1, 'Token Economy: How the Web3 reinvents the Internet', 'web3', 'Shermin Voshmgir', 1592120979, 5, 70)"
db = client.db(mk)
db.sql_exec(sql)
const sql =
"INSERT INTO bookshop.book (id, name, category, author, public_at, stock, price) value (1, 'Token Economy: How the Web3 reinvents the Internet', 'web3', 'Shermin Voshmgir', 1592120979, 5, 70)";
const db = client.db(mk);
await db.sqlExec(sql);
sql = "INSERT INTO bookshop.book (id, name, category, author, public_at, stock, price) value (1, 'Token Economy: How the Web3 reinvents the Internet', 'web3', 'Shermin Voshmgir', 1592120979, 5, 70)"
glitterClient.SQLExec(ctx, sql, nil)
Insert Batch Example
We also provide a method for batch insertion of data. This function helps you concatenate the corresponding information
into the format of INSERT INTO... () VALUES(), (),...
.
SQL Example
INSERT INTO 'bookshop.book' (
id, name, category, author, public_at,
stock, price, abstract
) value (
1, 'Token Economy: How the Web3 reinvents the Internet',
'web3', 'Shermin Voshmgir', 1592120979,
5, 70
),
(
2, 'Mastering Bitcoin', 'btc', 'Andreas M.Antonopoulos',
1418548616, 6, 56
);
<Tabs defaultValue="Python" values={[ { label: "Python", value: "Python" }, { label: "JavaScript", value: "JavaScript" }, { label: "Golang", value: "Golang" }, ]}
Python
Python SDK provides the batch_insert()
method for batch data insertion.
items = [{
"id": 1,
"name": "Token Economy: How the Web3 reinvents the Internet",
"category": "web3",
"author": "Shermin Voshmgir",
"public_at": 1592120979,
"stock": 5,
"price": 70,
}, {
"id": 2,
"name": "Token Economy: How the Web3 reinvents the Internet",
"category": "web3",
"author": "Shermin Voshmgir",
"public_at": 1592120979,
"stock": 5,
"price": 70,
}]
db = client.db(mk)
db.batch_insert("bookshop","book", items)
JavaScript SDK provides the BatchInsert
method for batch data insertion.
const items = [
{
id: 1,
name: "Token Economy: How the Web3 reinvents the Internet",
category: "web3",
author: "Shermin Voshmgir",
public_at: 1592120979,
stock: 5,
price: 70,
},
{
id: 2,
name: "Token Economy: How the Web3 reinvents the Internet",
category: "web3",
author: "Shermin Voshmgir",
public_at: 1592120979,
stock: 5,
price: 70,
},
];
const db = client.db(mk);
await db.batchInsert("bookshop", "book", items);
Golang SDK provides the ()
method for batch data insertion.
resp, err := glitterClient.BatchInsert(ctx, table, map[string][]interface{}{
"id": []interface{}{1, 2},
"name": []interface{}{"Token Economy: How the Web3 reinvents the Internet", "Token Economy: How the Web3 reinvents the Internet"},
"category": []interface{}{"web3", "web3"},
"author": []interface{}{"Shermin Voshmgir",
"public_at": []interface{}{1592120979, 1592120979},
"stock": []interface{}{5, 5},
"price": []interface{}{70,70},
})