Simple application of "Android" database--Create Database

Source: Internet
Author: User

Sqliteopenhelper is an abstract class that must write a class to inherit it to use it. Sqliteopenhelper has two abstract methods OnCreate () and Onupgrade (), and we want to override both methods in the class to implement creating and updating the database.

The Sqliteopenhelper class also has two important examples of methods Getreadabledatabase and Getwritabledatabase (), both of which can open or create a database. The difference is that when the database is not writable, such as when disk space is full, the Getreadabledatabase () method opens the database in read-only form, and the Getwritabledatabase () method has an exception.

Overriding the two constructor methods of the Sqliteopenhelper class typically use the one with fewer parameters, which takes four arguments, the first parameter is the context context, the second parameter is the table name, and the third parameter allows us to return a custom cursor when querying the data. Typically, null is passed in. The fourth parameter is the version number of the current database, which can be used to upgrade the database.

After building an instance of Sqliteopenhelper, call its getreadabledatabase () or getwritabledatabase () to create the database.

eg

Create a database named Mydatabase.db, creating a new user table with the ID (primary key) and the Name,password property.

The CREATE TABLE statement for the user table is as follows:

CREATE TABLE user{

ID Integer PRIMARY KEY AUTOINCREMENT,//INTEGR: Shaping PRIMARY key: Primary key, Unique key autoincrement: self-growth

Name Text,//text: text type, plus real floating point, blob binary type

Password text)

Execute this SQL statement in your code, complete as follows:

        

public class Mydatabasehelper extends Sqliteopenhelper {public static final String create_user = "CREATE table Uer (" + "ID Integer PRIMARY key AutoIncrement, "+" Name text, "+" password text, ")";p rivate Context mcontext;public mydatabasehelper (context context, String name, cursorfactoryfactory, int version) {Super (context, name, Factory, version); mcontext = context;} @Overridepublic void OnCreate (Sqlitedatabase db) {db.execsql (create_user); Toast.maketext (Mcontext, "Create succeeded", Toast.length_short). Show (); @Overridepublic void Onupgrade (sqlitedatabase db, int oldversion, int newversion) {}}

  

      

    

Simple application of "Android" database--Create Database

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.