
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Simple wrapper for common mysql queries, including utilities for pandas DataFrames
A wrapper for Python Mysqldb with pandas functionality.
Built on top of the SimpleMysql package available at https://github.com/knadh/simplemysql
with pip:
pip install mysql_wrap
from source:
python -m pip install .
from mysql_wrap import MysqlWrap, ConnectionOptions
options = ConnectionOptions(
host="127.0.0.1",
db="mydatabase",
user="username",
passwd="password",
keep_alive=True # try and reconnect timedout mysql connections?
)
db = MysqlWrap(**options)
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 # 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
getDataTypefromDType(), setMySqlFieldName()
getTable(), createTable(), SyncColumns(), insertFromDataFrame(), InsertOrUpdateFromDataFrame(), CreateInsertTable(), CreateUpdateTable()
insert(), update(), insertOrUpdate(), describe(), delete(), getOne(), getAll(), lastId(), query(), tableExist()
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 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])
)
Insert Multiple values into table.
# insert multiple values in table
db.insertBatch("books", [{"discount": 0},{"discount":1},{"discount":3}])
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"
)
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
)
Get the last insert id
# get the last insert ID
db.lastId()
Get the last query executed
# get the SQL of the last executed query
db.lastQuery()
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]))
Run a raw SQL query. The MySQLdb cursor is returned.
# run a raw SQL query
db.query("DELETE FROM books WHERE year > 2005")
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}
FAQs
Simple wrapper for common mysql queries, including utilities for pandas DataFrames
We found that mysql-wrap demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.