Python databases
<p>I would like run a script that will ping some servers from a database and update a field in a database.</p>

<p>My database is sqlite and the layout is:</p>

<p><code>name IP last_poll status</code></p>

<p>Typically the values will be:</p>

<p><code>HQ 192.168.1.1 12:00 2016-01-01 online</code></p>

<p>The idea is to have a loop to select the data from the database but how can I insert the result back into the same row?</p>

<p>I have the basic setup where I can ping a single host:</p>

<pre><code>import os
hostname = "192.168.1.1"
response = os.system("ping -c 1 " + hostname)

if response == 0:
    print "online"
</code></pre>

<p>I could iterate through the tuple if I select the data but I don't know how I can insert it back into the rows.</p>

<p>Thank you, I have a list of tuples now achieved with this: </p>

<p><code>def read_from_db():
c.execute('SELECT ip FROM sites')
data = c.fetchall()
hostnames = tuple(sum(data, ()))</code></p>

<p><code>for h in hostnames:
    responses = tuple(os.system('ping -c 1 ' + h) for h in hostnames)</code></p>

<p><code>z = zip(hostnames, responses)</code></p>

<p>That gives me a list of tuples:</p>

<p><code>[(u'10.0.0.260', 512), (u'10.0.0.254', 0)]</code></p>

<p>But how can that be used to update the database in a batch checking where the IP and updating the status with a code?</p>

python database sqlite