Complex usage of query in Android original SQLite

Source: Internet
Author: User

Android direct execution SQL is execsql (String sql).

This method can execute arbitrary SQL statements. But changing this is not flexible enough.

Query This method can be a good solution to this problem.

Executes query queries the specified data table returns a data set with a cursor

Tools/Materials
    • Compilation tool: Eclipse
Method/Step
  1. Because we're just introducing a sqlite approach, we don't explain the structure of the project or anything.

    We won't say anything about building a table.

    (Cursor) query (string table, string[] columns, string selection, string[] Selectionargs, String groupBy, string having, St Ring, String limit)

  2. Now the explanation of the parameters

    String table: The table to query

    string[] Columns: Returns that column, and if the argument is null, all columns are returned.

    (but this is not recommended)

    String selection: Returns the filter for that row.

    (format is the where of SQL, set to NULL, return all rows of this table)

  3. string[] Selectionargs: '? ' may be used in the selection field The form to add some extra parameters.

    String GroupBy: How to Group a filter. (not grouped if set to null)

  4. String having: the filter condition of the aggregation after grouping. (The function is the same as the SQL statement)

    String ORDER By: Sort, in the same format as the SQL order.

    (set NULL to use the default (unordered Unonder) arrangement.) )

  5. String limit: The number of rows returned, set to NULL to indicate no restriction clauses.

    Examples such as:

  6. Query returns a cursor.

    There are also a number of ways to target cursors (cursor)

    GetCount (): Total number of records

    IsFirst (): Determine if the first record

    Islast (): Determine if the last record

    Movetofirst (): Move to first record

    Movetolast (): Move to last record

    Move (int offset): Moves to the specified record

    MoveToNext (): Move to scare a record

    Movetoprevious (): Move to previous record

    Getcolumnindex (String columnName): Gets the int type value of the specified column index

  7. The aggregation functions in the SQL statement are written for example: SUM, COUNT, MAX, AVG, and so on. The fundamental difference between these functions and other functions is that they generally function on more than one record. SELECT SUM (population) from BBC

  8. In an SQL statement, there is a filter after grouping (group by), and the filter in the grouped data group is filtered before grouping.

    By using the GROUP BY clause, you can have the sum and COUNT functions work on data that belongs to a group.

    The HAVING clause allows us to filter the group data after the group. The WHERE clause filters the records before aggregating. That is, it works before the GROUP BY clause and the HAVING clause. The HAVING clause filters the group records after aggregation.

    END
Precautions

Complex usage of query in Android original SQLite

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.