Android:sqlite:光标:getColumnIndex
我在规范化的 sqlite 数据库上有一个相当复杂的查询(多个联接)。查询执行 SELECT * 来启用一些自动属性选择逻辑(因此我无法消除“*”)
我遇到的问题是我的结果集包含具有相同属性名称的多个列。例如,查询中每个表共有的一个属性是“_id”。当我去调用 "cursor.getColumnIndex("_id")"
时,返回的值始终是结果集列列表中最后一个 "_id"
属性的索引(即不是我想要的)。我希望能够使用我的 SQL 别名前缀,例如 cursor.getColumnIndex("A._id")
,但这不起作用。
问题
- 看来
cursor.getColumnIndex(AttributeName)
返回最后一个“AttributeName”的索引。谁能证实这一点吗? - 另外,关于如何返回带有“AttributeName”的第一个属性的索引有什么建议吗?或者更好的是第 X 个属性具有“AttributeName”?
I've got a fairly complicated query (multiple joins) on a normalized sqlite database. The query does a SELECT *
to enable some automated attribute selection logic (so I can't eliminate the "*")
The problem I am having is that my result set contains multiple columns with the same attribute name. For example, one attribute common to each table in the query is "_id". When I go to call "cursor.getColumnIndex("_id")"
the value returned is always the index of the last "_id"
attribute in the result set column list (i.e. not the one I want). I'd love to be able to use my SQL alias prefixes like cursor.getColumnIndex("A._id")
but that is not working.
QUESTIONs
- It appears that
cursor.getColumnIndex(AttributeName)
returns the index of the last "AttributeName". Can anyone confirm this? - Also, any suggestions on how return the index of the 1st attribute with "AttributeName"? or better the Xth attribute having "AttributeName"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以这样做:
这意味着结果中每个表的
_id
字段将出现两次,但两列之一将具有唯一的名称,您可以通过该名称找到它。You can do this:
This means the
_id
field will appear twice for each table in your results, but one of the two columns will have a unique name which should enable you to find it.不幸的是,文档没有没有提及您需要做什么,所以我假设它无法完成。
然而,你说
你所说的“自动属性选择逻辑”是什么?为什么你需要这个?
Unfortunately the documentation doesn't mention anything about what you need to do, so I am assuming it cannot be done.
However, you say
What is this 'automated attribute selection logic' you speak of? Why do you require this?
一个顺序的解决方案是:
然后执行相同的操作:
这就是 MS-Access 所做的。您可以创建一个查询,然后查看生成的 SQL 代码(只需转到“查看”菜单并从查询设计窗口中选择“SQL 视图”)
An oder solution is:
and then do the same with:
This is what MS-Access does. You can create a query and then see the generated SQL code (simply going to 'view' menu and selecting 'SQL view' from your query dessign window)