Skip to main content

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

pip install glitter-sdk==0.2.1

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

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))

Create Database

First, we need to use the CREATE DATABASE statement to create a database called "bookshop".

db = client.db(mk)
db.create_database("bookshop")

Create Table

We can create a "book" table to store book information using the CREATE Table statement.

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)

Insert Data

Insert data into the "book" table.

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)

Query Data

Query book information.

sql = prepare_sql("SELECT * FROM bookshop.book WHERE author=%s", 'Shermin Voshmgir')
db = client.db(mk)
rst = db.query(sql)