关闭

Python 对Oracle的操作

标签: oraclepythonquerycommandsql
810人阅读 评论(0) 收藏 举报
分类:

简单的封装

def Oracle_Exec(SqlStr):
    "Execute oracle command"
    conn = cx_Oracle.connect(DB_UserName, DB_UserPwd, DB_ConnectStr)    
    cursor = conn.cursor()
    try:
        cursor.execute(SqlStr)
        conn.commit()
        return True    
    except:
        conn.rollback()
        print "Exec sql failed: %s" % SqlStr
        return False
    finally:        
        cursor.close()
        conn.close()
        
def Oracle_Query(SqlStr, debug = 0):
    "Execute oracle query, and return datalist"
    datalist = []   
    conn = cx_Oracle.connect(DB_UserName, DB_UserPwd, DB_ConnectStr)
    cursor = conn.cursor()
    try:
        cursor.execute(SqlStr)    
        while 1:     
            rs = cursor.fetchone()
            if rs == None:
                break
            datalist.append(rs)
        if debug:
            fieldnames = []
            for field in cursor.description:
                fieldnames.append(field[0])
            print fieldnames
            print datalist
            print "Query success!"            
    except:
        print "Exec sql failed: %s" % SqlStr         
    finally:        
        cursor.close()
        conn.close()
        return datalist     


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:111503次
    • 积分:1641
    • 等级:
    • 排名:千里之外
    • 原创:50篇
    • 转载:40篇
    • 译文:0篇
    • 评论:4条
    文章分类
    最新评论