Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import MySQLdb as mysql
- db = None
- # Connect to database
- def connect(host, user, password, db_name):
- global db
- db = mysql.connect(host=host, user=user, passwd=password, db=db_name)
- # Build result as a dict
- def build_query_result(cursor):
- columns = list(map(lambda item: item[0], cursor.description))
- return list(map(lambda item: { column: item[index] for (index,column) in enumerate(columns) }, cursor.fetchall()))
- # Run a query
- def query(sql, params=[]):
- global db
- c = db.cursor()
- c.execute(sql, tuple(params))
- return build_query_result(c)
- # Run an update
- def update(sql, params=[]):
- global db
- c = db.cursor()
- c.execute(sql, tuple(params))
- db.commit()
- return (c.lastrowid, c.rowcount)
Add Comment
Please, Sign In to add comment