SQLite Quick Start [Android entry 4]

Source: Internet
Author: User

========================================================== ========================================================== ===
1. SQLite Introduction

Http://www.sqlite.org/

A very small database provides a relatively complete Relational Database

2. sqliteopenhelper usage


getReadableDatabase ()

       getWritableDatabase ()

       onCreate (SQLiteDatabase db)

       onOpen (SQLiteDatabase db)

       onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion)

       close ()

3. # sqlite3 test_mars_db
 
    sqlite> .schema command: all start with dots. Shows which tables exist in the current database, and how the tables are created!
======================================================= ===================================
1. Operation steps of SQLite database:

    (1) First create a class,

extends SQLiteOpenHelper
    (2)

Write a constructor, a constructor with four parameters (Context context, String name,
                                 CursorFactory factory, int version) {

             super (context, name, factory, version);
        }

        Constructor with two parameters (Context context, String name) {
              this (context, name, 1);
        }
         
        Three parameter constructor (Context context, String name, int version) {
              this (context, name, null, version);
        }

   
     (3)

@override OnCreate method (SQLiteDatabase db) {
              db.execSQL ("create table user (id int, name varchar (20))");
         }

         @override OnUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {
              Can write some operations
         }

2.// What is written in the listen method of the create database button
  

onClick (View v) in createDatabaseButtonListener {
       // Explain, DatabaseHelper is the class that inherits SQLiteOpenHelper above
      

 DatabaseHelper dbHelper = new DatabaseHelper (current class.this, "test_daming");
       SQLiteDatabase db = dbHelper.getReadableDatabase ();
   }

3.// What is written in the listen method of the update database button
  

onClick (View v) in updateDatabaseButtonListener {

       DatabaseHelper dbHelper = new DatabaseHelper (current class.this, "test_daming", 2);
       SQLiteDatabase db = dbHelper.getReadableDatabase ();
   }

4, / / Insert the database table operation, written in the Button button listen method
  

 onClick (View v) in insertButtonListener {
       ContentValues values = new ContentValues ();
       values.put ("id", 1);
       values.put ("name", "daming");
       DatabaseHelper dbHelper = new DatabaseHelper (current class.this, "test_daming");
       SQLiteDatabase db = dbHelper.getWritableDatabase ();
       db.insert ("user", null, values);
   }
5, / / Update the database table operation, written in the Button button listen method
 

  onClick (View v) in updateButtonListener {
       DatabaseHelper dbHelper = new DatabaseHelper (current class.this, "database name");
       SQLiteDatabase db = dbHelper.getWritableDatabase ();
       ContentValues values = new ContentValues ();
       values.put ("name", "Peking");
       db.update ("user", values, "id =?", new String [] {"1"});
   }
6, / / Find the database table operation, written in the Button button listen method
 

  onClick (View v) in queryButtonListener {
       DatabaseHelper dbHelper = new DatabaseHelper (current class.this, data name ");
       SQLiteDatabase db = dbHelper.getWritableDatabse ();
       Cursor cursor = db.query ("user", new String [] ("id", "name", "id =?", New
                                String [] {"1"}, "id", "id", "id"}));
       while (cursor.moveToNext ()) {
           String name = cursor.getString (cursor.getColumnIndex ("name"));
           System.out.println ("query->" + name);
       }
   }
7, / / delete the database table operation, written in the Button button listen method
 

  onClick (View v) in deleteButtonListener {
       DatabaseHelper dbHelper = new DatabaseHelper (current class.this, data name ");
       SQLiteDatabase db = dbHelper.getWritableDatabse ();
       db.delete ("student", "id =?", new String [] {"1"});
       System.out.println ("Delete the record with id = 1 in the table");
   }

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.