简体   繁体   English

如何使用内容提供者查询方法在android中联接两个表?

[英]how to join two tables in android using content provider query method?

Im new to android I'm using content provider in my application, In my application I want to join two tables but I don't know how to do it, please help me find out solution 我是android的新手,我在应用程序中使用内容提供程序,在我的应用程序中,我想联接两个表,但是我不知道该怎么做,请帮助我找出解决方案

my tables: 我的桌子:

CREATE TABLE Bank_customers (customer_id varchar PRIMARY KEY ,
    customer_name varchar,
    customer_date_of_birth date,
    address varchar,
    mobile integer,
    email varchar);

CREATE TABLE Bank_accounts (account_number integer(11) PRIMARY KEY,
    customer_id varchar ,
    account_type text,
    account_open_date date,
    account_balance real,FOREIGN KEY(customer_id) REFERENCES Bank_customers(customer_id));

my query: **SELECT mobile,Bank_accounts.customer_id from Bank_accounts,Bank_customers WHERE Bank_customers.customer_id = Bank_accounts.customer_id and Bank_accounts.account_number = 13323;**

How I can implement above query using content provider class "query method" 如何使用内容提供者类“查询方法”来实现上述查询

Content providers are an abstraction on top of SQL, so you do not have direct access to the tables to join them. 内容提供者是SQL之上的抽象,因此您没有直接访问表以将它们联接的权限。

The content provider implementation must already provide the joined tables. 内容提供者实现必须已经提供了联接的表。

If you do not need to make the data accessible for other apps, you should probably avoid using a content provider, and use the database more directly. 如果不需要使数据可被其他应用程序访问,则应避免使用内容提供程序,而直接使用数据库。

This is easy to do. 这很容易做到。 You need to think of the join as an abstract table (sorry, code is in Java...) 您需要将联接视为一个抽象表(对不起,代码在Java中...)

First, create a new content: URL and use a URIMatcher to parse it: 首先,创建一个新content: URL并使用URIMatcher进行解析:

private static final int JOIN_DIR_TYPE = 10;
private static final UriMatcher MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
static {
    // ...
    MATCHER.addURI(
        MyContract.AUTHORITY,
        MyContract.Join.TABLE,
        JOIN_DIR_TYPE);
}

Now, in your query method, delegate queries with the matching URL to a separate method: 现在,在您的查询方法中,将具有匹配URL的查询委托给一个单独的方法:

@Override
public Cursor query(Uri uri, String[] proj, String sel, String[] selArgs, String sort) {
    switch (MATCHER.match(uri)) {
        case JOIN_DIR_TYPE:
            return queryJoin(uri, proj, sel, selArgs, sort)
            break;
            // ...
        default:
            throw new IllegalArgumentException("Unexpected uri: " + uri);
    }
    // ...
}

Now, all you have to do is query the join, in the new method: 现在,您要做的就是使用新方法查询联接:

private Cursor queryJoin(Uri uri, String[] proj, String sel, String[] selArgs, String sort) {
     SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
     qb.setTables("Bank_accounts LEFT OUTER JOIN Bank_customers ON (Bank_customers.customer_id = Bank_accounts.customer_id)")");

You may want to use the QueryBuilder's ColumnMap to map abstract aliases onto column names. 您可能需要使用的QueryBuilder的ColumnMap抽象别名映射到列名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM