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