#!/usr/bin/python import MySQLdb class SpiderPDO: def __init__(self): db_host = '127.0.0.1' db_user = 'root' db_pass = '666666' db_name = 'db01' conn = MySQLdb.connect(db_host, db_user, db_pass, db_name, charset = 'utf8') self.cursor = conn.cursor(cursorclass = MySQLdb.cursors.DictCursor) def execute(self, sql, params = None): return self.cursor.execute(sql, params) def fetch(self, sql, params = None): self.execute(sql, params) return self.cursor.fetchone() def fetchall(self, sql, params = None): self.execute(sql, params) return self.cursor.fetchall() def close(self): return self.cursor.close() pdo = SpiderPDO() def execute(sql, params = None): if params: sql = prepare(sql) return pdo.execute(sql, params) def fetch(sql, params = None): if params: sql = prepare(sql) return pdo.fetch(sql, params) def fetchall(sql, params = None): if params: sql = prepare(sql) return pdo.fetchall(sql, params) def close(): pdo.close() def prepare(sql): return sql.replace('?', '%s');
写的第一个python module,感觉就是用方法呼叫