Android Java Layer Operations database

Source: Internet
Author: User

first, querying the database in Android uses two important classes:
1.SQLiteDatabase: Used to create, delete, execute SQL commands, and perform other common database administration tasks.
2.query (string table, string[] columns, string selection, string[] Selectionargs, String groupBy, string having, Str ing)
returns the Cursor object
Table : Names of tables inside the database
columns: Need to query out database column array
selection: The database query condition, which is equivalent to the condition behind the where. If not, use NULL instead
Selectionargs: Database where conditions are often followed? Number, this is it? Replacement of the number. If not, use NULL instead
GroupBy: Whether the queried data needs to be grouped. If not, use NULL instead
Having : aggregation operation. If not, use NULL instead
ORDER BY: Whether the queried data needs to be sorted. If not, use NULL instead
Cursor: Query The result object returned by the database.
IsFirst (): Returns whether the cursor is pointing to the first row.
islast (): Returns whether the cursor points to the last row.
Movetofirst (): Moves the cursor to the first line.
movetolast (): Moves the cursor to the last line.
MoveToNext (): Moves the cursor to the next line.
movetoposition (int position): Moves the cursor to an absolute position.
movetoprevious (): Moves the cursor to the previous line.
Getcolumnindex (String columnName): Gets the column index, starting with 0
Getcolumnindexorthrow (String columnName): Returns the zero-based index of the given column name

Second, Java code

1 definition: Private final String Database_path = "/data/data/com.android.providers.settings/databases";       Private final String database_filename = "settings.db"; Sqlitedatabase database;               2 Open database: Private Sqlitedatabase OpenDatabase () {try {//Get absolute path to dictionary.db file               String DatabaseFileName = Database_path + "/" + database_filename;               File dir = new file (Database_path);                       Open the Dictionary.db file in the/sdcard/dictionary directory sqlitedatabase database = Sqlitedatabase.openorcreatedatabase (               DatabaseFileName, NULL);           return database;       } catch (Exception e) {} return null; }//3 Query database: System.out.println ("*****************************select********************************");d atabase = OpenDatabase (); String name = NULL;        Cursor cs=database.rawquery ("SELECT * from secure where name=\" udp_url\ "", null); Print the third column parameter value while (Cs.movetonext ()) {         Name = Cs.getstring (2);        SYSTEM.OUT.PRINTLN (name);        } cs.close ();        Database.close (); System.out.println ("******************************select*******************************");//        4 Update Database: System.out.println ("****************************update*********************************");        Database = OpenDatabase ();        Cursor cs=database.rawquery ("Update secure Set Value =\" "+ str +" \ "where name= ' Udp_url '", null);            while (Cs.movetonext ()) {String name = cs.getstring (2);            SYSTEM.OUT.PRINTLN (name);        } cs.close ();        Database.close (); System.out.println ("****************************update*********************************");</span>


Android Java Layer Operations database

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.