python 使用Python从远程数据库运行SQL查询。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 使用Python从远程数据库运行SQL查询。相关的知识,希望对你有一定的参考价值。

#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
This module is used to run SQL code from remote DB host.
"""

__version__ = '0.2'
__date__ = '2016-04-13'
__author__ = 'Guangning Yu'

import sys
import cx_Oracle
import csv
import codecs
import os
from optparse import OptionParser

os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'

PARA_DICT={}

class Connection():
    def __init__(self, db):
        self.db = db
        self.cursor = db.cursor()
        
    def query(self, query):
        return self.cursor.execute(query)
        
    def fetchall(self):
        return self.cursor.fetchall()
        
    def close(self):
        self.cursor.close()
        self.db.close()
        
def run_sql(user_name, password, host, sql, output_file):
    db = cx_Oracle.connect(user_name, password, host)
    conn = Connection(db)
    cursor = conn.cursor
    result = conn.query(sql)
    with open(output_file, 'w') as f:
        f.write(codecs.BOM_UTF8)
        output = csv.writer(f, delimiter=',', dialect='excel')
        header = []
        for col in cursor.description:
            header.append(col[0])
        output.writerow(header)
        for line in result:
            output.writerow(line)
    conn.close()
    
def pass_parameter(option, opt, value, parser):
    try:
        key = value.strip().split('=')[0]
        vlu = value.strip().split('=')[1]
        PARA_DICT[str(key)] = str(vlu)
    except Exception as e:
        raise Exception('The parameter "%s" cannot be parsed.' % value)
    return
  
def set_parameter(sql, para_dict):
    for (key, vlu) in para_dict.items():
        sql = sql.replace('&'+key, vlu)
    return sql
    
    
    
if __name__ == '__main__':
    parser = OptionParser()
    parser.add_option("-u", dest="user_name", default=None, help="e.g.'yugnw'")
    parser.add_option("-p", dest="password",  default=None, help="e.g.'88888888'")
    parser.add_option("-t", dest="host",      default=None, help="e.g.'192.168.xx.xx:1521/xxx'")
    parser.add_option("-s", dest="script",    default=None, help="e.g.'/home/yugnw/query.sql'")
    parser.add_option("-o", dest="output",    default=None, help="e.g.'/home/yuwnw/result.csv'")
    parser.add_option("--var", action="callback", type="string", callback=pass_parameter, help="e.g.start_dt=\"2016-04-01\"; then write the parameter like \"create_dt >= '&start_dt' in the SQL code\"")
    (options, args) = parser.parse_args()
    
    if options.user_name:
        user_name = options.user_name
    else:
        raise Exception("请输入用户名")
        
    if options.password:
        password = options.password
    else:
        raise Exception("请输入密码")
        
    if options.host:
        host = options.host
    else:
        raise Exception("请输入HOST名称")
        
    if options.script and os.path.isfile(options.script):
        script = options.script
    else:
        raise Exception("请输入正确的脚本路径")
        
    if options.output:
        output = options.output
    else:
        raise Exception("请输入输出文件的路径")
        
    with open(script, 'rb') as f:
        sql = f.read()
        
    sql = set_parameter(sql, PARA_DICT)
    
    run_sql(user_name, password, host, sql, output)

以上是关于python 使用Python从远程数据库运行SQL查询。的主要内容,如果未能解决你的问题,请参考以下文章

python文件X11远程运行问题

如何从 .sh 可执行文件执行远程 Python 文件?

使用python子进程和ssh读取远程文件?

如何从远程连接访问 python http 服务器?

如何从远程连接访问python http服务器?

Windows计划任务调度Python脚本写NFS远程目录异常