Skip to content

Instantly share code, notes, and snippets.

@anjianshi
Created January 13, 2014 06:06
Show Gist options
  • Save anjianshi/8395421 to your computer and use it in GitHub Desktop.
Save anjianshi/8395421 to your computer and use it in GitHub Desktop.
python MySQLdb helper
# -*- coding: utf-8 -*-
import MySQLdb
def get_db(database, user="root", passwd=None, charset='utf8'):
return MySQLdb.connect(host="localhost", user=user, passwd=passwd, db=database, charset=charset)
def execute(db, sql):
cur = db.cursor()
cur.execute(sql)
return cur
def fetchall(db, sql, index=None):
result = []
for row in execute(db, sql).fetchall():
if index is not None:
row = row[index]
result.append(row)
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment