Python:查询数据库

2020-07-06  本文已影响0人  Liu____

pymysql安装

安装:
pip3 install PyMySQL
查看版本:
pip3 show pymysql
import pymysql

import xcodeSet

import os 

from pymysql.cursors import DictCursor

def get_dataDic(sql):
    sql = "SELECT * FROM `xxxx` WHERE `xxx` LIKE 'xxxx' LIMIT 0, 1000" 
    db = pymysql.connect(host='',user='',passwd='',db='',
    charset='utf8')

    #cur = db.cursor()
    #以字典形式
    cur = db.cursor(DictCursor)

    dataDic = {}
    try:
        cur.execute(sql)
         #描述名
        #description = cur.description
        fetchall = cur.fetchall()
        dataDic = fetchall[0]
        #cur.fetchone()
    except Exception as e:
        raise e
    finally:
        db.close()
    print("---------------sql fetch complete:\n",dataDic)
    return dataDic


上一篇下一篇

猜你喜欢

热点阅读