printing a int variable in a sqlite3 update query
<p>I am trying to update a sqlite3 db once I get a true statement but I can't feed my variable, which is an int, to the query which is a str.
Here is the database </p>

<pre><code>CREATE TABLE STATICIPS(
   ID INTEGER PRIMARY KEY   AUTOINCREMENT,
   IP           CHAR(50) NOT NULL,
   CITY           CHAR(50) NOT NULL,
   INCOMPLETE BOOL
, CMTSIP CHAR(50));
</code></pre>

<p>Here is the code</p>

<pre><code>#!/usr/bin/python

import sqlite3
conn = sqlite3.connect('ipdb.sqlite')

cursor = conn.execute("SELECT ID, IP, CITY, INCOMPLETE, CMTSIP  from STATICIPS WHERE CITY='LS'")
for row in cursor:
 if (row[3] == 1):
    print row[1]
    searchfile = open("arp-ls.txt", "r")
    for line in searchfile:
        if row[1] + ' ' in line: 
            print line
            conn.execute("UPDATE STATICIPS set INCOMPLETE = 0 where ID = " + row[0])
            conn.commit
    searchfile.close()`
</code></pre>

<p>The row[0] is the id in the db which is an int. I get this when i run the code:</p>

<pre><code> Traceback (most recent call last):
   File "getinc.py", line 16, in &lt;module&gt;
     conn.execute("UPDATE STATICIPS set INCOMPLETE = 0 where ID = " + row[0])
 TypeError: cannot concatenate 'str' and 'int' objects
</code></pre>

<p>So my question is how do I make it that row[0] prints correctly in my query so I can update the sqlite entry of this specific ID?</p>

<p>Don't use concatenation at all when constructing SQL queries. Use <em>SQL parameters</em> instead. These are placeholders in the query where the database will fill in the values for you.</p>

<p>This ensures that those values are properly escaped (avoiding SQL injection attacks), and allows the database to re-use queries for different values (giving you a performance boost).</p>

<p>In <code>sqlite3</code>, placeholders are question marks; you pass in the values in a sequence as a second argument to <code>execute()</code>:</p>

<pre><code>conn.execute("UPDATE STATICIPS set INCOMPLETE = 0 where ID = ?",
             [row[0]])
</code></pre>

<p>For the <em>general</em> case (so not using SQL queries), you'd convert that integer value to a string first. Either by using <code>str(row[0])</code> or by using <code>str.format()</code> string templating.</p>

python sqlite3