Skip to main content

Database management system based on python and JSON.

Project description

Documentation

Table of content
Preface
  1. What is sayuDB
Tutorial
  1. Getting Started

Preface

1. What is sayuDB

sayuDB is an database management system based on python and JSON. Developed at Clee Ltd. This project actually for personal purpose only but for some reason I publish it. It supports a large part of the SQL standard feature

Tutorial

1. Getting Started

1.1 Installation

Before you can use sayuDB you need to install it, of course. Just clone this repository and place it to your project folder.

> pip3 install sayuDB

for help menu:

> python3 sayuDB --h

1.2 Creating database

The first test to see whether you can access the database server is to try to create a database.

To create a new database, in this example named myDB, you use the following funtion:

import sayuDB

# Creating database
sayuDB.create_database('myDB')

You can also create databases with other names. sayuDB allows you to create any number of databases at a given site. Database names must have an alphabetic first character, can not contain space and are limited to 63 bytes in length.

If you do not want to use your database anymore you can remove it. For example, you can destroy it using the following function:

import sayuDB

sayuDB.drop_database('myDB')

You can import export using:

# Exporting database (must use file name)
sayuDB.export_database('<name_of_database>', path_='<path>/filename.ezdb')
# Importing database
sayuDB.import_database(path_='/<path>/filename.ezdb')

1.3 Creating a New Table

You can create a new table by specifying the table name, along with all column names and their types:

you must define the database first with:

import sayuDB

db = sayuDB.sayuDB(database='myDB')

then

db.create_table('people', [
    ['name','str'],
    ['age','int'],
    ['city','str']
])

sayuDB currenly support data types str,int,float,dict

If you want to show the table, using this following function:

db.show_table(name='people')

it will returned as printed table:

name    column    datas
------  --------  -------

Finally, it should be mentioned that if you don't need a table any longer or want to recreate it differently you can remove it using the following function:

db.drop_table(name='people')

1.4 Populating a Table With Rows

The insert_row function is used to populate a table with rows:

db.insert_row(table='people', col='name,age,city', contents=['Arsybai', 23, 'Solo, Indonesia'])

You can also use col as list. EX ['name','age','city']

1.5 Querying a Table

To retrieve data from a table, the table is queried. An select_row funtion is used to do this. The funtion is divided into a select list (the part that lists the columns to be returned), a table list (the part that lists the tables from which to retrieve the data), and an optional qualification (the part that specifies any restrictions). For example, to retrieve all the rows of table people, type:

data = db.select_row(table='people', col='*')
print(data)

Here * is a shorthand for “all columns”. So the same result would be had with:

db.select_row(table='people', col='name,age,city')

The output should be:

name       age  city
-------  -----  ------------------
Arsybai     23  Solo, Indonesia
Ataro       25  Bekasi, Indonesia
Moepoi      21  Jakarta, Indonesia

If you want the output is json, use as_json=True:

data = db.select_row(table='people', col='*', as_json=True)
print(data)

A query can be “qualified” by adding a where clause that specifies which rows are wanted:

data = db.select_row(table='people', col='*', where='age=23')
print(data)

you can also use contain for specific column that contain some value:

data = db.select_row(table='people', col='*', where='age contain 23')
print(data)

Result:

name       age  city
-------  -----  ---------------
Arsybai     23  Solo, Indonesia

You can request that the results of a query be returned in sorted order:

data = db.select_row(table='people', col='*', order_by='age|asc')
print(data)

The order should be asc or desc

Result:

name       age  city
-------  -----  ------------------
Moepoi      21  Jakarta, Indonesia
Arsybai     23  Solo, Indonesia
Ataro       25  Bekasi, Indonesia

Updates

You can update existing rows using the update_row. as example I want to set Ataro age to 26:

db.update_row(table='people', set_='age=26', where='name=Ataro')

Look at the new state of the data:

name       age  city
-------  -----  ------------------
Arsybai     23  Solo, Indonesia
Ataro       26  Bekasi, Indonesia
Moepoi      21  Jakarta, Indonesia

Deletion

Rows can be removed from a table using the delete_row:

db.delete_row(table='people', where='name=Ataro')

Look at the new state of the data:

name       age  city
-------  -----  ------------------
Arsybai     23  Solo, Indonesia
Moepoi      21  Jakarta, Indonesia

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sayuDB-0.0.5.tar.gz (11.2 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page