import TabItem from "@theme/TabItem"; import Tabs from "@theme/Tabs";
Quick Start
Glitter provides decentralized storage and easy access to SQL. It also offers flexible permission management for isolating permissions for databases, tables, and more.
To make it easier for you to read the content in the Application Development Guide, we will use the data table structure and data from the bookshop
application as a basis for writing an example SQL. This section will introduce how to create a database, create tables, and insert and query data.
Install SDK
<Tabs defaultValue="Python" values={[ { label: "Python", value: "Python" }, { label: "JavaScript", value: "JavaScript" }, { label: "Golang", value: "Golang" }, ]}
pip install glitter-sdk==0.2.1
npm install @glitter-protocol/glitter-sdk
go get github.com/blockved/glitter-go
Initialize Client
To initialize the client for subsequent operations, you must provide a valid mnemonic phrase as user information.
For more information about the Glitter network, please refer to the Glitter Network
<Tabs defaultValue="Python" values={[ { label: "Python", value: "Python" }, { label: "JavaScript", value: "JavaScript" }, { label: "Golang", value: "Golang" }, ]}
from glitter_sdk.client.lcd import LCDClient
from glitter_sdk.core import Numeric, Coins
from glitter_sdk.key.mnemonic import MnemonicKey
XIAN_HOST = "https://api.xian.glitter.link"
#LOCAL_HOST = "http://127.0.0.1:41317"
CHAIN_ID = "glitter_12000-1"
mk = MnemonicKey(
"bleak aspect work unfold coconut equip mask cup prize panel sword unit slice inject famous iron build slice mean evolve bundle must wheel impulse"
)
client = LCDClient(
chain_id=CHAIN_ID,
url=XIAN_HOST,
gas_prices=Coins.from_str("0.15agli"),
gas_adjustment=Numeric.parse(1.5))
import { MnemonicKey, LCDClient, Coins, Numeric } from "glitter";
const XIAN_HOST = "https://api.xian.glitter.link";
//const LOCAL_HOST = "http://127.0.0.1:41317"
const CHAIN_ID = "glitter_12000-1";
const mk = new MnemonicKey({
mnemonic:
"bleak aspect work unfold coconut equip mask cup prize panel sword unit slice inject famous iron build slice mean evolve bundle must wheel impulse",
});
const client = new LCDClient({
URL: XIAN_HOST,
chainID: CHAIN_ID,
gasPrices: Coins.fromString("0.15agli"),
gasAdjustment: Numeric.parse(1.5),
});
package main
import (
"fmt"
"github.com/blockved/glitter-go/client"
"github.com/blockved/glitter-go/key"
"github.com/blockved/glitter-go/msg"
"time"
)
func NewClient() *client.LCDClient {
xianHost := "https://api.xian.glitter.link"
chainID := "glitter_12000-1"
// Create Mnemonic
mnemonic := "bleak aspect work unfold coconut equip mask cup prize panel sword unit slice inject famous iron build slice mean evolve bundle must wheel impulse"
// Derive Raw Private Key
privKeyBz, err := key.DerivePrivKeyBz(mnemonic, key.CreateHDPath(0, 0))
if err != nil {
return nil
}
// Generate StdPrivKey
privKey, err := key.PrivKeyGen(privKeyBz)
if err != nil {
return nil
}
// Create glitterClient
glitterClient := client.NewGlitterClient(
xianHost,
chainID,
msg.NewDecCoinFromDec("agli", msg.NewDecFromIntWithPrec(msg.NewInt(15), 1)), // 1.5uusd
msg.NewDecFromIntWithPrec(msg.NewInt(15), 1),
privKey,
time.Second*10,
)
return glitterClient
}
Create Database
First, we need to use the CREATE DATABASE
statement to create a database called "bookshop".
<Tabs defaultValue="Python" values={[ { label: "Python", value: "Python" }, { label: "JavaScript", value: "JavaScript" }, { label: "Golang", value: "Golang" }, ]}
db = client.db(mk)
db.create_database("bookshop")
const db = client.db(mk);
await db.createDatabase("bookshop");
resp, err := glitterClient.CreateDatabase(ctx, "bookshop")
Create Table
We can create a "book" table to store book information using the CREATE Table
statement.
<Tabs defaultValue="Python" values={[ { label: "Python", value: "Python" }, { label: "JavaScript", value: "JavaScript" }, { label: "Golang", value: "Golang" }, ]}
sql = """
CREATE TABLE bookshop.book (
id INT(11) PRIMARY KEY COMMENT 'id',
name VARCHAR(255) COMMENT 'name of book',
category VARCHAR(255) COMMENT 'book category (eg:history,cartoon..)',
author VARCHAR(255) COMMENT 'author',
public_at INT(11) COMMENT 'public time',
stock INT(11) COMMENT 'stock of book',
price INT(11) COMMENT 'price',
_tx_id VARCHAR(255)
) ENGINE=standard;
"""
db = client.db(mk)
db.create_table(sql)
const sql = `
CREATE TABLE bookshop.book (
id INT(11) PRIMARY KEY COMMENT 'id',
name VARCHAR(255) COMMENT 'name of book',
category VARCHAR(255) COMMENT 'book category (eg:history,cartoon..)',
author VARCHAR(255) COMMENT 'author',
public_at INT(11) COMMENT 'public time',
stock INT(11) COMMENT 'stock of book',
price INT(11) COMMENT 'price',
_tx_id VARCHAR(255)
) ENGINE=standard;
`;
db = client.db(mk);
db.createTable(sql);
sql := `
CREATE TABLE bookshop.book (
id INT(11) PRIMARY KEY COMMENT 'id',
name VARCHAR(255) COMMENT 'name of book',
category VARCHAR(255) COMMENT 'book category (eg:history,cartoon..)',
author VARCHAR(255) COMMENT 'author',
public_at INT(11) COMMENT 'public time',
stock INT(11) COMMENT 'stock of book',
price INT(11) COMMENT 'price',
_tx_id VARCHAR(255)
) ENGINE=standard;`
resp, err := glitterClient.CreateTable(ctx, sql)
Insert Data
Insert data into the "book" table.
<Tabs defaultValue="Python" values={[ { label: "Python", value: "Python" }, { label: "JavaScript", value: "JavaScript" }, { label: "Golang", value: "Golang" }, ]}
sql = {
"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", sql)
const sql = {
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", sql);
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,
})
Query Data
Query book information.
<Tabs defaultValue="Python" values={[ { label: "Python", value: "Python" }, { label: "JavaScript", value: "JavaScript" }, { label: "Golang", value: "Golang" }, ]}
sql = prepare_sql("SELECT * FROM bookshop.book WHERE author=%s", 'Shermin Voshmgir')
db = client.db(mk)
rst = db.query(sql)
const queryTemp = "SELECT * FROM bookshop.book WHERE author=?";
const keyWords = ["Shermin Voshmgir"];
const db = client.db(mk);
await db.query(queryTemp, keyWords);
type BookItem struct {
Id int `db:"id"`
Name string `db:"name"`
Category string `db:"category"`
Author string `db:"author"`
PublicAt int `db:"public_at"`
Stock int `db:"stock"`
Price int `db:"price"`
TxID string `db:"_tx_id"`
}
var queryResult []BookItem = make([]BookItem, 0)
sql, _ := sqlutil.EscapeSQL("select * from bookshop.book WHERE author=%?", "Shermin Voshmgir")
err = glitterClient.QueryResult(ctx, &queryResult, sql)