The reason why you bind your values to the query is to prevent an SQL-injection attack.
Basically, you send your query (including placeholders) to your database and say "My next query is going to be of this form and none other!". When an attacker then injects a different query-string (through a form-field, for example) the database says "Hey, that's not the query you said you would send!" and throws an error at you.
Since commands (which can be injected into your actual query as show in the linked article) are strings, the string-datatype is the more "dangerous" one.
If a user tries to inject some code into your field which should only take numbers and you try to cast/parse the input to an integer (before putting the value into your query), you'll get an exception right away. With a string, there is no such kind security. Therefor, they have to be escaped probably.
That might be the reason that the bind values are all interpreted as strings.
The above is bogus! It doesn't matter if the arguments you bind are strings or integers, they're all equally dangerous. Also, pre-checking your values in-code results in a lot of boilerplate code, is error-prone and unflexible!
To prevent your application from SQL-Injections and also speed up multiple database writing operations (using the same query with different values), you should use "prepared statements". The correct class for writing to the database in the Android SDK is SQLiteStatement
.
To create a prepared statement, you use the compileStatement()
-method of your SQLiteDatabase
-object and bind the corresponding values (which will be replaced with the ?
-marks in your query) using the correct bindXX()
-method (which are inherited from SQLiteProgram
):
SQLiteDatabase db = dbHelper.getWritableDatabase();
SQLiteStatement stmt = db.compileStatement("INSERT INTO SomeTable (name, age) values (?,?)");
// Careful! The index begins at 1, not 0 !!
stmt.bindString(1, "Jon");
stmt.bindLong(2, 48L);
stmt.execute();
// Also important! Clean up after yourself.
stmt.close();
Example taken from this older question: How do I use prepared statements in SQlite in Android?
Sadly, SQLiteStatement
does not have an overload that returns a Cursor
, so you can't use it for SELECT
-statements. For those, you can use the rawQuery(String, String[])
-method of SQLiteDatabase
:
int number = getNumberFromUser();
String[] arguments = new String[]{String.valueOf(number)};
db.rawQuery("SELECT * FROM TABLE_A WHERE IFNULL(COLUMN_A, 0) >= ?", arguments);
Note that the rawQuery()
-method takes a String-array for the argument values. This actually doesn't matter, SQLite will automatically convert to the correct type. As long as the string-representations equal what you would expect in an SQL query, you're fine.