python简单封装MySQLdb模块

Python代码


# -*- coding: UTF-8 -*-
import os
import MySQLdb

def getConn(host, user, pwd, db):
    conn = MySQLdb.Connect(host=host, user=user, passwd=pwd, db=db, charset="utf8", use_unicode="True")
    return conn

def insert(sql, conn):
    cursor = conn.cursor()
    cursor.execute(sql)
    conn.commit()

def update(sql, conn):
    cursor = conn.cursor()
    cursor.execute(sql)
    conn.commit()

def selectRows(sql, conn):
    cursor = conn.cursor()
    cursor.execute(sql)
    conn.commit()
    rows = cursor.fetchall()
    return rows

def closeConn(conn):
    conn.close()
    

点赞