Skip to main content

SQL Database management without even a SQL line

Project description

EasySQL - Now with Async System by 5.0.0

Downloads Downloads Downloads
This library allow you to run SQL Databases without knowing even SQL.
This library will create SQL queries and execute them as you request and is very simple to use.

This library is still under development, so we appreciate if you help us improve it on the GitHub!

Having an issue?

You can always find someone on our discord server here:

https://discord.gg/6exsySK

Wiki

The official wiki of this library is now available at GitHub

https://github.com/AGM-Studio/EasySQL/wiki

How to install

To install just use following command

pip install PyEasySQL

This library will have dev/beta builds on the GitHub, to install them you can use

pip install --upgrade git+https://github.com/AGM-Studio/EasySQL.git

By installing this library following libraries and their dependencies will be installed too.

asyncmy: Which is the basic library for connecting to database

Example

Sync example:

Link to GitHub: https://github.com/AGM-Studio/EasySQL/blob/master/sync_test.py

import EasySQL

# Enable debug mode if you like to get SPAMMED with SQL!
EasySQL.enable_debug()

# Simply provide connection info to your database
database = EasySQL.SyncedDatabase(
    database="MyDatabase",
    password="", # host, port and user set by default to usual localhost settings
)

# In V5 the table is now merged with table! Access the table through User.table!
class User(EasySQL.SQLData, database=database, name="MyTable"):
    id = EasySQL.SQLColumn("ID", EasySQL.Types.BIGINT, EasySQL.PRIMARY, EasySQL.AUTO_INCREMENT)
    name = EasySQL.SQLColumn("Name", EasySQL.Types.STRING(255), EasySQL.NOT_NULL, default="Missing!")
    balance = EasySQL.SQLColumn("Balance", EasySQL.Types.BIGINT.UNSIGNED, EasySQL.NOT_NULL) # int default is 0, no need to pass it
    premium = EasySQL.SQLColumn("Premium", EasySQL.Types.BOOL, EasySQL.NOT_NULL, default=False)

# Make sure to call database prepare once after a table is defined.
# No need to do it per table, just one after all tables is enough.
database.prepare()

# Insert values with a simple command giving the data object!
user_1 = User(name="Ashenguard", balance=100, premium=True) # Uses the default for any field mising!
User.table.insert(user_1)       # Give a premade object...
User.table.insert(name="Bob")   # Or let the method creates it!

# Let's  also add some random data
from random import randint
for i in range(5):
    User.table.insert(name=f"User-{i}", balance=randint(0, 20))

# Selecting data with another simple command which returns a list of data fetched.
### Let's get all the data
all_data = User.table.select()
no_one = User.table.select(User.name == "NO-ONE") # This is Still a list
mid_class = User.table.select(EasySQL.WhereIsBetween(User.balance, 50, 150))
print(all_data, no_one, mid_class)
### If you want only one object, then get one! Will return a User or None
one_user = User.table.select_one(User.name == "Ashenguard")
print("Type:", type(one_user))  # Prints User!
### You can also define order, descending, limit and offset too!
mixed = User.table.select(descending=True, limit=2, order=User.balance, offset=1)
print(mixed)

# Advanced Where clauses!
### While ==, !=, <, >, <=, >= work for common where clauses you can have them chained by binary operators
eg_and = (User.id != 0) & (User.id < 5)
eg_or = (User.id != 0) | (User.id < 5)
eg_not = ~ (User.id > 5)
### You also have access to more advanced ones like:
EasySQL.WhereIsIn(User.id, [0, 1, 2])
EasySQL.WhereIsBetween(User.id, 0, 5)
EasySQL.WhereIsLike(User.name, "Ash%")
### You can chain them with binary operators with general Where clauses or use AND, OR, NOT!
EasySQL.WhereIsIn(User.id, [0, 1, 2]).AND(User.name == "Ash")
EasySQL.WhereIsEqual(User.id, 5) | EasySQL.WhereIsIn(User.id, [0, 1, 2])
EasySQL.WhereIsIn(User.id, [0, 1, 2]) | ((User.id != 0) & (User.id < 5))

# Finally to update data you have also 2 approaches!
### A. Let the object auto updates itself:
### This approach will generate the Where clause by PRIMARY tags. Which means you can't change a primary value.
one_user.balance += 5
User.table.update(one_user) # The Where clause will be as if you've done: "User.id == one_user.id"
### B. Give the where clause yourself!
### It's useful if you have no primary key in your table, or you want to change a primary value!
User.table.update_where(User.id == 1, premium=True)
### You can also use the insert pattern and pass a whole new object, and it will update it whole!
User.table.update_where(User.id == 2, User(name="Chosen one!", balance=1000000, premium=True))

# Delete data with simple commands again!
User.table.delete(User.id > 2)

Async example

Link to GitHub: https://github.com/AGM-Studio/EasySQL/blob/master/async_test.py

import EasySQL

# The goal is to have both sync and async be the same as much as we can!
# Check sync example for basic information, here only async related differences are shown!
EasySQL.enable_debug()

# Tables and database should be defined before the main loop starts
database = EasySQL.AsyncDatabase(
    database="MyDatabase",
    password="",
)

# It doesn't matter if you use SQLData or AsyncSQLData, SQLData will create the table based on the type of database.
# But for IDE Compatibility we recommend to use AsyncSQLData for your async project!
class User(EasySQL.AsyncSQLData, database=database, name="MyTable"):
    id = EasySQL.SQLColumn("ID", EasySQL.Types.BIGINT, EasySQL.PRIMARY, EasySQL.AUTO_INCREMENT)
    name = EasySQL.SQLColumn("Name", EasySQL.Types.STRING(255), EasySQL.NOT_NULL, default="Missing!")
    balance = EasySQL.SQLColumn("Balance", EasySQL.Types.BIGINT.UNSIGNED, EasySQL.NOT_NULL) # int default is 0, no need to pass it
    premium = EasySQL.SQLColumn("Premium", EasySQL.Types.BOOL, EasySQL.NOT_NULL, default=False)


# All other tasks should be done inside a main loop!
# Everything is the same as the sync, but we need to await all database requests...
async def main():
    await database.prepare()

    user_1 = User(name="Ashenguard", balance=100, premium=True)
    await User.table.insert(user_1)
    await User.table.insert(name="Bob")

    from random import randint
    for i in range(5):
        await User.table.insert(name=f"User-{i}", balance=randint(0, 20))

    all_data = await User.table.select()
    no_one = await User.table.select(User.name == "NO-ONE")
    mid_class = await User.table.select(EasySQL.WhereIsBetween(User.balance, 50, 150))
    print(all_data, no_one, mid_class)

    one_user = await User.table.select_one(User.name == "Ashenguard")
    print("Type:", type(one_user))

    mixed = await User.table.select(descending=True, limit=2, order=User.balance, offset=1)
    print(mixed)

    # Where clauses have no ASYNC change!
    eg_and = (User.id != 0) & (User.id < 5)
    eg_or = (User.id != 0) | (User.id < 5)
    eg_not = ~ (User.id > 5)

    EasySQL.WhereIsIn(User.id, [0, 1, 2])
    EasySQL.WhereIsBetween(User.id, 0, 5)
    EasySQL.WhereIsLike(User.name, "Ash%")

    EasySQL.WhereIsIn(User.id, [0, 1, 2]).AND(User.name == "Ash")
    EasySQL.WhereIsEqual(User.id, 5) | EasySQL.WhereIsIn(User.id, [0, 1, 2])
    EasySQL.WhereIsIn(User.id, [0, 1, 2]) | ((User.id != 0) & (User.id < 5))


    one_user.balance += 5
    await User.table.update(one_user)
    await User.table.update_where(User.id == 1, premium=True)
    await User.table.update_where(User.id == 2, User(name="Chosen one!", balance=1000000, premium=True))

    await User.table.delete(User.id > 2)


# Let's run the main loop!
import asyncio

asyncio.run(main())

Advertisement Banner

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

pyeasysql-5.0.0.tar.gz (15.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyeasysql-5.0.0-py3-none-any.whl (16.8 kB view details)

Uploaded Python 3

File details

Details for the file pyeasysql-5.0.0.tar.gz.

File metadata

  • Download URL: pyeasysql-5.0.0.tar.gz
  • Upload date:
  • Size: 15.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for pyeasysql-5.0.0.tar.gz
Algorithm Hash digest
SHA256 6870e30419ce489ff95f9f215d9b8a7d7d0d5d36096eb011c696972f94ea1e1d
MD5 7206b0930df0491afa89bdba8eb01461
BLAKE2b-256 0b04a655b33b2d20554d5d4a9f0c3de77c006681f335e6ac07e57c98a600c8f0

See more details on using hashes here.

File details

Details for the file pyeasysql-5.0.0-py3-none-any.whl.

File metadata

  • Download URL: pyeasysql-5.0.0-py3-none-any.whl
  • Upload date:
  • Size: 16.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for pyeasysql-5.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3200854546f96174c0655ea8503e9c4e8f6d7693f1c96c7da5e39180c2b2c7cd
MD5 886bf7b32d378bf374de0750c21b1f51
BLAKE2b-256 6555f7c94fa517827b9670c7eadef2e2fa2e6cc9f2d4c85a0010804ce014bb66

See more details on using hashes here.

Supported by

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