Python connection MySQL, Oracle small example

Source: Internet
Author: User
Tags dsn

Import MySQLdb
Import cx_oracle as Ora
Import Pandas as PD
From SQLAlchemy import Create_engine
From Settings Import DATABASES
From sqlalchemy.sql.sqltypes Import *
From math import isNaN

Connect to MySQL

Conn_mysql = MySQLdb.connect (host=databases["stock_diagnosis"].get ("host"), user=databases["Stock_Diagnosis"].get ("User"), passwd=databases["Stock_diagnosis"].get ("passwd"), db=databases["Stock_diagnosis"].get ("db"), charset= " UTF8 ")


Engine_mysql = Create_engine (databases["stock_diagnosis"].get ("engine") + "://" + databases["stock_diagnosis"].get (" User ") +": "+ databases[" stock_diagnosis "].get (" passwd ") +" @ "+ databases[" stock_diagnosis "].get (" host ") +"/"+ Databas es["Stock_diagnosis"].get ("db") + "? Charset=utf8")

Connect Oracle

DSN = ORA.MAKEDSN (databases["Datacenter"].get ("host", "127.0.0.1"),
databases["Datacenter"].get ("Port", "1521"),
databases["Datacenter"].get ("Sid", "Upapp"))
Oraconn = Ora.connect (databases["Datacenter"].get ("user"),
databases["Datacenter"].get ("passwd"),
dsn
Oraengine = Create_engine (databases["Datacenter"].get ("engine")
+ "://"
+ databases["Datacenter"].get ("user")
+":"
+databases["Datacenter"].get ("passwd")
+"@"
+databases["Datacenter"].get ("host")
+"/"
+databases["Datacenter"].get ("Sid")
+ "? Charset=utf8")

#查询数据

strSQL = "Select C.indu_name from Upapp. Pub_indu_code c where C.indu_uni_code in (select B.indu_uni_code from Upapp. Pub_com_indu_rela b where b.indu_sys_code= ' "and B.com_uni_code in" (Select A.com_uni_code from Upapp. Stk_basic_info a where a.stk_code= ' 600000 ')) "
Dfora = Pd.read_sql (strSQL, Oraconn) #返回一个DataFrame

#写库

#构建DataFrame, save the money face data in the STK_MONEY_FLOW_FAC table

#self. _stkpool_uni, codes, end_date ... These are the list (the value of the append, but the number of elements of the list is the same)
Dfdata = {"Stk_uni_code": Self._stkpool_uni, "Stk_code": Codes, "End_date": end_date,
"SCORE_FAC": Score, "DIS_FAC":d Escri, "RAT_FAC": Star, "MAC_FAC": MAC}
DF = PD. DataFrame (Data=dfdata)
Dttype = {"Stk_uni_code": INT, "Stk_code": VARCHAR (Ten), "end_date":D ate,
"SCORE_FAC": FLOAT, "DIS_FAC": varchar, "RAT_FAC": varchar (+), "MAC_FAC": varchar (200)}
Df.to_sql ("Stk_money_flow_fac", Oraengine, if_exists= ' append ', Dtype=dttype)

Python connection MySQL, Oracle small example

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.