2

I see in examples when executeSql takes several parameters, we use question marks:

db.transaction(function(tx){
var addedOn = new Date();
tx.executeSql("INSERT INTO todo(todo, added_on) VALUES (?,?)",
    [todoText, addedOn],
    html5rocks.webdb.onSuccess,
    html5rocks.webdb.onError);
});

Can executeSql take named parameters?

Makoto
  • 104,088
  • 27
  • 192
  • 230
Phillip Senn
  • 46,771
  • 90
  • 257
  • 373

1 Answers1

5

From that same page:

On November 18, 2010, the W3C announced that Web SQL database is a deprecated specification. This is a recommendation for web developers to no longer use the technology as effectively the spec will receive no new updates and browser vendors aren't encouraged to support this technology.

Also see the big warning at http://www.w3.org/TR/webdatabase

But to answer your question: As far as I know: no. You could write a 'wrapper' though, replacing your own placeholders with actual values, but I think it won't be worth bothering.

Another shot in the dark would by trying to use :foo as placeholder. AFAIK most browsers use(d) SQLite for their webdatabase implementation and, from the top of my head having looked it up, SQLite supports named parameters in the :parametername-form several forms. No idea how you would actually supply values, though I imagine something like [{'foo': 'bar'}, {'baz': 'bat'}]


EDIT
I have tried many permutations on your example like this:

var p = {};
p.aa = todoText;
p.bb = addedOn;

tx.executeSql("INSERT INTO todo(todo, added_on) VALUES (:aa, :bb)",
    p,
    ...
});

Or:

tx.executeSql("INSERT INTO todo(todo, added_on) VALUES (:aa, :bb)",
    {"aa": todoText, "bb": addedOn},
    ...
});

Or:

tx.executeSql("INSERT INTO todo(todo, added_on) VALUES (@aa, @bb)",
    {"aa": todoText, "bb": addedOn},
    ...
});

Or:

tx.executeSql("INSERT INTO todo(todo, added_on) VALUES (?aa, ?bb)",
    {"aa": todoText, "bb": addedOn},
    ...
});

...and many more in Chrome 25; none of them seem to indicate named parameters are supported. All I can get to work is a simple nth position value to map to an nth parameter position (or index if you will).

I'm not saying there is absolutely no way, maybe I just couldn't find it or guess the correct way to use it, but I think you're out of luck. And, again as mentioned in the comments, even should I or someone else get it to work I wouldn't rely on it (because: undocumented, as well as deprecated anyway) and it will probably fail in many (most?) other browsers. To be honest: trying to get this to work is a waste of your time.

Community
  • 1
  • 1
RobIII
  • 8,488
  • 2
  • 43
  • 93
  • Why do I get the feeling that the walls are caving in around me? – Phillip Senn Mar 26 '13 at 18:31
  • But what about http://caniuse.com/#search=indexdb It says that it's not supported in Safari (meaning: iPad) – Phillip Senn Mar 26 '13 at 18:33
  • Compare it to http://caniuse.com/#search=database and draw your own conclusions... – RobIII Mar 26 '13 at 18:41
  • I was able to use ?1,?2,?3 but I haven't been able to figure out how to use named parameters yet. – Phillip Senn Mar 27 '13 at 16:52
  • Oh, and I watch some videos on IndexedDB last night. So thanks for pointing me in that direction. Man, I've got a lot of work to do. – Phillip Senn Mar 27 '13 at 16:54
  • Happy to help. If this answer solved your issue, please mark it as accepted. (See http://goo.gl/uA0MZ) – RobIII Mar 27 '13 at 17:12
  • I've only gotten as far as using counters for my parameters, such as: ?1, ?2, ?3. I haven't gotten named parameters to work yet. – Phillip Senn Mar 27 '13 at 17:38
  • I would need to test this, but it's safe to assume they won't work. **If** I (or someone) happens to find a way it will probably fail in other browsers than the one you're testing in (these browser might have a different, if any at all, implementation for named parameters). I wouldn't get my hopes up, nor rely on undefined behaviour and stick with documented features. I guess the actual answer to your question "Can executeSql take named parameters?" would have to be: NO (unless... and a bunch of footnotes for browser specific implementations, *if* any at all) – RobIII Mar 27 '13 at 17:44
  • The "[official documentation](http://www.w3.org/TR/webdatabase/#preprocess-the-sql-statement)" states: `Bind each ? placeholder with the value of the argument in the arguments array with the same position. (So the first ? placeholder gets bound to the first value in the arguments array, and generally the nth ? placeholder gets bound to the nth value in the arguments array.)`. There is also no mention of 'named parameters' anywhere. So, *if* you'd get it to work it would be, as pointed out earlier, be browser specific and not something to rely on. – RobIII Mar 27 '13 at 18:02
  • Interesting [comment on this page](http://www.raymondcamden.com/index.cfm/2010/7/17/Show-me-a-better-way-Inserting-datetime-strings-into-SQLite-via-HTML-and-Adobe-AIR). – Phillip Senn Mar 28 '13 at 21:15
  • ...except that it uses Adobe's [AIR](http://en.wikipedia.org/wiki/Adobe_Integrated_Runtime)'s SQLite implementation. That's not browser native but a workaround using/via AIR. Your question was specifically targeted at WebDB's executeSql method which, as far as I can see, doesn't support named parameters the way you want them to be supported. Using the "AIR workaround" would only introduce another dependency but, granted, it should work cross-platform after that. You have to weigh the pro's and con's yourself, I can't do that for you, neither can anyone else. – RobIII Mar 28 '13 at 22:06
  • ...and if you ask me, when the only added value is support for named parameters it isn't worth introducing the overhead, requirements and dependencies of AIR. But, again, ofcourse it's all up to you. – RobIII Mar 28 '13 at 22:08