Created
January 13, 2014 06:06
-
-
Save anjianshi/8395421 to your computer and use it in GitHub Desktop.
python MySQLdb helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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