Menu

#15 SQLiteStatement.columnValue returns a Long when an Integer is expected

Delivered
nobody
None
Medium
Defect
201
2010-08-23
2010-08-19
Anonymous
No

Originally created by: olivier....@free.fr
Originally owned by: ser...@gmail.com

When you use columnValue method to retrieve an Integer, you obtain a Long. Here is a small test case:

SQLiteStatement st = cnx.prepare("select 1;");
st.step();
Assert.assertEquals(Integer.class, st.columnValue(0).getClass());

The problem comes from SQLiteStatement.java, line 1044:

return value == ((long) ((int) value)) ? Integer.valueOf((int) value) : Long.valueOf(value);

This instruction works fine unless you mix Integer, Byte, Short, Long, Float or Double value as the result of the inline condition. For example, the following inline condition:

<cond> ? new Integer(1) : new String()

has a type of Object: the common base class of Integer and String. But the following condition:

<cond> ? new Integer(1) : new Long(1)

has a type of Long. This is because a promotion occurs on Integer and it becomes a Long.

you must use standard condition like:

if (value == (int)value) {
  return Integer.valueOf(value);
}
return Long.valueOf(value);

It works fine for me.

Discussion

  • Anonymous

    Anonymous - 2010-08-19

    Originally posted by: ser...@gmail.com

    Thx!

    Owner: sereda
    Status: Accepted

     
  • Anonymous

    Anonymous - 2010-08-23

    Originally posted by: ser...@gmail.com

    (No comment was entered for this change.)

    Labels: FixVersion-201
    Status: Delivered

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.