Question

I cannot get around this Exception. Am i missing something obvious?

Stack Trace

12-05 20:07:43.129: E/AndroidRuntime(4154): FATAL EXCEPTION: Thread-1470
12-05 20:07:43.129: E/AndroidRuntime(4154): java.lang.StringIndexOutOfBoundsException: length=0; regionStart=0; regionLength=6
12-05 20:07:43.129: E/AndroidRuntime(4154):     at java.lang.String.startEndAndLength(String.java:593)
12-05 20:07:43.129: E/AndroidRuntime(4154):     at java.lang.String.substring(String.java:1474)
12-05 20:07:43.129: E/AndroidRuntime(4154):     at net.sqlcipher.database.SQLiteProgram.<init>(SQLiteProgram.java:69)
12-05 20:07:43.129: E/AndroidRuntime(4154):     at net.sqlcipher.database.SQLiteQuery.<init>(SQLiteQuery.java:49)
12-05 20:07:43.129: E/AndroidRuntime(4154):     at net.sqlcipher.database.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42)
12-05 20:07:43.129: E/AndroidRuntime(4154):     at net.sqlcipher.database.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1420)
12-05 20:07:43.129: E/AndroidRuntime(4154):     at net.sqlcipher.database.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1389)

Query used

Query= SELECT column1,max(datetime(creation_timestamp)) FROM table_name where dfs_id=? GROUP BY column2

(column1, column2, table_name etc are placeholders - not the actual names)

Argument passed = "a08eff29ee6a71dd_1381146091100_0002"


Note: I am using SQLCipher(A wrapper around SQLCipher), which should not be relevant to this exception.

Was it helpful?

Solution

The code that causes the StringIndexOutOfBoundsException is the following.

/* package */ SQLiteProgram(SQLiteDatabase db, String sql) {
    mDatabase = db;
    mSql = sql.trim();
    db.acquireReference();
    db.addSQLiteClosable(this);
    this.nHandle = db.mNativeHandle;

    // only cache CRUD statements
    String prefixSql = mSql.substring(0, 6); // <<<< HERE

The only reason why you can get that exception is a String with less than 6 characters. (Your's is length=0; i.e. empty after the trim() a few lines above)

There is no indication where or how exactly you manage to put an empty string in there. It comes from a SQLiteDatabase.rawQuery but that could have been called from other query methods. Look further down the stacktrace and look through your code where you are possibly passing an empty string to a query method.


Your query also seems to break the syntax of the where clause: If there is an AND it would have to be

WHERE {condition} AND {another condition expected here} GROUP BY ...

You have either forgot to add another expression or you have an AND too much.

And table is a reserved keyword. To use those as names you'll have to quote them. SQLite supports quotation marks ("), backticks (`) and brackets ([]).

OTHER TIPS

I think the problem is that you are using table for the name of your table. TABLE is a command in SQL; it should not be used for table names. Since SQL is not case-sensitive TABLE is the same as table. Just change the name of your table to something else.

The stack strace shows that you give an empty query string to the rawQuery function.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top