Basic operation of the SQLite database for Android storage learning

Source: Internet
Author: User
Tags sqlite database

This section learns the SQLite database, SQLite is a simple lightweight database built into Android. Introduction to SQLite I do not introduce too much here.

Since we want to learn the basic manipulation of the database, it is inseparable from the creation of databases, additions and deletions and other operations.

Before learning about database operations, let me introduce you to a class: Sqliteopenhelper. Sqliteopenhelper is a tool class for database management introduced by the Android system for easy program development. Can be used to create and version updates about a database. The general usage is to create a subclass of Sqliteopenhelper and implement its OnCreate method and OnUpdate method.


For example: Here is a subclass of Sqliteopenhelper I have defined:

public class Mysqliteopenhelper extends Sqliteopenhelper {/* * Context: contexts, usually activity * Name: Database file name * Factory: usually default is NULL * Version: The release number of the database, usually starting at 1, and must be greater than 0 */public mysqliteopenhelper (context context, String name,cursorfactory factory, int version) {Super (context, name, Factory, version);//TODO auto-generated constructor stub}//The database is created when you call @overridepublic void OnCreate (Sqlitedatabase arg0) {log.i ("Mysqliteopenhelper", "onCreate-----was called! ");} Database upgrade is called @overridepublic void Onupgrade (sqlitedatabase arg0, int arg1, int arg2) {log.i ("Mysqliteopenhelper", " Onupgrade-------was called ");}}
We have to implement the method of constructing the subclass.

Since we probably know Sqliteopenhelper, we'll start with the action first.


1: Create a database about the weather first

Private sqlitedatabase db;public void Create () {Mysqliteopenhelper Oh = new Mysqliteopenhelper (GetContext (), "weather.db ", null, 1);/* If the database does not exist, create the database first, and then obtain a readable writable database object. If the database exists, open it directly. * Generally: Getwritabledatabase and getreadabledatabase return a readable and writable database * But in case of an error: for example, if the disk is full, the call to Getreadabledatabase returns only the read-only database. See API description */db = Oh.getwritabledatabase ();//oh.getreadabledatabase ();}
The Create method in Mysqliteopenhelper is called when there is no database for the first time

The database was created when called @overridepublic void OnCreate (Sqlitedatabase db) {log.i ("Mysqliteopenhelper", "onCreate-----was called! ");d b.execsql (" CREATE TABLE weather (_id Integer primary key autoincrement, City Char (TEN), temp integer (3), PM integer (5)) " );}

When I test:

At this point, under the package name Databasesx, the database already exists.


Open after discovery:

Now that the database exists, if you call Oh.getwritabledatabase () again, or oh.getreadabledatabase (), open the database directly and no longer call the OnCreate method.


2: Insert a few data into the database

public void Insert () {//Get database object Mysqliteopenhelper Oh = new Mysqliteopenhelper (GetContext (), "weather.db", NULL, 1);d B = Oh . Getwritabledatabase ();//Insert 4 Records Db.execsql ("INSERT into weather (city, temp, PM) VALUES (?,?,?)", New object[]{"Beijing", 37,280});d B.execsql ("INSERT into weather (city, temp, PM) VALUES (?,?,?)", New object[]{"Xi ' an", 35,200});d B.execsql (" Insert into weather (city, temp, PM) VALUES (?,?,?) ", New object[]{" Shanghai ", 37,100});d B.execsql (" INSERT into weather (city, TEM P, PM) VALUES (?,?,?) ", New object[]{" Harbin ", 30,80});//Close Database Db.close ();}

To export a database:

3: Delete a record

public void Delete () {//Get database object Mysqliteopenhelper Oh = new Mysqliteopenhelper (GetContext (), "weather.db", NULL, 1);d B = Oh . Getwritabledatabase ();//delete the city named Harbin Db.execsql ("Delete from weather to where City =?", New object[]{"Harbin"});// Close database Db.close ();}
When exported, it appears as:

4: Modify XI ' An's temperature to 36 degrees

public void Update () {//Get database object Mysqliteopenhelper Oh = new Mysqliteopenhelper (GetContext (), "weather.db", NULL, 1);d B = Oh . Getwritabledatabase ();//Modify XI ' an temperature of 36 degrees Db.execsql ("update weather set temp =? WHERE city =? ", New object[]{36," Xi ' an "});//Close Database Db.close ();}

The export is displayed as:

5: Search for cities with temperatures greater than 36 degrees

public void query () {//Get database object Mysqliteopenhelper Oh = new Mysqliteopenhelper (GetContext (), "weather.db", NULL, 1);d B = Oh. Getwritabledatabase ();//Query the city with a temperature greater than 36 cursor cursor = db.rawquery ("SELECT * from weather where temp >?",  new string[ ]{(Cursor.movetonext ()) {String city = cursor.getstring (Cursor.getcolumnindex ("City")); String temp = cursor.getstring (Cursor.getcolumnindex ("temp")); String pm = cursor.getstring (Cursor.getcolumnindex ("PM")); System.out.println (city + ";" + temp + ";" + pm);} Close database Db.close ();}

Print as:

About the database additions and deletions to be here. We have wood. It is not very troublesome to write SQL statements directly, and it is possible to write a wrong letter. So Google provides us with a set of APIs that make it easy to manipulate databases. We'll talk about using the API in the next section.



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Basic operation of the SQLite database for Android storage learning

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.