如何从数据库中获取不同的值?

发布于 2024-12-21 23:42:06 字数 824 浏览 1 评论 0原文

我想以这种格式从数据库中获取值

1 Audi
3 Nissan

但我现在就这样了。我不需要在输出中包含 _id 。但我无法消除,因为当我删除它时它会抛出 NullPointerException 。

id  MAKE
========= 
1   Audi  
2   Audi  
3   Nissan  
4   Audi

这是我在“

cursor = db.rawQuery(
    "SELECT DISTINCT _id,MAKE FROM "+SQLiteAdapter.MYDATABASE_TABLE+
    " WHERE MAKE || ' ' || MODEL LIKE ?", new String[]{"%" + "" + "%"}
);

是否需要完全更改序列以获得输出或对上述代码进行更改”中使用的查询。谢谢。

我的代码

cursor = db.rawQuery("SELECT DISTINCT SERIES FROM "+SQLiteAdapter.MYDATABASE_TABLE+" WHERE YEAR=2012 AND MAKE='Audi' AND MODEL=?", new String[]{"A6"});

adapter = new SimpleCursorAdapter(this, R.layout.employee_list_item, cursor, new String[] {"series"}, new int[] {R.id.firstName});

employeeList.setAdapter(adapter);

I would like to get values from the database in this format

1 Audi
3 Nissan

But I am getting like this as of now. I don't need to get to include the _id in the output.But I couldn't eliminate as it throws a NullPointerException when I remove it.

id  MAKE
========= 
1   Audi  
2   Audi  
3   Nissan  
4   Audi

this is my query I have used in

cursor = db.rawQuery(
    "SELECT DISTINCT _id,MAKE FROM "+SQLiteAdapter.MYDATABASE_TABLE+
    " WHERE MAKE || ' ' || MODEL LIKE ?", new String[]{"%" + "" + "%"}
);

Do have I to change the sequence entirely to get the output or make changes on the above code.Thanks.

my code

cursor = db.rawQuery("SELECT DISTINCT SERIES FROM "+SQLiteAdapter.MYDATABASE_TABLE+" WHERE YEAR=2012 AND MAKE='Audi' AND MODEL=?", new String[]{"A6"});

adapter = new SimpleCursorAdapter(this, R.layout.employee_list_item, cursor, new String[] {"series"}, new int[] {R.id.firstName});

employeeList.setAdapter(adapter);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

书间行客 2024-12-28 23:42:06

尝试:

select min(id), min(make) from cars group by make

try:

select min(id), min(make) from cars group by make
没︽人懂的悲伤 2024-12-28 23:42:06

您可以使用 SQLiteQueryBuilder 的 buildQueryString 方法:

buildQueryString (boolean distinct, String tables, String[] columns, String where, String groupBy, String having, String orderBy, String limit)

you can use SQLiteQueryBuilder's buildQueryString method:

buildQueryString (boolean distinct, String tables, String[] columns, String where, String groupBy, String having, String orderBy, String limit)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文