Use SQLite database to store data under Android

Source: Internet
Author: User
Tags sqlite database

First step: Write a class, inherit Sqliteopenhelper

1  Public class extends Sqliteopenhelper {23 }

Step Two: Add a constructor and specify the necessary parameters

// name   :  database names //  factory:  cursor Factory    //    version:  public  mydatabaseopenhelper (context context) {      Supernull, 1);}

And in the OnCreate method , the SQL statement is executed through the DB object to create the table

Db.execsql ("CREATE Table Users" (_id integer primary key autoincrement, name varchar (ten), password varchar (20)));

Step three: The new object, and then the DB instance object, you can create the database.

New Mydatabaseopenhelper (this);   // This line of code executes before the database file is created Sqlitedatabase db = Helper.getreadabledatabase ();

Attention:

    • The Oncreate method will only be executed when the database is first created

Generally here to create a table,

    • The 0nupgrade method will only be executed if the database version is upgraded

The version of the database can only be upgraded, not degraded, the database version of the upgrade can be skip.

This is mostly used to modify the structure of existing tables. Or add a new table.

Use SQLite database to store data under Android

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.