Python + Sqlite: Add a new column and fill its value at interval of 2
<p>I want to add a new column to a <code>db</code> file and fill its value at an interval of 2. Here I wrote some codes...</p>

<pre><code>import sqlite3

WorkingFile = "C:\\test.db"
con = sqlite3.connect(WorkingFile)

cur = con.cursor()

cur.execute("ALTER table MyTable add column 'WorkingID' 'long'") # Add a column "WorkingID" 

rows = cur.fetchall()

iCount = 0
for row in rows:
    iCount = iCount + 2 
    print iCount
    cur.execute("UPDATE MyTable SET WorkingID = ?" , (iCount,)) # Here I have question: How to write the WHERE command?

con.commit()
cur.close()
</code></pre>

<p>The code above gives me a new Column with same values. Something like this:</p>

<pre><code>WorkingID
10
10
10
...
</code></pre>

<p>But I want a result like this:</p>

<pre><code>WorkingID
2
4
6
8
10
...
</code></pre>

<p>My question on the code is, I dont know how to write the <code>WHERE</code> code under <code>UPDATE</code>. Would you please help me out? Thanks.</p>

<p>If you can't add ID to your table, you can make a really strange way. So..
Before your loop you make a query</p>

<pre><code>UPDATE MyTable SET WorkingID = 2 LIMIT 1
</code></pre>

<p>And after</p>

<pre><code>iCount = 2
for row in rows:
    iCount = iCount + 2 
    print iCount
    cur.execute("UPDATE MyTable SET WorkingID = ? WHERE WorkibgID is NULL LIMIT 1" , (iCount,))
</code></pre>

<p>It's not a good way, but it should work.</p>

python sqlite