Quantcast
Channel: how to update/delete rows in Bigquery from the python api? - Stack Overflow
Viewing all articles
Browse latest Browse all 4

Answer by vreyespue for how to update/delete rows in Bigquery from the python api?

$
0
0

You can simply use Data Manipulation Language (DML) statements instead of SQL queries when using the Google BigQuery API.

For instance, in order to update specific rows in the following table:

Inventory+-------------------+----------+--------------------+|      product      | quantity | supply_constrained |+-------------------+----------+--------------------+| dishwasher        |       30 |               NULL || dryer             |       30 |               NULL || front load washer |       30 |               NULL || microwave         |       30 |               NULL |+-------------------+----------+--------------------+

you could use the following code:

from google.cloud import bigqueryclient = bigquery.Client()dml_statement = ("UPDATE dataset.Inventory ""SET quantity = quantity - 10 ""WHERE product like '%washer%'")query_job = client.query(dml_statement)  # API requestquery_job.result()  # Waits for statement to finish

obtaining the following results:

Inventory+-------------------+----------+--------------------+|      product      | quantity | supply_constrained |+-------------------+----------+--------------------+| dishwasher        |       20 |               NULL || dryer             |       30 |               NULL || front load washer |       20 |               NULL || microwave         |       30 |               NULL |+-------------------+----------+--------------------+

Viewing all articles
Browse latest Browse all 4

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>