Lets assume, that we have some 2 tables in SQLite: TBL_PRODUCT
and TBL_PRODUCT_ALIASES
. Now, I want to query some data from joined two tables:
String sql = "SELECT " + TBL_PRODUCT + "." + C_PROD_PKEY_ID + ", " + TBL_PRODUCT_ALIASES + "." + C_PROD_ALIASES_PKEY_ID +
" FROM " + TBL_PRODUCT + " LEFT JOIN " + TBL_PRODUCT_ALIASES + " ON " + TBL_PRODUCT + "." + C_PROD_PKEY_ID + " = " + TBL_PRODUCT_ALIASES + "." + C_PROD_ALIASES_PKEY_ID +
" WHERE " + C_PROD_SERVER_ID + " = ? LIMIT 1";
Cursor cursor = SQLiteDataHelper.getInstance().rawQuery(sql, new String[] {"" + js_CartProduct.getLong("prod_id")});
Thats works great without any problem. And then I want to acquire some data from the returned cursor:
if (cursor.getCount() > 0) {
cursor.moveToFirst();
Long prodId = cursor.getLong(cursor.getColumnIndexOrThrow(TBL_PRODUCT + "." + C_PROD_PKEY_ID));
//... Some other code
}
さて、ここに問題があります:C_PROD_PKEY_ID
そしてC_PROD_ALIASES_PKEY_ID
、現実の世界では同じ Strings:"_id"
です。そして、結果getLong()
が の必要な列からではなく long を返すのでcursor
。
必要な列に正しくアドレス指定するにはどうすればよいですか? AS
SQLクエリでOR/ANDを使用する以外に、cursor.getLong(0)
の代わりに明確な数を使用する方法はありcursor.getLong(cursor.getColumnIndexOrThrow(TBL_PRODUCT + "." + C_PROD_PKEY_ID))
ますか?
アップデート:
cursor.getColumnNames()
これを返します: [_id, _id]
.