0

Android sqlite commands are like db.delete(table, whereClause, whereArgs); whereClause is a string such as "column1 = ? and column2 = ?" whereArgs is a string array that holds the values that will be substituted in the whereClause. Is there an easy way for me to get the decoded string from this (for logging purposes). String.format seems close, but not quite. This is what I am aiming for:

 Log.e(MY_DEBUG_TAG, 
     "Error executing Delete(" + table + ", " + 
            String.format(whereClause, whereArgs + ")");
4
  • 1
    Why would you want to make yourself vulnerable to SQL injection attacks (or work out all the quoting required) rather than using a parameterized query in the normal way? Commented Jun 1, 2012 at 20:06
  • I am using the parameterized query. It's just that when I have a problem, I want to read the logs and see what it was that I was trying to do. string[].ToString() doesn't give any useful information about the strings in the array, so I want to know how to turn the arguments into an easily read string. Commented Jun 1, 2012 at 20:13
  • I do apologise - I missed the fact that this is just for logging. I would personally just log the parameterized SQL and log the parameters separately. Commented Jun 1, 2012 at 20:18
  • What I am asking is that when there is an error, the stack trace shows the complied query. It looks like a reqular SQL query, in a nice human-readable form. I would like to log the query with the same format. But converting the whereclause and whereargs into a friendly string seems tedious. I was hoping there was an easy way. String.format comes to mind, but it isn't quite right. Commented Jun 1, 2012 at 20:23

1 Answer 1

2

If only for logging purposes you may try:

 Log.e(MY_DEBUG_TAG, 
     "Error executing Delete(" + table + ", " + 
            String.format(whereClause.replaceAll("\\?", "%s"), (Object[])whereArgs) + ")");
Sign up to request clarification or add additional context in comments.

1 Comment

I get this warning "The argument of type String[] should explicitly be cast to Object[] for the invocation of the varargs method format(String, Object...) from type String. It could alternatively be cast to Object for a varargs invocation" How do I make that go away?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.