2017-05-03 61 views
1

我在Android中查询我的SQLITE数据库时发生问题。我有一个名为“重置”的表格,其中包含一些值。目前我只有一个条目。当SQLiteDatabase.rawQuery()正常工作时SQLiteDatabase.query()出现问题

reset_timestamp | interval 
1479442048  | 5 

这是我试图执行的查询。但是,当我调用cursor.getCount()时,它返回零结果。我想执行的查询是:

SELECT reset_timestamp FROM resets WHERE (reset_timestamp=1479442048); 

我真的不想使用rawQuery()。我想使用query()。这是我的查询语句。

SQLiteDatabase db = new PowerDbHelper(this).getWritableDatabase(); 
String[] resetsQueryColumns = {"reset_timestamp"}; 
String[] resetsQuerySelectArgs = {"1479442048"}; 

Cursor cursor = db.query("resets", resetsQueryColumns, "reset_timestamp=?", 
       resetsQuerySelectArgs, null, null, null); 

但是,getCount()返回0。另一方面,这工作正常,并返回我的结果

cursor = db.rawQuery("select reset_timestamp from resets where (reset_timestamp=1479442048)", null); 

和getCount()返回1,我想要的。把报价放在'?'上给我

java.lang.IllegalArgumentException: Cannot bind argument at index 1 because the index is out of range 

我在做什么错了查询()?

+0

当我参考getCount(),我引用cursor.getCount()。 – kernel

回答

0

"1479442048"是一个字符串(你甚至在它前面写过String)。一个字符串和一个数字是不相等的。

query功能仅支持字符串参数,所以你必须将字符串转换回数字:

query(..., "reset_timestamp=CAST(? AS INT)", ...); 

或者,直接将数到查询:

query(..., "reset_timestamp=1479442048", null, ...); 
相关问题