Skip to content

pandafox/nmi_mysql

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

nmi-mysql

A very simple and intuative mysql client wrapper for pymysql.

Installation

  • Run the command to install: pip install nmi_mysql
  • Make sure you install pymysql. You can check out the instructions here

Usage

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    
}
SELECT operations
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)
INSERT operations
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()

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 100.0%