I'm trying to write a function in Python that will use the CartoDB API to write data to one of my tables. Does anyone have a simple example that shows how to do this that I can use to start?
Writing data through the API is pretty simple. Here is the most basic,Suppose we have two variables already, how you get them is up to you,
Next, lets create an INSERT statement to use
Create the URL endpoint for our account API
Create an object containing the parameters of our request
Send the request using urllib2
SETUP: You should have a couple libraries imported in your python script
BONUS: Batch inserts are wayyyy better than single line inserts.
Here is an example of a multi-row (3) insert strategy. I use an array to initially store all my row values. Each row value is a comma delimited list, wrapped in parentheses and stored to the array,
Using this, I've wrapped all 3 rows into a single
Here's an example script that I wrote a while back. It basically implements what andrewxhill wrote above, but uses the "requests" library instead of urllib.
It batches together inserts to do 1000 at a time, then uses the API to do the inserts.
The script loops over a set of Lidar files in subdirectories, and inserts the bounding boxes for the lidar files into CartoDB.