OK, with the one-time <span style="font-family:courier new,monospace"><span style="background-color:rgb(192,192,192)">CREATE UNIQUE INDEX on &#39;table&#39; (</span></span><span style="background-color:rgb(153,153,153)"><span style="background-color:rgb(192,192,192)"><span style="font-family:courier new,monospace"><span style="background-color:rgb(192,192,192)">hostname,realm,data_key);</span><span style="background-color:rgb(255,255,255)"> </span></span></span></span><span style="background-color:rgb(255,255,255)">s</span>tmt in place at the schema-level, a portable work-a-like of INSERT OR REPLACE stmt is the UPDATE-INSERT (i.e. &quot;UPSERT&quot; lol) stmt pair that follows,<br>
<br><span style="background-color:rgb(153,153,153)"><span style="background-color:rgb(192,192,192)"><span style="font-family:courier new,monospace">BEGIN TRANSACTION;                                                                                                                 <br>
UPDATE &#39;table&#39; SET data_value=&#39;value_data&#39; <span style="background-color:rgb(255,255,102)">WHERE hostname=&#39;value_hostname&#39; AND realm=&#39;value_realm&#39; AND data_key=&#39;value_data_key&#39;</span> ;<br>
INSERT INTO &#39;table&#39; (hostname,realm,data_key,data_value) SELECT &#39;value_hostname&#39;, &#39;value_realm&#39;, &#39;value_data_key&#39;, &#39;value_data_value&#39;<br>   WHERE NOT EXISTS (SELECT 1 FROM &#39;table&#39; <span style="background-color:rgb(255,255,0)">WHERE hostname=&#39;value_hostname&#39; AND realm=&#39;value_realm&#39; AND data_key=&#39;value_data_key&#39;</span> );<br>
COMMIT;                                                                                                                             </span></span><br></span><br>The two WHERE clauses must be identical and the variables should be in the same order as the unique compound index is ordered.<br>
The UPDATE fails quietly and benignly if the record does NOT already exist and succeeds if it does.<br>The INSERT ... WHERE NOT EXISTS stmt succeeds if the record does NOT already exist fails quietly and benignly if it does. <br>
The BEGIN and COMMIT aren&#39;t typically necessary but they&#39;re over-safe just in case the same record is attempted by another thread.<br><br>I think this get&#39;s it done.<br><br>:-)<br><br><br><div class="gmail_quote">
On 18 November 2012 06:25, S. Scott <span dir="ltr">&lt;<a href="mailto:8f27e956@gmail.com" target="_blank">8f27e956@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div bgcolor="#FFFFFF"><div><div>The IF NOT EXITS term is easily omitted.  It&#39;s a helper term for batch scripted SQL to prevent an error being thrown in the case of the index already exists as in the case where the make schema script is run more than once.  Easily omitted and, perhaps more properly, replaced with a DROP INDEX stmt and CREATE UNIQUE INDEX stmt pair.</div>

<div><br></div><div>Yeah, i checked further, the OR REPLACE modifier is a little more Hit/miss.  The capability is there typically present, but the syntax varies -- e.g sqlite3 ON REPLACE is mySQL&#39;s ON DUPLICATE KEY UPDATE.</div>

<div><br></div><div>Darn shame sqlite3 doesn&#39;t support stored procedures &#39;cause then it&#39;s more easily abstracted for portability up the SQL server engine curve.</div><div><div class="h5"><div><br>On 2012-11-18, at 3:53, Gerald Weber &lt;<a href="mailto:gerald.weber@besharp.at" target="_blank">gerald.weber@besharp.at</a>&gt; wrote:<br>

<br></div><div></div><blockquote type="cite"><div><div><p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,&quot;sans-serif&quot;;color:#1f497d" lang="EN-US">AFAIK, „if not exists“ and „or replace“ is not ansi sql  syntax, so you will get in trouble if </span></p>

<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,&quot;sans-serif&quot;;color:#1f497d" lang="EN-US">someone uses odbc to a db that doesn’t support non standard syntax.</span></p><p class="MsoNormal">

<span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,&quot;sans-serif&quot;;color:#1f497d" lang="EN-US"> </span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,&quot;sans-serif&quot;;color:#1f497d" lang="EN-US">The insert or replace could be replaced with the merge statement.</span></p>

<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,&quot;sans-serif&quot;;color:#1f497d" lang="EN-US"> </span></p><p class="MsoNormal"><b><span style="font-size:10.0pt;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;" lang="DE">Von:</span></b><span style="font-size:10.0pt;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;" lang="DE"> <a href="mailto:freeswitch-users-bounces@lists.freeswitch.org" target="_blank">freeswitch-users-bounces@lists.freeswitch.org</a> [mailto:<a href="mailto:freeswitch-users-bounces@lists.freeswitch.org" target="_blank">freeswitch-users-bounces@lists.freeswitch.org</a>] <b>Im Auftrag von </b>Scott<br>

<b>Gesendet:</b> Sonntag, 18. November 2012 02:42<br><b>An:</b> FreeSWITCH Users Help<br><b>Betreff:</b> [Freeswitch-users] Suggestion to harmonize &#39;hash&#39; &amp; &#39;db&#39; insert method</span></p><p class="MsoNormal">

 </p><p class="MsoNormal" style="margin-bottom:12.0pt">Are there reasons why the function &#39;hash&#39; (non-persistent storage) and function &#39;db&#39; (persistent storage) share a look-a-like user interface (e.g. select/insert/delete) but do not work-a-like.  In particular, in &#39;hash&#39;, insert overwrites an identical realm/data_key pair whereas &#39;db&#39; , in so far as I can tell, just blindly adds, and adds, adds, the insert(s).  However, the &#39;db&#39; select method returns one record only even if the db has accumulated many realm/data_key records, including data_value duplicates.  <br>

<br>NOTWITHSTANDING reasons-unknow-to-me, a remedy to harmonize the non-persistent &#39;hash&#39; with the persistent &#39;db&#39;  I *think* is straight forward ...<br><br>(1) At the call_limit.db schema-level, a one-time create UNIQUE COMPOUND index, as follows,<br>

<br>CREATE UNIQUE INDEX IF NOT EXISTS &#39;idx_db_data_HostRealmDK&#39; ON &#39;db_data&#39; (&#39;hostname&#39;,&#39;realm&#39;,&#39;data_key&#39;);<br><br>(1) At the c-language embedded SQL string-level,<br><br>change the existing INSERT to be the following,<br>

<br>INSERT OR REPLACE INTO db_data (hostname,realm,data_key,data) VALUES (%s,%s,%s,%s);<br><br>The index will help speed UP the reads.  Neither change is &quot;exotic&quot; and should be portable across standard sql implementations.<br>

<br>;-) ... thoughts ?<br><br>Thanks,<br><br><br></p></div></div></blockquote></div></div><blockquote type="cite"><div><span>_________________________________________________________________________</span><br><span>Professional FreeSWITCH Consulting Services:</span><br>

<span><a href="mailto:consulting@freeswitch.org" target="_blank">consulting@freeswitch.org</a></span><br><span><a href="http://www.freeswitchsolutions.com" target="_blank">http://www.freeswitchsolutions.com</a></span><br>
<span></span><br><span>FreeSWITCH-powered IP PBX: The CudaTel Communication Server</span><br>
<span><a href="http://www.cudatel.com" target="_blank">http://www.cudatel.com</a></span><br><span></span><br><span>Official FreeSWITCH Sites</span><br><span><a href="http://www.freeswitch.org" target="_blank">http://www.freeswitch.org</a></span><br>
<span><a href="http://wiki.freeswitch.org" target="_blank">http://wiki.freeswitch.org</a></span><br>
<span><a href="http://www.cluecon.com" target="_blank">http://www.cluecon.com</a></span><br><span></span><br><span>FreeSWITCH-users mailing list</span><br><span><a href="mailto:FreeSWITCH-users@lists.freeswitch.org" target="_blank">FreeSWITCH-users@lists.freeswitch.org</a></span><br>

<span><a href="http://lists.freeswitch.org/mailman/listinfo/freeswitch-users" target="_blank">http://lists.freeswitch.org/mailman/listinfo/freeswitch-users</a></span><br><span>UNSUBSCRIBE:<a href="http://lists.freeswitch.org/mailman/options/freeswitch-users" target="_blank">http://lists.freeswitch.org/mailman/options/freeswitch-users</a></span><br>

<span><a href="http://www.freeswitch.org" target="_blank">http://www.freeswitch.org</a></span><br></div></blockquote></div><div><span></span></div></div>
</blockquote></div><br>