Sqlitedatabase Basic Operation

Source: Internet
Author: User
Tags sqlite database

First, Sqliteopenhelper class

Android provides class Sqliteopenhelper to help users simplify operations on the SQLite database. The class is an abstract class that must be implemented by the user themselves and overridden by the OnCreate () and Onupgrade () methods, and the construction method must be overridden. The construction method includes the context, database name, version, and other parameters.

Sqliteopenhelper provides two methods to get the database: the Getwriteabledatabase () method, and Getreadabledatabase (), both of which return a Sqlitedatabase object, If the object does not exist, call the OnCreate method to create one.

Second, sqlitedatabase

Get to Sqlitedatabase can manipulate the database arbitrarily,

2.1 Call Execsql ("XXX") to execute the SQL command directly, for example:

Execsql ("CREATE TABLE book (ID integer primary key autouincrement, author text, Price real,pages integer)") creates a table named ".

Execsql ("drop table if exists book") if the Book table exists, delete it.

Insert ("book", NULL, Contentvalue), adds data to the Book table, and the data is described by Contentvalue. Contentvalue has a number of put methods that receive a key with the corresponding value, where the key is the column in the table.

2.2 Adding data to a table

Insert ("book", NULL, Contentvalue), adds data to the Book table, and the data is described by Contentvalue. Contentvalue has a number of put methods that receive a key with the corresponding value, where the key is the column in the table.

2.3 Updating data in a table

Updata ("book", "Data", "name =?", New String ("The Lost Symbol"), the first parameter specifies the table to update the data, the second parameter specifies the parameter to update, the corresponding column, the third parameter, and the fourth parameter describe the row to delete. which is a placeholder, you can specify its value by using the contents of the four parameters of the ground. The third parameter corresponds to where.

2.4 Deleting data from a table

Delete ("book", "PAGES>?", "500"), the first parameter corresponds to the action of the table, the second and third parameters describe the row to delete the data.

2.5 Querying data

Sqlitedatabase provides a query () method for querying data, but the query method is more complex. Query mainly contains the following parameters:

The parameters correspond to the SQL section description

Table from table_name the name of the specified query

Columns select column1 columns The column name of the specified query

Selection where Column=value a constraint that specifies where

Selection Args-------------------provide a specific value for the placeholder in the where

GroupBy GROUP By column specifies that a group by column is required

Having a having column=value further constraints on the results of GROUP by

ORDER BY COLUMN1,COLUMN2 Specifies how query results are sorted

When the query () method is called, a cursor object is returned, and all data that is queried is removed from the object.

For example:

cursor cursor = db.query ("book", Null,null,null,null,null,null);
if (Cursor.movetofirst ()) {
do {
String name = cursor.getstring (Cursor.getcolumnindex ("name"));
String author = cursor.getstring (Cursor.getcolumnindex ("author"));
int pages = Cursor.getint (Cursor.getcolumnindex ("pages"));
float Price = Cursor.getfloat (Cursor.getcolumnindex ("price"));
LOG.D ("Mainactivity", "title" +name);
LOG.D ("Mainactivity", "author", +author);
LOG.D ("Mainactivity", "pages", +pages);
LOG.D ("Mainactivity", "Price" +price);
}while (Cursor.movetonext ());
Cursor.close ();
}
Third, using SQL to manipulate the database
Use SQL to manipulate databases directly, example:
3.1 Adding data
Db.execsql ("INSERT into book (name, Author,pages,price) VALUES (?,?,?,?)", New string[]{"The Da Vinci Code", "Dan Brown", " 520 "," 19.9 "});
3.2 Updating data
Db.execsql ("updata book set price =? WHERE name =?", New string[]{"9.9", "The Da Vinci Code"});

3.3 Deleting data

      Db.execsql ("Delete from book where Pages>? WHERE name =? ", New string[]{", "The Da Vinci Code"});

3.4 Querying data

Db.rawquery ("SELECT * from book", NULL);

Sqlitedatabase Basic Operation

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.