Skip to main content

Easy MySQL Manager

Project description

EasyMySQL: A Library for Managing MySQL in Python

Website

Downloads

Total Last Month Last Week
Downloads Downloads Downloads

Installing EasyMySQL

pip install easymysql

If you encounter an error due to having an older version of Python, you can use:

pip3 install easymysql

Connecting to the Database

#!/usr/bin/python

from easymysql.mysql import mysql

my = mysql('localhost','user_db','pass_db','db_name')

Inserting Data

Inserting data is extremely easy; you only need to provide two parameters: the table name and an array with the data to insert:

my.insert('table_name',{
	'field_1':'value_1',
	'field_2':'value_2',
	'field_3':'value_3',
})

The array uses a Key-Value format where the key corresponds to the table field name, and the value is the value to be inserted.

Updating Data

Updating data is similar to inserting, but this time you need to pass three parameters: the table name, an array with the data to update (Key-Value format), and a third parameter specifying the WHERE clause, either as a Key-Value array or a SQL string:

my.update('table_name',{
	'field_1':'value_1',
	'field_2':'value_2',
	'field_3':'value_3',
},{
	'id':300
})

The third parameter, if given as an array, will generate a standard SQL string concatenated with AND. For example:

my.update('table_name',{
	'field_1':'value_1'
},{
	'field_2':'value_3',
	'field_3': 'value_3'
})

This translates to:

UPDATE table_name SET field_1=value_1 WHERE field_2='value_2' AND field_3='value_3'

Alternatively, if the third parameter is a string:

my.update('table_name',{
	'field_1':'value_1',
	'field_2':'value_2',
	'field_3':'value_3',
},
	'field_2=value_2 OR field_3=value_3
)

This translates to:

UPDATE table_name SET field_1=value_1,field_2=value_2,field_3=value_3 WHERE field_2='value_2' OR field_3='value_3'

Querying and Listing Data

The SELECT function works similarly to the previous cases. You provide the table name, an optional WHERE condition as an array or string, and optionally an ORDER clause as the third parameter.

To filter data:

lst = my.select('table_name', {'field_1': value_1})

lst = my.select('table_name', "field_1='value_1'")

If there's only one result:

[{'field_1': 'value_1', 'field_2': 'value_2','field_3':'value_3'}]

If there are multiple results:

[{'field_1': 'value_1', 'field_2': 'value_2','field_3':'value_3'},
{'field_1': 'value4', 'field_2': 'value_5','field_3':'value_6'},
{'field_1': 'value_7', 'field_2': 'value_8','field_3':'value_9'}]

You can also use standard SQL WHERE clauses with LIKE, OR, >, <, etc.:

lst = my.select('table_name', "field_1 LIKE '%value%')

ORDER Clause

Technically, the ORDER clause can include any SQL statement that follows the WHERE clause:

lst = my.select('table_name', "field_1 LIKE '%value%',order='LIMIT 10')
lst = my.select('table_name', "field_1 LIKE '%value%',order='LIMIT 1,10')
lst = my.select('table_name', "field_1 LIKE '%value%',order='ORDER BY id DESC')
lst = my.select('table_name', "field_1 LIKE '%value%',order='ORDER BY id DESC LIMIT 5')
lst = my.select('table_name', "field_1 LIKE '%value%',order='ORDER BY id DESC LIMIT 1,10')

Iterating Through Data

To iterate through the data, simply use a for loop:

lst = my.select('table_name')

for item in lst:
    print(item)

for item in lst:
    print(item['field_1'])
    print(item['field_2'])
    print(item['field_3'])

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

easymysql-0.1.9.3.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

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

easymysql-0.1.9.3-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

Details for the file easymysql-0.1.9.3.tar.gz.

File metadata

  • Download URL: easymysql-0.1.9.3.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.0

File hashes

Hashes for easymysql-0.1.9.3.tar.gz
Algorithm Hash digest
SHA256 b37249a5ef7b9f25f76a84a2b122a6e495b0e79c70da8c0cfa5b679145f135e5
MD5 82d26f67f6cb765b84564a293a3dc081
BLAKE2b-256 442a357ea07deb6570c33a0e312188f3235942d450d7815119bf126a1a1d5a9d

See more details on using hashes here.

File details

Details for the file easymysql-0.1.9.3-py3-none-any.whl.

File metadata

  • Download URL: easymysql-0.1.9.3-py3-none-any.whl
  • Upload date:
  • Size: 4.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.0

File hashes

Hashes for easymysql-0.1.9.3-py3-none-any.whl
Algorithm Hash digest
SHA256 8e6b4ca887df947d442fc60874d6bd03b49ad9f0e71f269075e23a0dcbdc80d4
MD5 1d51b123408168c1a4a17a688d336496
BLAKE2b-256 c26bcac5dce35f8945256d778daa1a5b1125d014d508bc1a1661c4bc1596fe67

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