美文网首页
Python:查询数据库

Python:查询数据库

作者: Liu____ | 来源:发表于2020-07-06 16:06 被阅读0次

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


相关文章

网友评论

      本文标题:Python:查询数据库

      本文链接:https://www.haomeiwen.com/subject/cdtgqktx.html