<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div><blockquote type="cite"><div>4.11.3. Executing SQL statements</div><div><br class="webkit-block-placeholder"></div><div>[…]</div><div><br class="webkit-block-placeholder"></div><div>1. If the syntax of sqlStatement is not valid (except for the use of ? characters in the place of literals), or the statement uses features that are not supported (e.g. due to security reasons), then the the method must raise a SYNTAX_ERR exception and abort these steps.</div></blockquote><div><br class="webkit-block-placeholder"></div><div><div><font class="Apple-style-span" color="#000000"><span class="Apple-style-span" style="background-color: transparent;">I think step one needs more clarification on what an invalid statement is. In the current WebKit implementation a SYNTAX_ERR is being thrown for all queries that </span></font><span class="Apple-style-span" style="white-space: pre; "><font class="Apple-style-span" color="#000000"><span class="Apple-style-span" style="background-color: transparent;">sqlite3_prepare doesn't return SQLITE_OK. This means queries against tables that don't exist will throw a <span class="Apple-style-span" style="white-space: normal; ">SYNTAX_ERR, among other things that are perfectly well-formed statements.</span></span></font></span></div><div><span class="Apple-style-span" style="white-space: pre;"><br class="webkit-block-placeholder"></span></div><div><span class="Apple-style-span" style="white-space: pre;">Take this example and assume WebKitNotes doesn't exist:</span></div><div><span class="Apple-style-span" style="white-space: pre;"><br class="webkit-block-placeholder"></span></div><div><span class="Apple-style-span" style="white-space: pre;">db.executeSql("CREATE TABLE WebKitNotes (note TEXT, timestamp REAL)", [], function(result) {});
db.executeSql("INSERT INTO WebKitNotes VALUES (?, ?)", [note, timestamp], function(result) {});</span><span class="Apple-style-span" style="white-space: pre;">
</span></div><div><span class="Apple-style-span" style="white-space: pre;"><br class="webkit-block-placeholder"></span></div><div><span class="Apple-style-span" style="white-space: pre;">The first query will be queued for execution. It is a valid statement. The second query can come in before the first has been executed. Since the table hasn't been created yet, executeSql will throw a <span class="Apple-style-span" style="white-space: normal; ">SYNTAX_ERR because <span class="Apple-style-span" style="white-space: pre; ">sqlite3_prepare fails to find the table<span class="Apple-style-span" style="white-space: normal; ">.</span></span></span></span></div><div><br class="webkit-block-placeholder"></div><div>Now, run the code again and the create table will throw SYNTAX_ERR because the table already exists. Users could use CREATE TABLE IF NOT EXISTS, but SQLite has not always had this (for instance, the version shipped on Mac OS 10.4.)</div><div><br class="webkit-block-placeholder"></div><div>So a developer needs to write their code like this:</div><div><br class="webkit-block-placeholder"></div><div><div>try {</div><div><span class="Apple-tab-span" style="white-space:pre">      </span>db.executeSql("CREATE TABLE WebKitNotes (note TEXT, timestamp REAL)", [], function(result) {</div><div><span class="Apple-tab-span" style="white-space:pre">               </span>db.executeSql("INSERT INTO WebKitNotes VALUES (?, ?)", [note, timestamp], function(result) {});</div><div><span class="Apple-tab-span" style="white-space:pre">    </span>});</div><div>} catch(e) {</div><div><span class="Apple-tab-span" style="white-space:pre">       </span>db.executeSql("INSERT INTO WebKitNotes VALUES (?, ?)", [note, timestamp], function(result) {});</div><div>}</div><div><br class="webkit-block-placeholder"></div><div>I think SYNTAX_ERR should only be thrown for non well-formed queries (missing quotes, clauses, etc.), and if a table doesn't exist it should not throw. Once the whole queue is executed, that table might exist. If the table still doesn't exist the developer will have an errorCode and a meaningful error string instead of a generic SYNTAX_ERR with no context.</div></div><div><br></div><div></div><blockquote type="cite"></blockquote></div><div><span class="Apple-style-span" style="font-size: 10px; ">— Timothy Hatcher</span></div></div><div apple-content-edited="true"><span class="Apple-style-span" style="border-collapse: separate; border-spacing: 0px 0px; color: rgb(0, 0, 0); font-family: Verdana; font-size: 11px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; text-align: auto; -khtml-text-decorations-in-effect: none; text-indent: 0px; -apple-text-size-adjust: auto; text-transform: none; orphans: 2; white-space: normal; widows: 2; word-spacing: 0px; "><br class="Apple-interchange-newline"></span> </div><br></body></html>