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>The SQL engine cannot tell the difference between the rows.</p>

<p>You should add an <a href="http://sqlite.org/autoinc.html" rel="nofollow">autoincrement</a> or ROWID column in order to have it set as ID.</p>

<p>I do not know the structure of your table as you don't create it here but rather alter it.</p>

<p>If you do happen to create it, create a new <code>INTEGER PRIMARY KEY</code> column which you can then use <code>WHERE</code> on like so:</p>

<pre><code>cur.execute(
    "CREATE TABLE MyTable"
    "(RowID INTEGER PRIMARY KEY,"
    "WorkingID INTEGER)")
</code></pre>

python sqlite