3

If I hardcode a number into the where section I get the rows I'm expecting back

Cursor cursor = db.query( tableName, 
new String[] {colID, colString, colNumber },  
colNumber + "=14", 
null, 
null, null, null, null);

If I instead put the number in as a parameter, I don't get any rows back.

Cursor cursor = db.query( tableName, 
new String[] {colID, colString, colNumber },  
colNumber + "=?", 
new String[] { String.valueOf(14) }, 
null, null, null, null);

What am I doing wrong in the second case?

Create table statements:

db.execSQL("CREATE TABLE "
        + otherTableName + " ("
        + otherTableID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
        + otherTableString + " TEXT"
        + ");");

db.execSQL("CREATE TABLE "
        + tableName + " ("
        + colID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
        + colString + " TEXT, "
        + colNumber + " REFERENCES "
        + otherTableName + "(" + otherTableID
            + "));");
3
  • Odd. Normally SQLite doesn't care about data types, but how did you define colNumber? Commented Apr 2, 2013 at 15:59
  • colNumber references an ID in a second table. Commented Apr 2, 2013 at 16:02
  • 1
    Please show your "CREATE TABLE" statement. Commented Apr 2, 2013 at 16:03

1 Answer 1

1

I think that problem is your second DML statement, more accurate your declaration of foreign key. I think that it should be:

db.execSQL("CREATE TABLE "
        + tableName + " ("
        + colID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
        + colString + " TEXT, "
        + colNumber + " INTEGER, "
        + " FOREIGN KEY (" + colNumber + ") REFERENCES "
        + otherTableName + "(" + otherTableID
            + "));");

I cannot explain myself other reason why it's not working. Your statements are equal except one uses hardcored value and second placeholder. Probably you tried parametrized statement on tableName table where i think you declared FK incorrectly and this was reason why you got any rows.

Placeholders work based on SQLite's type affinity

If you defined column as TEXT, placeholder is replaced and value is compared as col = 'value'. If you defined column as INTEGER, placeholder is replaced and value is compared as col = value. But in your second DML statement, you didn't specified column's datatype, only that it references to some column somewhere.

Sign up to request clarification or add additional context in comments.

2 Comments

Actually, parameters are always replaced as col = 'value' (i.e., as strings; this is a silly restriction in Android's DB interface), but SQLite's type affinity will then compare it correctly for numeric-typed columns.
@CL. thanks for correction. Good to know. i was wrong with that.

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.