Skip to main content

Mysql Builder

Hi! Wellcome to Mysql Builder, Using this library you can easily make your mysql query. Link: Goto Package link

Installation

pip install mysqlbuilder

Setup

Create logs folder in your project root.

mkdir logs

import the package in your project

from mysqlbuilder.Builder import Builder

Example

obj = Builder('Your_database_name')
obj.select('*').from('your_table_name).where({'column1' : 'condition1', 'column2' : 'condition2'}).get().execute()

Output

SELECT * FROM your_table_name WHERE column1 = 'condition1' AND column2 = 'condition2';

Doc

init the Class

db_config = {
    'host': 'localhost',
    'user': 'root',
    'password': '',
    'database': 'Your_database_name',
}

obj = Builder(db_config)

Note You need to pass the database config in mentioned dictonary with same keyword.

select

obj.select('*')

You can also pass the column names as well

obj.select('id, name, age, mobile number')

You can use alias as well

obj.select('name as username')

Note You can use all of those mysql default command in select

table

obj.table('your_table_name')

set table alias

obj.table('your_table_name as table')

where

obj.where({'id': 1})

Note You have to use the python Dictionary in where condition

orWhere

Basically orWhere as same as Where condition but the difference is it will place a "OR" in the condition

 obj.orWhere({'si.is_delete': '0', 'si.is_create': '0'})

join

Basically there are three type of joining we are support right now

  1. INNER JOIN
  2. LEFT JOIN
  3. RIGHT JOIN

Example of INNER JOIN

You do not have to pass any additional parameter, by default we will treat join as INNER JOIN

obj = Builder('Your_database_name').select('u.name as username, a.city as 
user_city').table('users u').join('address a', 'u.address_id = a.id')

Output

SELECT u.name as username, a.city as user_city FROM users u INNER JOIN address a ON u.address_id = a.id

Example of LEFT JOIN

You have to pass the left as keyword in the last parameter in join method

obj = Builder('Your_database_name').select('u.name as username, a.city as 
user_city').table('users u').join('address a', 'u.address_id = a.id', 'left')

Output

SELECT u.name as username, a.city as user_city FROM users u LEFT JOIN address a ON u.address_id = a.id

Example of RIGHT JOIN

You have to pass the right as keyword in the last parameter in join method

obj = Builder('Your_database_name').select('u.name as username, a.city as 
user_city').table('users u').join('address a', 'u.address_id = a.id', 'right')

Output

SELECT u.name as username, a.city as user_city FROM users u RIGHT JOIN address a ON u.address_id = a.id

raw_Sql

raw sql is given to write the custom query which you want to execute but not given to this library.

obj.table('your_table_name').select(*).raw_sql("date => '2023-07-23' AND date <= '2023-07-23'")

Output

SELECT * FROM your_table_name WHERE date => '2023-07-23' AND date <= '2023-07-23'

limit

 obj.table('your_table_name').select(*).limit('100')

Output

SELECT * FROM your_table_name limit 100

limit with offset

you can use the limit offset to query the limit

 obj.table('your_table_name').select(*).limit(100,300)

Output

SELECT * FROM your_table_name limit 100, 300

Order By

 obj.table('your_table_name').select(*).orderBy('id', 'ASC')

Output

SELECT * FROM your_table_name ORDER BY id ASC

GROUP By

 obj.table('your_table_name').select(*).groupBy('name')

Output

SELECT * FROM your_table_name GROUP BY name

like

 obj.table('your_table_name').select(*).like('name', 'santu sarkar')

Output

SELECT * FROM your_table_name where name like '%santu sarkar%'

get

This method is used to combine all of your query params & condition and together generate the sql

obj.table('your_table_name').select('id, name, state_id').get()

Note This comman do not produce any additional output but internally it's used to generate the sql

execute

This method is the used to compile & execute the sql query and produce the output as an python dictionary format

obj.table('your_table_name').select('id, name, state_id').get().execute

compiled query

This method is used to check the query as plain text
you can use this in print()

print(obj.compiled_query())

Release files for mysqlbuilder 1.1.1

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

Source distribution (sdist)

Source distribution for mysqlbuilder 1.1.1
File Size Uploaded
mysqlbuilder-1.1.1.tar.gz 5.5 kB Details

Built distribution (wheel)

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

Total release size:11.6 kB

Release files / mysqlbuilder-1.1.1.tar.gz

Download URL mysqlbuilder-1.1.1.tar.gz
Size 5.5 kB
Tags Source
SHA-256 checksum
How to use checksums
70e056d38fba96baecb95c4e1cc50babbb2ea5a344395d7767e22b243cb78028
BLAKE2b-256 checksum
How to use checksums
ecc05739fc31a90dd106a3bb3ed90ed996215b824830346e1a0441b46a5ef201
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.0

Release files / mysqlbuilder-1.1.1-py3-none-any.whl

Download URL mysqlbuilder-1.1.1-py3-none-any.whl
Size 6.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
dabd5b38aba48d87e977f4290c7f7e1c51584b53f73558dcf17a249a3ddf903e
BLAKE2b-256 checksum
How to use checksums
79f0020171134fb18fca4f1b0f7da7e87ae26a693a6b1fbcaeddd8b74a913763
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.0

Release history Release notifications | RSS feed

This release

1.1.1 This release

2 release files

1.1.0

2 release files

1.0.0

2 release files

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