Android database creation and android database creation

Source: Internet
Author: User

Android database creation and android database creation

Main java

Package com. itheima. createdatabase; import android. app. activity; import android. content. context; import android. database. sqlite. SQLiteDatabase; import android. OS. bundle; public class MainActivity extends Activity {private Context mContext; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); mContext = this; // create a help Class Object MySqliteOpenHelper mySqliteOpenHelper = new helper (mContext); // call the getReadableDatabase method to initialize the database creation SQLiteDatabase db = MySqliteOpenHelper. getReadableDatabase ();}}

 

Create a class in the same directory to inherit from the database

Package com. itheima. createdatabase; import android. content. context; import android. database. sqlite. SQLiteDatabase; import android. database. sqlite. SQLiteDatabase. cursorFactory; import android. database. sqlite. SQLiteOpenHelper; public class MySqliteOpenHelper extends SQLiteOpenHelper {public MySqliteOpenHelper (Context context) {// context: Context, name: database file name factory: used to create a cursor object, the default value is null // version: the database version number. The onUpgrade method will be called if it changes from 1. After 4.0, the super (context, "info. db ", null, 1);} // The oncreate method is called when the database is created for the first time. It is especially suitable for table structure initialization and SQL statements need to be executed; SQLiteDatabase db can be used to execute SQL statements @ Override public void onCreate (SQLiteDatabase db) {// use SQLiteDatabase to execute the SQL statement db.exe cSQL ("create table info (_ id integer primary key autoincrement, name varchar (20 ))");} // The onUpgrade database is executed only when the version number of the database changes. It is especially suitable for modifying the table structure @ Override public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {// add a phone field db.exe cSQL ("alter table info add phone varchar (11 )");}}

 

Instructor notes

Under what circumstances do we use databases for data storage? When a large amount of data with the same structure needs to be stored.
Mysql sqlserver2000 sqlite embedded lightweight

SqliteOpenHelper

Steps for creating a database:
1. Create a class to integrate SqliteOpenHelper. You need to add a constructor to implement oncreate and onupgrade methods.
Parameters in constructor:

// Context: context, name: name of the database file factory: used to create a cursor object. The default value is null.
// Version: the version number of the database. It starts from 1. If it changes, the onUpgrade method will be called. After 4.0, The onUpgrade method can only be upgraded.
Super (context, "info. db", null, 1 );


2. Create an object for this help class. Calling the getReadableDatabase () method will help us create and open a database.

3. Rewrite oncreate and onupgrdate methods:
The oncreate method is called when the database is created for the first time. It is especially suitable for table structure initialization and requires the execution of SQL statements. SQLiteDatabase db can be used to execute SQL statements.

// The onUpgrade database is executed only when the version number of the database changes. It is especially suitable for modifying the table structure.



Both getWritableDatabase and getReadableDatabase in the help class object can help us get a database operation object SqliteDatabase.

Differences:
GetReadableDatabase:
First, try to open the database in read/write mode. If the disk space is full, he will try to open the database again in read-only mode.
GetWritableDatabase:
Open the database directly in read/write mode. If the disk space is full, an error is reported directly.

 

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.