Usage of SQLite in Android storage mode

Source: Internet
Author: User

Android provides five storage methods: file, sharedPreference, network, SQLite, and ContentProvider. SQLite is a lightweight database with the advantages of independence, isolation, cross-platform, multi-language interface, and security. It is widely used at present. Now we will focus on the usage of SQLite in Android. First, I create a database operation class DataBaseHelper, let him inherit from SQLiteOpenHelper, and then re-write the two methods onCreate and onUpgrade. Copy code 1 package com. example. sqlitedb; 2 3 import android. content. context; 4 import android. database. sqlite. SQLiteDatabase; 5 import android. database. sqlite. SQLiteDatabase. cursorFactory; 6 import android. database. sqlite. SQLiteOpenHelper; 7 8 public class DataBaseHelper extends SQLiteOpenHelper {9 10 private final static String DBName = "sqlite3.db"; 11 private final static String TableName = ""; 12 private final static String firstname = "provincial"; 13 private final static String lastname = "number"; 14 15 public DataBaseHelper (Context context, String name, CursorFactory factory, 16 int version) {17 super (context, DBName, factory, version); 18 // TODO Auto-generated constructor stub19} 20 21 @ Override22 public void onCreate (SQLiteDatabase db) {23 // create table 24 String SQL = ("CREATE TABLE" + TableName + "(INTEGER PRIMARY KEY AUTOINCREMENT," + firstname + "VARCHAR," + lastname + "SMALLINT) "); 25 db.exe cSQL (SQL); 26} 27 28 @ Override29 public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {30 // Delete TABLE 31 String SQL = "drop table if exists" + TableName; 32 db.exe cSQL (SQL); 34} 35 36} copy the code and, use SQL statements to perform some common SQLite operations, as shown in the following code: Copy code 1 @ Override 2 public void onCreate (SQLiteDatabase db) {3 // create table 4 String SQL = "create table city (integer primary key autoincrement, provincial VARCHAR, number SMALLINT)"; 5 db.exe cSQL (SQL ); 6 // insert data 7 // Method 1: execSQL 8 String sql1 = "insert into city (firstname, lastname) values ('beijing', '20 ')"; 9 db.exe cSQL (sql1); 10 // Method 2: Use insert11 ContentValues cv = new ContentValues (); 12 cv. put ("provincial", "Tianjin"); 13 cv. put ("number", 30); 14 db. insert ("city", null, cv); 15 // modify data 16 // 1. execSQL17 String sql2 = "update city set lastname = '25' where provincial = 'tianjin '"; 18 db.exe cSQL (sql2); 19 // 2. use insert20 ContentValues cv1 = new ContentValues (); 21 cv1.put ("provincial", "Beijing"); 22 cv1.put ("number", 30); 23 db. insert ("city", null, cv); 24 // delete data 25 String sql3 = "delete from city where provincial = 'beijing'"; 26 db.exe cSQL (sql3 ); 27 // close the database 28 db. close (); 29}

Related Article

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.