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 mysql_wrap import MysqlWrap, ConnectionOptions
options = ConnectionOptions(
host="127.0.0.1",
db="mydatabase",
user="username",
passwd="password",
keep_alive=True
)
db = MysqlWrap(**options)
For SSL Connection
from mysql_wrap import MysqlWrap
db = MysqlWrap(
host="127.0.0.1",
db="mydatabase",
user="username",
passwd="password",
ssl = {'cert': 'client-cert.pem', 'key': 'client-key.pem'},
keep_alive=True
)
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).
db.update("books", {"discount": 0})
db.update("books",
{"discount": 10},
["id=1"]
)
db.update("books",
{"discount": 10},
("id=%s AND year=%s", [id, year])
)
insertBatch(table, rows{})
Insert Multiple values into 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.
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"])
book = db.getOne("books", ["name", "year"], ("id=1"))
books = db.getAll("books",
["id", "name"],
("year > %s and price < %s", [year, 12.99])
)
books = db.getAll("books",
["id", "name", "year"],
("year > %s and price < %s", [year, 12.99]),
["year", "DESC"],
[0, 10]
)
lastId()
Get the last insert id
db.lastId()
lastQuery()
Get the last query executed
db.lastQuery()
delete(table, fields[], condition[], order[], limit[])
Delete one or more records based on a condition (or no condition)
db.delete("books")
db.delete("books", ("price > %s AND year < %s", [25, 1999]))
query(table)
Run a raw SQL query. The MySQLdb cursor is returned.
db.query("DELETE FROM books WHERE year > 2005")
commit()
Insert, update, and delete operations on transactional databases such as innoDB need to be committed
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}