使用SQLiteDatabase多个表的SQLite查询 [英] SQLite query from multiple tables using SQLiteDatabase

查看:154
本文介绍了使用SQLiteDatabase多个表的SQLite查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个表在我的数据库,例如:表1:ID(PK),数据1 表2:ID(PK),id_table1 (FK),数据2 。如何让这样的查询:

I have 2 tables in my database, for example: Table1: id (PK), data1 and Table2: id (PK), id_table1 (FK), data2. How can I make a query like that:

SELECT * FROM Table1, Table2 WHERE Table1.id = Table2.id_table1 
GROUP BY Table1.data1

我用 SQLiteDatabase 查询()方法。

Cursor mCursor = db.query(true, new String[] {"Table1","Table2"}, 
new String[] {"Table1.id","data1", "Table2.id", "id_table1", "data2"},
"Table1.id=Table2.id_table1", null, "Table1.data1", null,null,null);

但有与第二ARG一个问题 - 这是唯一可以使用字符串,而不是字符串[](如新的String [] {表1,表2} ),我应该怎么做才能让来自多个表的查询呀?

But there's a problem with the second arg - it's only possible to use String, not String[] (like new String[] {"Table1","Table2}). What should I do to make a query from multiple tables in that way?

推荐答案

试试这个:

Cursor mCursor = db.rawQuery("SELECT * FROM Table1, Table2 " +
                             "WHERE Table1.id = Table2.id_table1 " +
                             "GROUP BY Table1.data1", null);

这篇关于使用SQLiteDatabase多个表的SQLite查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆