Skip to main content

SQLLEX alpha v0.1.9 📚

Python:3.9 Language grade: Python Total alerts


Better than sqlite3. Seriously, try it out

Installation

pip install sqllex

If you need most stable version install sqllex==0.1.8.12

Version Status Tests, and actions
0.1.9.2 ⚠️ unstable CodeQL
Test Sqlite3x
Upload Python Package
0.1.8.12 ✔️ stable

About

Use databases without thinking about SQL. Let me show you how sqllex makes your life easier. Imagine you need create some database, save some data into it and take it back. That's how your code will look like.

If you never used SQLite before read this awesome example or this one

from sqllex import *

db = SQLite3x(                              
    path='my_database.db',                      
    template={                              
        "users": {                          
            "username": [TEXT, NOT_NULL],   
            "age": INTEGER,                 
        }                                   
    }                                       
)

users = db["users"]

users.insert('Sqllex', 33)

user = users.find(age=33)

print(users)  # [['Sqllex', 33]]

WHAT IS GOING ON THERE?!
from sqllex import *

# Create some database, with simple structure
db = SQLite3x(                              # create database 
    path='my_data.db',                      # path to your database, or where you would like it locate
    template={                              # schema for tables inside your database                              
        "users": {                          # name for the 1'st table
            "username": [TEXT, NOT_NULL],   # 1'st column of table, named "username", contains text-data, can't be NULL
            "age": INTEGER,                 # 2'nd column of table, named "age", contains integer value
        }                                   # end of table
    }                                       # end of schema (template)
)

# Ok, now you have database with table inside it.
# Let's take this table as variable
users = db["users"]

# Now add record of 33 years old user named 'Sqllex' into it
# Dear table, please insert ['Sqllex', 33] values 
users.insert('Sqllex', 33)

# Dear table, please find records where column 'age' == 33
user = users.find(age=33)

# Print results
print(users)  # [['Sqllex', 33]]

Ok, what if you need more complex structure with FOREIGN KEYs? Not a big deal.

"""
    For the first, you need to import * (all) from Sqllex lib and init your database
"""


# import * (all) from Sqllex
from sqllex import *

# Init-ing your databse
db = SQLite3x(path='my_awesome_db.db')

db.connect()    # It'll lock yor database until you disconnect, but makes sqllex work damn faster

"""
    Ok, now we need to create your tables into a database,
    use create_table method (as SQL-like CREATE TABLE)
"""


# Creating Groups table
db.create_table(
    'groups',                                            # here is name of table
    {                                                    # here is table structure
        'id': [INTEGER, PRIMARY_KEY, UNIQUE],            # group id
        'name': [TEXT, NOT_NULL, DEFAULT, 'Unknown']     # group name
    }
)


"""
    And one more table
"""

db.create_table(
    name='users',  # here is name of table
    columns={
        'id': [INTEGER, PRIMARY_KEY, UNIQUE],                # user id
        'username': [TEXT, NOT_NULL, DEFAULT, 'Unknown'],    # user name
        'user_group': INTEGER,                               # the group user belongs to
        FOREIGN_KEY: {
            "user_group": ["groups", "id"]                   # link to table groups, column id
        }
    })


"""
    Well done, now let's add some groups and some users into your database
    For example:
        1: Admin
        2: User
        3: Guest
"""

# Record some groups for the first

groups = db['groups']

groups.insert(id=1, name="Admin") # You can add data like this

groups.insert([2, "User"])        # Or like this

groups.insert(3, 'Guest')         # Or like this


# Same but without table object
# db.insert('groups', id=1, name="Admin")
# db.insert('groups', [2, "User"])
# db.insert('groups', 3, 'Guest')

"""
    Now let's add many users
"""


# Here we have a list of users in format: [id, name, group_id]
users_list = [
    [0, "User_0", 1],
    [1, "User_1", 2],
    [2, "User_2", 3],
    [3, "User_3", 1],
    [4, "User_4", 2],
    [5, "User_5", 3],
    [6, "User_6", 1],
    [7, "User_7", 2],
    [8, "User_8", 3],
    [9, "User_9", 1],
]

users = db['users']

# Insert it by one line
users.insertmany(users_list)

# Done!


"""
    Now we need to take it back by select method (as SQL-like SELECT)
"""

# SELECT FROM (table) (what)
users_in_db = users.select('username')

print(users_in_db)
# It'll print:
# [['User_0',] ['User_1'], ['User_2'], ['User_3'], ['User_4'], ['User_5'], ['User_6'], ['User_7'], ['User_8'], ['User_9']]


"""
    Prefect, and now select some specific records
    (only usernames where group_id parameter equalized 1)
"""


users_group_1 = users.select(
    'username',
    WHERE={'user_group': 1}
)

print(users_group_1)
# It'll print:
# [['User_0'], ['User_3'], ['User_6'], ['User_9']]


# And for some another table

users.select(
        SELECT=['username', 'group_name', 'description'],                 # SELECT username, group_name, description
        WITH=['users', AS, 'us'],                                         # FROM users AS us
        JOIN=[                                                            # JOIN
            ['groups', AS, 'gr', ON, 'us.group_id == gr.group_id'],       ## INNER JOIN groups AS gr ON us.group_id == gr.group_id
            [CROSS_JOIN, 'about', 'ab', ON, 'ab.group_id == gr.group_id'] ## INNER JOIN about ab ON ab.group_id == gr.group_id
        ],
        WHERE={'username': 'user_1'},                                     # WHERE (username='user_1')
        ORDER_BY='age DESC',                                              # order by age ASC
        LIMIT=50,
        OFFSET=20                                                           
    )

# Same as SQL script like
# SELECT username, group_name, description
# FROM users AS us
# INNER JOIN groups AS gr ON us.group_id == gr.group_id
# CROSS_JOIN about ab ON ab.group_id == gr.group_id
# WHERE (username='user_1')
# ORDER BY age DESC
# LIMIT 50
# OFFSET 20

db.disconnect() # unlock your database
Code without comments
from sqllex import *

db = SQLite3x(path='my_awesome_db.db')

db.connect()


db.create_table(
    'groups',                                            
    {                  
        'id': [INTEGER, PRIMARY_KEY, UNIQUE],
        'name': [TEXT, NOT_NULL, DEFAULT, 'Unknown']
    }
)

db.create_table(
    name='users',
    columns={
        'id': [INTEGER, PRIMARY_KEY, UNIQUE],
        'username': [TEXT, NOT_NULL, DEFAULT, 'Unknown'],
        'user_group': INTEGER,
        FOREIGN_KEY: {
            "user_group": ["groups", "id"]
        }
    })

groups = db['groups']

groups.insert(id=1, name="Admin")

groups.insert([2, "User"])

groups.insert(3, 'Guest')

users_list = [
    [0, "User_0", 1],
    [1, "User_1", 2],
    [2, "User_2", 3],
    [3, "User_3", 1],
    [4, "User_4", 2],
    [5, "User_5", 3],
    [6, "User_6", 1],
    [7, "User_7", 2],
    [8, "User_8", 3],
    [9, "User_9", 1],
]

users = db['users']

users.insertmany(users_list)

users_in_db = users.select('username')

print(users_in_db)

users_group_1 = users.select(
    'username',
    WHERE={'user_group': 1}
)

print(users_group_1)

users.select(
        SELECT=['username', 'group_name', 'description'], 
        WITH=['users', AS, 'us'],
        JOIN=[                                   
            ['groups', AS, 'gr', ON, 'us.group_id == gr.group_id'],
            [CROSS_JOIN, 'about', 'ab', ON, 'ab.group_id == gr.group_id']
        ],
        WHERE={'username': 'user_1'},                                     
        ORDER_BY='age DESC',                                             
        LIMIT=50,
        OFFSET=20                                                           
    )

db.disconnect()

Not enough? Need examples? Read more in Sqllex Wiki! (link)


Other

TODO-list

Release files for sqllex 0.1.9.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sqllex 0.1.9.3
File Size Uploaded
sqllex-0.1.9.3.tar.gz 34.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for sqllex 0.1.9.3
File Interpreter ABI Platform
sqllex-0.1.9.3-py3-none-any.whl Python 3 none any Details

Total release size: 66.3 kB

Release files / sqllex-0.1.9.3.tar.gz

Download URL sqllex-0.1.9.3.tar.gz
Size 34.2 kB
Tags Source
SHA-256 checksum
How to use checksums
2c8d62a3ee4db07bd35b83ddf73c928f296b18a78f2ddd8c2bbf3b0ad05b3944
BLAKE2b-256 checksum
How to use checksums
605cc1d28b1299b45b215d0c71ece8618b9bfb690f8def273b664ecd6bef0536
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.9.5

Release files / sqllex-0.1.9.3-py3-none-any.whl

Download URL sqllex-0.1.9.3-py3-none-any.whl
Size 32.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
76a42c42222e29ae4342986f749d7effa5fd7463352a85d021715fcd2dbcd71d
BLAKE2b-256 checksum
How to use checksums
67c7be46536e07c4519b69825ec015ffdd1671fede7af99a9af3a3ee7c0d8990
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.9.5

Release history Release notifications | RSS feed

0.3.0

2 release files

0.2.3

2 release files

0.2.0.0

1 release file

This release

0.1.9.3 This release

2 release files

0.1.8.8

1 release file

0.1.8.7

1 release file

0.1.8.6

1 release file

0.1.8.5

1 release file

0.1.8.4

1 release file

0.1.8.3

1 release file

0.1.8.2

1 release file

0.1.7.2

1 release file

0.1.7.1

1 release file

0.1.7

1 release file

0.1.6.5

1 release file

0.1.6.3

1 release file

0.1.6.2

1 release file

0.1.6.1

1 release file

0.1.5.2

1 release file

0.1.5

1 release file

0.1.3

1 release file

0.1.2

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page