2016-08-29 30 views
1

我有Postgres表稱爲具有多個appIds的記錄。我正在寫一個函數來提取一個AppId(ABC0123)的數據,並且希望將這個結果存儲在數據框df中。 我創建了一個變量AppId1來存儲我的AppId(ABC0123)並將它傳遞給查詢。 該腳本已執行,但未創建數據框。使用輸入定義Python函數

def fun(AppId): 
    AppId1=pd.read_sql_query(""" select "AppId" from "Records" where "AppId"='%s'""" %AppId,con=engine) 
    query = """" SELECT AppId from "Records" where AppId='%s' """ % AppId1 
    df=pd.read_sql_query(query,con=engine) 
    return fun 

fun('ABC0123') 
+0

'回報fun' ?? ..爲什麼呢?...你應該返回'df'不是你的函數參考?..'回報df' –

+0

你'query'的定義以四個雙引號開頭。這意味着第四個引號是字符串數據的一部分,所以這個字符串包含''SELECT AppId from'Records',其中AppId ='%s''。是否有錯字輸入或代碼錯誤? – ShadowRanger

回答

0
  • 更改%AppId1到%的AppId只。
  • 迴歸DF

def fun(AppId): 
    AppId1=pd.read_sql_query(""" select 'AppId' from 'Records' where 'AppId'='%s'""" %AppId,con=engine) 
    query = """ SELECT 'AppId' from 'Records' where AppId='%s' """ %AppId 
    df=pd.read_sql_query(query,con=engine) 
    return df 
+0

謝謝。 。我將它更改爲%AppId並返回df - 我可以打印appId值,但未創建變量df。 – Prasad

+0

@Prasad - 請再次檢查編輯後的代碼並通知我。 –

+0

感謝Dinesh ..它的工作原理 – Prasad