Python MySQLdb select(选择) 封装

对MySQL选择的封装

def select_data(sql):
    conn = MySQLdb.connect(host="10.10.10.77", user="xxxxx", passwd="xxxx", db="xxxx", charset="utf8")
    cursor = conn.cursor(cursorclass=MySQLdb.cursors.DictCursor)

    cursor.execute(sql)
    result = cursor.fetchall()

    resultList = []
    for i in result:
        resultList.append(i)
    cursor.close()
  # 得到的结果是字典格式
return resultList

 

使用说明:

 sql = """ SELECT
                *
            FROM
                `oversea_thierrydemaigret_2017_2_zc`
            WHERE
                item_auct_time LIKE '%2017%'
            AND (
                item_auct_time LIKE '%décembre%'
                OR item_auct_time LIKE '%novembre%'
                OR item_auct_time LIKE '%octobre%'
                OR item_auct_time LIKE '%septembre%'
                OR item_auct_time LIKE '%août%'
                OR item_auct_time LIKE '%Juillet%'
            )  """

    resultList = select_data(sql)

 

posted @ 2018-01-18 13:25  淋哥  阅读(1725)  评论(0编辑  收藏  举报