Skip to main content

SQL Database management without even a SQL line

Project description

EasySQL

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.

mysql-connector: Which is the basic library for connecting to database

Example

import EasySQL

# Simply provide connection info to your database
@EasySQL.auto_init
class MyDatabase(EasySQL.EasyDatabase):
    _database = 'MyDatabase'
    _password = '**********'
    _host = '127.0.0.1'
    _port = 3306
    _user = 'root'

# Simply create a MyTable with its columns!
@EasySQL.auto_init
class MyTable(EasySQL.EasyTable, database=MyDatabase, name='MyTable'):
    ID = EasySQL.EasyColumn('ID', EasySQL.Types.BIGINT, EasySQL.PRIMARY, EasySQL.AUTO_INCREMENT)
    Name = EasySQL.EasyColumn('Name', EasySQL.Types.STRING(255), EasySQL.NOT_NULL, default='Missing')
    Balance = EasySQL.EasyColumn('Balance', EasySQL.Types.INT, EasySQL.NOT_NULL)
    Premium = EasySQL.EasyColumn('Premium', EasySQL.Types.BOOL, EasySQL.NOT_NULL, default=False)

# Insert values with a simple command
MyTable.insert([MyTable.Name, MyTable.Premium, MyTable.Balance], ['Ashenguard', True, 10])
MyTable.insert([MyTable.Name, MyTable.Premium], ['Sam', False])

# Let's add some random data 
from random import randint
for i in range(5):
    MyTable.insert(['Name', 'Balance'], [f'User-{i}', randint(0, 20)])

# Selecting data with another simple command
### Let's get all the data
all = MyTable.select()
### Something that does not exist
empty = MyTable.select(MyTable.ID, where=EasySQL.WhereIsEqual(MyTable.Name, "NO-ONE"))
### To select multiple data give a list of columns as 1st argument
premiums = MyTable.select([MyTable.ID, MyTable.Name], EasySQL.WhereIsEqual(MyTable.Premium, True))
### You can have more complicated condition with AND (&), OR (|) and NOT (~)
specific = MyTable.select(MyTable.Name, where=EasySQL.WhereIsLike(MyTable.Name, "Ash%").AND(EasySQL.WhereIsLesserEqual(MyTable.ID, 5)))
### Giving no column will select all the columns, Also you can use limit, offset and order to sort data
second = MyTable.select(order=MyTable.Balance, descending=True, limit=1, offset=1)
top5 = MyTable.select(order=MyTable.Balance, descending=True, limit=5)
### If you want only one result not a sequence of them! It will return a SelectData if a data is found or return None if none is found.
one = MyTable.select(where=EasySQL.WhereIsEqual(MyTable.Name, "Ashenguard"), force_one=True)

# The result will be an EmptySelectData if nothing was found, A SelectData if only one was found, Or a tuple of SelectData
# All 3 of them are iterable, so it is safe to use a `for` loop for any result
# To get data from the result you can use `get`, but it only contains columns requested in select method.
for data in top5:
    print(f'{data.get(MyTable.ID)}: {data.get(MyTable.Name)}\tBalance: {data.get(MyTable.Balance)}')

# To delete data just use the delete method
MyTable.delete(EasySQL.WhereIsGreater(MyTable.ID, 5))

# Update data with following command
MyTable.update(MyTable.Premium, True, EasySQL.WhereIsEqual(MyTable.ID, 3).OR(EasySQL.WhereIsEqual(MyTable.Name, 'Sam')))

# Not sure if you should update or insert and don't have primary or unique keys? Use set and it will be handled
MyTable.set(MyTable.columns, [6, 'Nathaniel', 50, False], where=EasySQL.WhereIsEqual(MyTable.ID, 5))

# And if you have unique and primary keys set let insert handle duplicates for you
MyTable.insert(MyTable.columns, [3, 'Jack', 5000, False], update_on_dup=True)

# Safety error on delete/update/set without a where statement
# MyTable.delete() -> raise EasySQL.DatabaseSafetyException
# Turn the safety off with following command.
MyDatabase.remove_safety(confirm=True)
# Now there will be no error, it will clean the all data that's why we had safety lock
MyTable.delete()

Advertisement Banner

Extras & Features

  1. Need unsigned types? EasySQL has them.

BIGINT.UNSIGNED, INT.UNSIGNED, MEDIUMINT.UNSIGNED, SMALLINT.UNSIGNED

  1. Afraid of unsigned or signed values? EasySQL will check them for you!

Raises ValueError if you are out of bound

  1. Multiple primary keys? EasySQL will take care of it.

Tag them with PRIMARY or add them to YourTableClass.PRIMARY

  1. Want to mark multiple columns as unique together? EasySQL have it.

Add Unique(column_1, column_2) to YourTableClass.UNIQUES

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-3.4.1.tar.gz (14.4 kB view details)

Uploaded Source

Built Distribution

pyeasysql-3.4.1-py3-none-any.whl (17.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pyeasysql-3.4.1.tar.gz
  • Upload date:
  • Size: 14.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.31.0 setuptools/56.0.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.9.6

File hashes

Hashes for pyeasysql-3.4.1.tar.gz
Algorithm Hash digest
SHA256 76e5143a8e6f8fb8bbddd8c117e87dd3301014140bc61be001aedce585fd098b
MD5 d5ccaccaedee56f6343b054e0b41333b
BLAKE2b-256 883295175d212d430d9c248c431f5696a91ca68b8d44c16f194852859212afb8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pyeasysql-3.4.1-py3-none-any.whl
  • Upload date:
  • Size: 17.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.31.0 setuptools/56.0.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.9.6

File hashes

Hashes for pyeasysql-3.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9261609e2f6b8cf7f8b3151949dc0f4bbe65108234b3df6df0b4184dc09e26f6
MD5 9a2e787566bbfa51bf71c701843a3a7b
BLAKE2b-256 c9b437911a4ffd577473bd9aabaa2ffda0241d1b9e0e85a225cea62180ef82ba

See more details on using hashes here.

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