python连接数据库

python连接MongoDB查询:

def fech_mongo(data):
    host = data.pop("host")
    port = int(data.pop("port"))
    db_name = data.pop("db_name")
    batch_no = data.pop("batch_no")
    print(host,port,db_name,batch_no,data)
    message = ''
    for field in data.keys():
        client = pymongo.MongoClient(host, port)
        db = client[db_name]
        table_name = field.split(".")[0]
        key = field.split(".")[1]
        collection_set = db[table_name]
        try:
            value = collection_set.find_one({'batch_no': batch_no})[key]
            if value != data[field]:
                message += field+" "+data[field]+"不等于"+value+","
        except Exception:
            logger.exception('')

    return message

 

python连接sqlserver查询

def fethData():
    host = '10.138.**。**'
    user = 'sa'
    password = 'f******93'
    database = 'po******n'
    conn = pymssql.connect(host=host, user=user, password=password, database=database, charset='UTF8')
    cursor = conn.cursor()
    cursor.execute('select top 1 businessid from business')
    results = cursor.fetchall()
    conn.close()
    print(results)

注意:charset='UTF8'

参考:https://www.cnblogs.com/baiyangcao/p/pymssql_basic.html

 

python连接Oracle

1.安装cx_Oracle版本:cx_Oracle-5.3-11g.win-amd64-py3.4.exe(注意与python版本匹配)

   下载地址:https://pypi.python.org/pypi/cx_Oracle/5.3

   不建议直接使用pip install cx_Oracle命令安装

2.安装oracle客户端:instantclient-basic-windows.x64-11.2.0.4.0.zip(注意版本和位数与python匹配)   

   下载地址:http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html

   把下载的文件解压,复制oci,oraocci11,oraociei11的3个DLL粘贴到你的PY目录的Libs/site-packages文件夹下面。

3.python连接Oracle数据脚本 

import os
os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'
# os.environ['path'] = 'D:\Oracle\instantclient_11_2'
import cx_Oracle



if __name__ == '__main__':
    str = 'vcs_prod/vcs_prod@10.138.60.145/orcl'
    conn = cx_Oracle.connect(str)
    sql = "select * from M_CONTACT_INFO where  BUSINESS_ID = '304815'"
    cr = conn.cursor()
    cr.execute(sql)
    data = cr.fetchall()
    print(data)

参考:https://www.cnblogs.com/lansan0701/p/8039332.html

posted @ 2019-03-11 10:18  carlvine  阅读(214)  评论(0编辑  收藏  举报