Skip to content

Instantly share code, notes, and snippets.

@jacoby
Created April 21, 2014 20:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jacoby/11154890 to your computer and use it in GitHub Desktop.
Save jacoby/11154890 to your computer and use it in GitHub Desktop.
Module for simplifying database access in Python
#!/usr/bin/env python
import sys
import yaml
import MySQLdb as db
class Database:
config = ''
def __init__( self , client="default" ):
myyaml = '/home/jacoby/.my.yaml'
c = open( myyaml , 'r' )
c_yaml = c.read()
c_obj = yaml.load(c_yaml)
clients = c_obj[ "clients" ]
self.user = clients[client]["user"]
self.database = clients[client]["database"]
self.password = clients[client]["password"]
self.host = clients[client]["host"]
self.con = db.connect(self.host, self.user,
self.password, self.database)
def db_do( self , query='' , values=[] ):
if ( len(query) > 0 ):
with self.con:
cur = self.con.cursor()
cur.execute( query , values )
numrows = int(cur.rowcount)
return numrows
def db_array( self , query='' , values=[] ):
if ( len(query) > 0 ):
output = []
with self.con:
cur = self.con.cursor()
if len( values ) > 0 :
cur.execute( query , values )
else:
cur.execute( query )
numrows = int(cur.rowcount)
for i in range( numrows ):
row = cur.fetchone()
output.append(row)
return output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment