簡體   English   中英

使用 python 查詢到 BigQuery(Python 字符串格式)時出現問題

[英]Problems querying with python to BigQuery (Python String Format)

我正在嘗試查詢 BigQuery 以修改一行的所有值(在 python 中)。 當我使用一個簡單的字符串來查詢時,我沒有問題。 然而,當我引入字符串格式時,查詢不起作用。 如下所示,我提出了相同的查詢,但減少了我正在修改的列數。 我已經通過定義客戶端等連接到 BigQuery(並且工作正常)。

我試過:

"UPDATE `riscos-dev.survey_test.data-test-bdrn` SET informaci_meteorol_gica = {inf}, risc = {ri} WHERE objectid = {obj_id}".format(inf = df.informaci_meteorol_gica[index], ri = df.risc[index], obj_id = df.objectid[index])

以格式指定輸入值: df.informaci_meteorol_gica[index] = 'Neu' ,也是df.risc[index] and df.objectid[index] = 3的字符串

我收到以下錯誤消息:

BadRequest: 400 Braced constructors are not supported at [1:77]

我建議您使用Pythonf string格式化的另一種方法,而不是使用stringformat方法:

def build_query():
  inf = "'test_inf'"
  ri = "'test_ri'"
  obj_id = "'test_obj_id'"

  return f"UPDATE `riscos-dev.survey_test.data-test-bdrn` SET informaci_meteorol_gica = {inf}, risc = {ri} WHERE objectid = {obj_id}"

if __name__ == '__main__':
  query = build_query()

  print(query)

結果是:

UPDATE `riscos-dev.survey_test.data-test-bdrn` SET informaci_meteorol_gica = 'test_inf', risc = 'test_ri' WHERE objectid = 'test_obj_id'

我在我的示例中模擬了查詢參數:

inf = "'test_inf'"
ri = "'test_ri'"
obj_id = "'test_obj_id'"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM