Skip to main content

Mysql query builder

Project description

Python MysqlCommand

Python MysqlCommand is a MySql query builder that enables using keyword parameters. Inspired by the MysqlCommand class by Mysql for ASP.net

Installation

Use the package manager pip to install Python MysqlCommand

pip install mysqlcommand

Usage

Select Rows

import mysql.connector
from mysqlcommand import MysqlCommand

dbcon = mysql.connector.connect(
  host='{host}',
  user='{username}',
  passwd='{password}'
)

dbcom = MysqlCommand('select * from {table}', dbcon)
dbcom.execute_reader()

while dbcom.read():
  dbcom.data['{columnName}']

Select Rows using Parameter

dbcom.commandstr = 'select * from {table} where {columnName} = ?columnValue'
dbcom.parameters.add('?columnValue', '{value}')
dbcom.execute_reader()
while dbcom.read():
  dbcom.data['{columnName}']

Select Rows using Parameter with list value

dbcom.commandstr = 'select * from {table} where {columnName} in ?columnValue'
dbcom.parameters.add('?columnValue', ['{value}', '{value2}', '{value3}'])
dbcom.execute_reader()
while dbcom.read():
  dbcom.data['{columnName}']

Retrieve single value

Useful when just needing a boolean or count

dbcom.commandstr = 'select count(*) from {table}'
table_count = dbcom.execute_scalar()

Database Manipulation - Autocommit

# For database manipulation to ensure data changes are saved automatically
dbcom.set_autocommit(True)

Update/Delete using Parameters

dbcom.commandstr = 'update {table} set '{columnName}' = ?columnNewValue where '{columnName}' = ?columnOldValue';
dbcom.parameters.add('?columnOldValue', 3)
dbcom.parameters.add('?columnNewValue', 5)
dbcom.execute_non_query()

# To commit database changes without autocommit
dbcom.commit()

Insert Row

new_row = {
  '{columnName}': 1
}
insert_id = dbcom.insert('{table}', new_row)

Insert Rows

new_rows = [{
  '{columnName}': 2
},
{
  '{columnName}': 3
}]

recordsInserted = dbcom.insert('{table}', new_rows)

Other Parameter options

Parameter update current parameter valuue

dbcom.parameters.set('?columnOldValue', 5)

Parameter upsert parameter value - update if existing, create if doesn't

dbcom.parameters.setadd('?columnOldValue', 5)

Remove all parameters

dbcom.parameters.clear()

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

License

MIT

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

mysqlcommand-0.1.1.tar.gz (5.3 kB view details)

Uploaded Source

File details

Details for the file mysqlcommand-0.1.1.tar.gz.

File metadata

  • Download URL: mysqlcommand-0.1.1.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.6.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.6.9

File hashes

Hashes for mysqlcommand-0.1.1.tar.gz
Algorithm Hash digest
SHA256 15bc8d08173e907d91df0dd81cb2a4285e5000a426aea7396dd15efc7a391804
MD5 6123880b5f0566b994aa2887426f0f2b
BLAKE2b-256 83c1e7c13a8b4b5aea0323f9e5996525630216e19c75daa485117ae44eda801b

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