sqlite3 INSERT IF NOT EXIST (with Python)
<p>First of all, I am really super-new so I hope that I will be able to post the question correctly. Please tell me if there is any problem.</p>

<p>Now, here is my question: I would like to fill a database with a data, only if it doesn't already exist in it. I searched for this topic and I think I found correct the answer (you can read one example here: [<a href="http://stackoverflow.com/questions/19337029/insert-if-not-exists-statement-in-sqlite]">&quot;Insert if not exists&quot; statement in SQLite</a>) but I need to write these simple command line in python.. and that's my problem. (I anticipate that I am quite new in Python too)</p>

<p>So, here is what I did:</p>

<pre><code>    self.cur.execute("INSERT INTO ProSolut VALUES('a','b','c')")
    self.cur.execute("SELECT * FROM ProSolut")
    self.cur.execute("WHERE NOT EXISTS (SELECT * FROM ProSolut WHERE VALUES = ('a','b','c'))")
</code></pre>

<p>and here's the error:</p>

<pre><code>[ERROR] behavior.box :_safeCallOfUserMethod:125 _Behavior__lastUploadedChoregrapheBehaviorbehavior_1142022496:/ProSolutDB_11: Traceback (most recent call last):   File "/usr/lib/python2.7/site-packages/albehavior.py", line 113, in _safeCallOfUserMethod     func(functionArg)   File "&lt;string&gt;", line 45, in onInput_onStart OperationalError: near "WHERE": syntax error  
</code></pre>

<p>so basically I think there is some problem with the bracket "(" in the 3rd string. --> ("OperationalError: near "WHERE": syntax error")</p>

<p>I know that probably it's a stupid error.
If you can help me, I would really appreciate. </p>

<p>Thank you so much</p>

<hr>

<p>E.G.: I forgot to say that I am using the <strong>software Choregraphe</strong>, which uses the Python language to construct all the functional blocks.
That means that, even if the language is basically Python, sometimes the semantic is not perfectly the same.
I hope that this post can help someone in the future.</p>

<p>Assuming <code>a</code> is in a column called "Col1", <code>b</code> is in "Col2" and <code>c</code> is in "Col3", the following should check for the existence of such a row:</p>

<pre><code>self.cur.execute('SELECT * FROM ProSolut WHERE (Col1=? AND Col2=? AND Col3=?)', ('a', 'b', 'c'))
entry = self.cur.fetchone()

if entry is None:
    print 'No entry found'
else:
    print 'Entry found'
</code></pre>

<p>This selects all entries in <code>ProSolut</code> that match these values. <code>fetchone</code> then tries to grab a result of this query - if there are no such matches then it returns <code>None</code>.</p>

<p><strong>EDIT:</strong> In line with Barmar's comment, to make this insert the values, adapt to the following:</p>

<pre><code>self.cur.execute('SELECT * FROM ProSolut WHERE (Col1=? AND Col2=? AND Col3=?)', ('a', 'b', 'c'))
entry = self.cur.fetchone()

if entry is None:
    self.cur.execute('INSERT INTO ProSolut (Col1, Col2, Col3) VALUES (?,?,?)', ('a', 'b', 'c'))
    print 'New entry added'
else:
    print 'Entry found'
</code></pre>

<p>You'll need to make sure you <code>commit()</code> your changes too!</p>

python database sqlite sqlite3 insert