Binding SQLite Parameters directly by Name Binding SQLite Parameters directly by Name objective-c objective-c

Binding SQLite Parameters directly by Name


  1. There are; it's the sqlite3_bind_parameter_index() function you mentioned that you use to turn a parameter name into an index, which you can then use with the sqlite3_bind_*() functions. However, there's no sqlite3_bind_*_by_name() function or anything like that. This is to help prevent API bloat. The popular Flying Meat Database sqlite wrapper has support for named parameters in one of its branches, if you're interested in seeing how it's used.

    If you think about what it would take to implement full named parameter binding methods, consider the current list of bind functions:

    int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*));int sqlite3_bind_double(sqlite3_stmt*, int, double);int sqlite3_bind_int(sqlite3_stmt*, int, int);int sqlite3_bind_int64(sqlite3_stmt*, int, sqlite3_int64);int sqlite3_bind_null(sqlite3_stmt*, int);int sqlite3_bind_text(sqlite3_stmt*, int, const char*, int n, void(*)(void*));int sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int, void(*)(void*));int sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*);int sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n);

    If we wanted to add explicit support for named parameters, that list would double in length to include:

    int sqlite3_bind_name_blob(sqlite3_stmt*, const char*, const void*, int n, void(*)(void*));int sqlite3_bind_name_double(sqlite3_stmt*, const char*, double);int sqlite3_bind_name_int(sqlite3_stmt*, const char*, int);int sqlite3_bind_name_int64(sqlite3_stmt*, const char*, sqlite3_int64);int sqlite3_bind_name_null(sqlite3_stmt*, const char*);int sqlite3_bind_name_text(sqlite3_stmt*, const char*, const char*, int n, void(*)(void*));int sqlite3_bind_name_text16(sqlite3_stmt*, const char*, const void*, int, void(*)(void*));int sqlite3_bind_name_value(sqlite3_stmt*, const char*, const sqlite3_value*);int sqlite3_bind_name_zeroblob(sqlite3_stmt*, const char*, int n);

    Twice as many functions means a lot more time spent maintaining API, ensuring backwards-compatibility, etc etc. However, by simply introducing the sqlite3_bind_parameter_index(), they were able to add complete support for named parameters with only a single function. This means that if they ever decide to support new bind types (maybe sqlite3_bind_int128?), they only have to add a single function, and not two.

  2. As for why no one seems to use it... I can't give any sort of definitive answer with conducting a survey. My guess would be that it's a bit more natural to refer to parameters sequentially, in which case named parameters aren't that useful. Named parameters only seem to be useful if you need to refer to parameters out of order.