MySQL Oracle python connection

Source: Internet
Author: User
Tags rowcount


#encoding: Utf-8

#dataProc

#auth Xiajikun

Import Sys

# Oracle Library Connectivity Module

Import Cx_oracle

# MySQL Library connection

Import MySQLdb

Import time

Import OS


#水电煤库

# SDM = ' username/[email protected]:p ort/servicename '

Db_ora_str = ' Admin/[email protected]:1521/c9db1111 '


# Oracle Connectivity

Class Oraclelogin (object):

#初始化, create a connection database object

def __init__ (self, loginName):

#--Establishing a connection

Self.conn = Cx_oracle.connect (loginName)

#--fetching Cursors

Self.cursor = Self.conn.cursor ()

Self.l_v_status = Self.cursor.var (cx_oracle.number)

Self.l_v_return_code = Self.cursor.var (cx_oracle.string)

Self.l_v_sno = Self.cursor.var (cx_oracle.string)

#查询sql方法

def selectsql (self, SQL):

Self.cursor.execute (SQL)

Self.result_sql = Self.cursor.fetchall ()

Self.count = Self.cursor.rowcount

Self.conn.commit ()

#执行sql方法

def execsql (Self,sql):

Self.cursor.execute (SQL)

Self.conn.commit ()

#执行存储过程方法

def getmetadata (self,procdname, SQL, Procargs):

Self.procresult = Self.cursor.callproc (Procdname, Procargs)

Self.cursor.execute (SQL)

Self.result_pro = Self.cursor.fetchall ()

Self.count = Self.cursor.rowcount

Self.conn.commit ()

#无用时自动析构此对象

def __del__ (self):

Self.cursor.close ()

Self.conn.close ()



# MySQL Connection

Class Mysqllogin (object):

#初始化, connect the database automatically

def __init__ (self, DB_STR):

# self.conn = MySQLdb.connect (host= ' 10.7.11.242 ', user= ' Credcard ', passwd= ' 5sFVDVCeKo ', db= ' Credcard ', port=3320)

Self.conn = MySQLdb.connect (Host=db_str[0],user=db_str[1],passwd=db_str[2],db=db_str[3],port=db_str[4])


#self. conn = MySQLdb.connect ('%s '% db_str)

#self. conn = MySQLdb.connect ('%s,%s,%s,%s,%d '% (DB_STR)

Self.cur = Self.conn.cursor ()

def exec_sql (self, SQL):

Self.cur.execute (SQL)

Self.conn.commit ()

def select_sql (self, SQL):

Self.cur.execute (SQL)

Self.tmpdata = Self.cur.fetchall ()

#自动断开游标和连接

def __del__ (self):

Self.cur.close ()

Self.conn.close ()



Db_str = (' 10.1.1.1 ', ' username ', ' 123456 ', ' db_name ', 3306)

Db_obj = Mysqllogin (DB_STR)

sql = ' SELECT * from tabname limit 10 '

Db_obj.select_sql (SQL)

Print Db_obj.tmpdata


MySQL Oracle python connection

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.