A very simple and intuative mysql client wrapper for pymysql.
- Run the command to install:
pip install nmi_mysql
- Make sure you install
pymysql
. You can check out the instructions here
Minimal and straightforward when doing queries
- Imports the nmi-mysql client library
from nmi_mysql import nmi_mysql
- Initialization: Accepts two parameters, first being the config object and the second specifying if autoconnect to db is enabled. If set to false, call
con.connect()
con = nmi_mysql.DB(conf, True)
- Query execution: Accepts two parameters. The first is the query and the second is the list of parameters to be used. See example below
data = con.query(query, params)
- Closing connection
con.close()
Sample config object
conf = {
'host': 'localhost',
'user': 'root',
'password':'',
'db': 'mydb',
'port': 3306
}
from nmi_mysql import nmi_mysql
connection = nmi_mysql.DB(conf, True)
data1 = connection.query('SELECT * FROM mytable WHERE name = %s', ['ninz'])
data2 = connection.query('SELECT * FROM mytable WHERE name IN (%s) AND age = %s', [['john', 'doe'], 10])
connection.close()
print(data)
print(data2)
from nmi_mysql import nmi_mysql
connection = nmi_mysql.DB(conf, True)
# Throws an error upon failure
result = connection.query('INSERT INTO users VALUES(%s)', [user_object])
connection.close()