SQL Alchemy无法写入mysql

时间:2019-05-27 14:35:46

标签: python mysql pandas dataframe sqlalchemy

我试图制作可以在php中执行的python程序。 python代码中有一些代码应该将数据写入mysql

import mysql.connector
import pandas as pd
from sqlalchemy import create_engine
from pandas.io import sql


mydb = mysql.connector.connect(host = "localhost", user = "root", passwd = "", db="skripsi")

df = pd.read_sql('SELECT * FROM testing', con=mydb)


k=df.drop(0)
testing=k

bins = [0,1.2,1.6,2.,2.4,2.8,3.2,3.6,float('inf')]
labels = ['7','6','5','4','3','2','1','0']
testing['ipk1'] = testing['ipk1'].astype(float)
testing['ipk2'] = testing['ipk2'].astype(float)
testing['ipk3'] = testing['ipk3'].astype(float)
testing['ipk4'] = testing['ipk4'].astype(float)
testing['ipk5'] = testing['ipk5'].astype(float)
testing['ipk1'] = pd.cut(testing.ipk1, bins=bins, labels=labels)
testing['ipk2'] = pd.cut(testing.ipk2, bins=bins, labels=labels)
testing['ipk3'] = pd.cut(testing.ipk3, bins=bins, labels=labels)
testing['ipk4'] = pd.cut(testing.ipk4, bins=bins, labels=labels)
testing['ipk5'] = pd.cut(testing.ipk5, bins=bins, labels=labels)


testing.loc[testing['jk'] == "P", 'jk'] = "1"
testing.loc[testing['jk'] == "L", 'jk'] = "0"


from sklearn.externals import joblib



gaus=joblib.load('modelgaus.pkl')
tree = joblib.load('modeltree.pkl')
 In[101]:


y_pred=gaus.predict(testing)
h_pred=tree.predict(testing)
df=df.drop(0)
df['nb']=y_pred
df['tree']=h_pred
engine = create_engine("mysql://root:@localhost/skripsi")
con = engine.connect()
df.to_sql(name='hasil_masif',con=con,if_exists='replace')
con.close()

当我以python.exe身份运行此python代码时,它会写入hasil_masif表。但是当我将这个python文件放到php exec时它没有写

我需要python代码来执行并在通过php exec调用时编写mysql表

我已经检查了该程序并逐个测试了每个程序,因为

testing['ipk1'] = testing['ipk1'].astype(float)
testing['ipk2'] = testing['ipk2'].astype(float)
testing['ipk3'] = testing['ipk3'].astype(float)
testing['ipk4'] = testing['ipk4'].astype(float)
testing['ipk5'] = testing['ipk5'].astype(float)

发生了什么事?

0 个答案:

没有答案