In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains the usage of Android SQL database query method query (). Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn the usage of the Android SQL database query method query ().
First of all, let's assume that we have the following table with the name "Employees":
Id-LastName-FirstName-Address--City
1-- Adams- John- Oxford Street- London
2-- Bush- George--Fifth Avenue- New York
3-- Carter- Thomas-Changan Street-Beijing
The basic format of SQL is as follows:
SELECT column name FROM table name
The most basic SQL statement is to select the column data to be returned from the table without any filtering conditions. Of course, if our column name is "*", then the entire table data will be returned. On Android, the SQL-related method usually has one parameter, String [] columns, which corresponds to the "column name" in the SQL statement. We can look at the method-query in an Android:
Public Cursor query (String table, String [] columns, String selection, String [] selectionArgs, String groupBy, String having, String orderBy, String limit)
Suppose we want to get the full name of the person, then the SQL statement is as follows:
SELECT FirstName, LastName FROM Employees
Of course, in general, we will conditionally filter the results we want. For example, if I only want to return the information of people whose city is Beijing, then I need to use WHERE to filter:
SELECT * FROM Employees WHERE City= 'Beijing'
Here the string followed by where is the corresponding parameter String selection in the method of Android. There is usually another parameter related to this in Android's method, which is String [] selectionArgs, when the selection parameter contains a question mark "?" Then selectionArgs will be used. For example, suppose the selection parameter is assigned as follows:
String selection = "City=?"
At this point we have to assign a value in selectionArgs
String [] selectionArgs = {"Beijing"}
In other words, the string in selectionArgs is the variable represented by the question mark in selection. In fact, the filter condition City in selection can be assigned dynamically, rather than written in the program. When query () executes, the string in selectionArgs is correctly escaped and replaced with the corresponding? Make up the complete selection string. Kind of like String.format ().
Then it is clear that the parameter String groupBy corresponds to the string after GROUP BY in the SQL statement, and GROUP BY is used with Aggregate Functions functions such as SUM (). You can check the detailed usage on the Internet.
The parameter String having corresponds to the string after the SQL statement HAVING and is also used with the aggregate function.
The parameter String orderBy corresponds to the string after the SQL statement ORDER BY.
The parameter limit indicates the number of rows returned.
Let's give an example. Suppose you have the following data table, named "Orders":
Id-CustomerName OrderPrice Country OrderDate
1-Arc-- 100-China--2010-1-2
2-Bor-200-USA--2010-3-20
3-Cut-500-Japan--2010-2-20
4-Bor-300-USA--2010-3-2
5-Arc-600-China--2010-3-25
6-Doom-200-China-2010-3-26
Suppose we want to query the names and total number of orders of customers with a total order of more than 500RMB and County in China, and sort them by CustomerName. The default ASC order is sorted, then the SQL statement should be:
SELECT CustomerName, SUM (OrderPrice) FROM Orders WHERE Country=? GROUP BY CustomerName HAVING SUM (OrderPrice) > 500 ORDER BY CustomerName
Then the parameters of the query function corresponding to Android are as follows:
String table = "Orders"; String [] columns = new String [] {"CustomerName", "SUM (OrderPrice)"}; String selection = "Country=?"; String [] selectionArgs = new String [] {"China"}; String groupBy = "CustomerName"; String having = "SUM (OrderPrice) > 500"; String orderBy =" CustomerName "; Cursor c = db.query (table, columns, selection, selectionArgs, groupBy, having, orderBy, null)
The result of the query should be:
CustomerName-SUM (OrderPrice)
Arc-700
At this point, I believe you have a deeper understanding of the usage of "Android SQL database query method query ()". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.