iangkub / sqlite4java

Automatically exported from code.google.com/p/sqlite4java
0 stars 0 forks source link

Add convenience methods `bind(String name, [types] value)` to SQLiteStatement class #53

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
To make code for binding named parameters to prepared SQLiteStatements simpler 
it'd be nice to have methods that lookup the index for you if you pass a string 
instead of an int to the first argument of  bind(). For example, instead of 
needing things similar to:

SQLiteStatement st = db.prepare("SELECT * FROM a WHERE col1 = :val1 AND col2 = 
:val2");
int i = st.getBindParameterIndex(":val1");
st.bind(i,"abc"); //finding index beforehand
st.bind(st.getBindParameterIndex(":val2"),"xyz"); //compacted

The code would look like:

SQLiteStatement st = db.prepare("SELECT * FROM a WHERE col1 = :val1 AND col2 = 
:val2");
st.bind(":val1","abc");
st.bind(":val2","xyz");

The code for the additional methods would be really simple and quick to add. 
Just need to add something like:

public SQLiteStatement bind(String name, String value) throws SQLiteException {
  bind(st.getBindParameterIndex(name),value);
}

For each value type that can be binded (double, int, long, String, etc).

Original issue reported on code.google.com by ymango...@c-2iinc.com on 1 Apr 2013 at 10:55

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Correction, new methods would be similar to:

public SQLiteStatement bind(String name, String value) throws SQLiteException {
  bind(getBindParameterIndex(name),value);
  return this;
}

Original comment by ymango...@c-2iinc.com on 1 Apr 2013 at 11:00

GoogleCodeExporter commented 9 years ago
Thanks for the suggestion! 

Original comment by ser...@gmail.com on 2 Apr 2013 at 8:37

GoogleCodeExporter commented 9 years ago

Original comment by linkogen...@gmail.com on 22 Aug 2013 at 8:09