Questions about SQL statements

Source: Internet
Author: User

============ Problem Description ============


I'm a rookie recently learning something about the Android database there's a problem I don't know how to solve
For example, there is now a table a,a with four fields q,w,e,r. where e is of type int, now to get the sum of all e in the table, how to write the code. Is database, is it using database.rawquery () This method?
Ask the great God for advice!

============ Solution 1============


int sum (Sqlitedatabase db) {cursor cursor = db.rawquery ("Select SUM (e) from a", null); Cursor.movetonext (); int sum = cursor . getInt (0); Cursor.close (); return sum;}

============ Solution 2============


Upstairs, but I think it's better.
int sum () {Sqlitedatabase db = Dbmanger.getinstance ();        cursor cursor = db.rawquery ("Select SUM (e) from a", null); Cursor.movetonext (); int sum = cursor.getint (0); Cursor.close (); Db.close (); return sum;}

Once the database is in use, close it immediately to avoid unnecessary exception collisions. Dbmanger.getinstance (); Is the way you get a DB instance

Questions about SQL statements

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.