Skip to main content

Simple wrapper for common mysql queries, including utilities for pandas DataFrames

Project description

mysql_wrap

A wrapper for Python Mysqldb with pandas functionality.

Built on top of the SimpleMysql package available at https://github.com/knadh/simplemysql

Installation

with pip:

pip install mysql-wrap

from source:

python -m pip install .

Usage

For normal connection

from simplemysql import SimpleMysql

db = SimpleMysql(
	host="127.0.0.1",
	db="mydatabase",
	user="username",
	passwd="password",
	keep_alive=True # try and reconnect timedout mysql connections?
)

For SSL Connection

from simplemysql import SimpleMysql

db = SimpleMysql(
    host="127.0.0.1",
    db="mydatabase",
    user="username",
    passwd="password",
    ssl = {'cert': 'client-cert.pem', 'key': 'client-key.pem'},
    keep_alive=True # try and reconnect timedout mysql connections?
)
# insert a record to the <em>books</em> table
db.insert("books", {"type": "paperback", "name": "Time Machine", "price": 5.55, year: "1997"})

book = db.getOne("books", ["name"], ["year = 1997"])

print "The book's name is " + book.name

Utility methods

getDataTypefromDType(), setMySqlFieldName()

Pandas methods

getTable(), createTable(), SyncColumns(), insertFromDataFrame(), InsertOrUpdateFromDataFrame(), CreateInsertTable(), CreateUpdateTable()

regular Query methods

insert(), update(), insertOrUpdate(), describe(), delete(), getOne(), getAll(), lastId(), query(), tableExist()

insert(table, record{})

Inserts a single record into a table.

db.insert("food", {"type": "fruit", "name": "Apple", "color": "red"})
db.insert("books", {"type": "paperback", "name": "Time Machine", "price": 5.55})

update(table, row{}, condition[])

Update one more or rows based on a condition (or no condition).

# update all rows
db.update("books", {"discount": 0})

# update rows based on a simple hardcoded condition
db.update("books",
	{"discount": 10},
	["id=1"]
)

# update rows based on a parametrized condition
db.update("books",
	{"discount": 10},
	("id=%s AND year=%s", [id, year])
)

insertBatch(table, rows{})

Insert Multiple values into table.

# insert multiple values in table
db.insertBatch("books", [{"discount": 0},{"discount":1},{"discount":3}])

insertOrUpdate(table, row{}, key)

Insert a new row, or update if there is a primary key conflict.

# insert a book with id 123. if it already exists, update values
db.insertOrUpdate("books",
		{"id": 123, type": "paperback", "name": "Time Machine", "price": 5.55},
		"id"
)

getOne(table, fields[], where[], order[], limit[])

getAll(table, fields[], where[], order[], limit[])

Get a single record or multiple records from a table given a condition (or no condition). The resultant rows are returned as namedtuples. getOne() returns a single namedtuple, and getAll() returns a list of namedtuples.

book = db.getOne("books", ["id", "name"])
# get a row based on a simple hardcoded condition
book = db.getOne("books", ["name", "year"], ("id=1"))
# get multiple rows based on a parametrized condition
books = db.getAll("books",
	["id", "name"],
	("year > %s and price < %s", [year, 12.99])
)
# get multiple rows based on a parametrized condition with an order and limit specified
books = db.getAll("books",
	["id", "name", "year"],
	("year > %s and price < %s", [year, 12.99]),
	["year", "DESC"],	# ORDER BY year DESC
	[0, 10]			# LIMIT 0, 10
)

lastId()

Get the last insert id

# get the last insert ID
db.lastId()

lastQuery()

Get the last query executed

# get the SQL of the last executed query
db.lastQuery()

delete(table, fields[], condition[], order[], limit[])

Delete one or more records based on a condition (or no condition)

# delete all rows
db.delete("books")

# delete rows based on a condition
db.delete("books", ("price > %s AND year < %s", [25, 1999]))

query(table)

Run a raw SQL query. The MySQLdb cursor is returned.

# run a raw SQL query
db.query("DELETE FROM books WHERE year > 2005")

commit()

Insert, update, and delete operations on transactional databases such as innoDB need to be committed

# Commit all pending transaction queries
db.commit()

To run tests:

  • add your test file to the tests/ folder

  • import the modules you want to test using src.folder.module path

  • run tests from terminal from the project root folder: python3 -m unittest tests.{test file}

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

mysql_wrap-1.0.1.tar.gz (11.5 kB view details)

Uploaded Source

Built Distribution

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

mysql_wrap-1.0.1-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file mysql_wrap-1.0.1.tar.gz.

File metadata

  • Download URL: mysql_wrap-1.0.1.tar.gz
  • Upload date:
  • Size: 11.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.7.17

File hashes

Hashes for mysql_wrap-1.0.1.tar.gz
Algorithm Hash digest
SHA256 9808de2d10922b364f9cf042a56bdb24fcac68cf1ec175da54d5a1e90b6df16b
MD5 a61dd48765ebf4bfbab2eee675dd28a3
BLAKE2b-256 ec98c08f16c2e9c02de19b56c5026eff4c2d0103dd553b797f261546149526e9

See more details on using hashes here.

File details

Details for the file mysql_wrap-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: mysql_wrap-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 9.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.7.17

File hashes

Hashes for mysql_wrap-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 77596c4773d273a304030498817fe11c1c08977a56fb1c312271254b522cacd8
MD5 96d0efda619c7e855876192bf4079303
BLAKE2b-256 d7812986cd563f1063b99721ce4805497aad9cbefaa515e05ca987912d69dca9

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