Simple implementation of android built-in Database

Source: Internet
Author: User
1. Create a data help class that inherits SQLiteOpenHelper. The MyDBOpenHelper code is as follows: packagecn. sg. mydb; importandroid. content. Context; importandroid. database. sqlite. SQLiteDatabase; importandroid. database. sqlite. SQLiteDatabase. CursorFactory; im

1. Create a data help class that inherits SQLiteOpenHelper. The MyDBOpenHelper code is as follows: package cn. sg. mydb; import android. content. Context; import android. database. sqlite. SQLiteDatabase; import android. database. sqlite. SQLiteDatabase. CursorFactory; im

1. Create a data help class that inherits SQLiteOpenHelper.

MyDBOpenHelper

The Code is as follows:

Package cn. sg. mydb;

Import android. content. Context;
Import android. database. sqlite. SQLiteDatabase;
Import android. database. sqlite. SQLiteDatabase. CursorFactory;
Import android. database. sqlite. SQLiteOpenHelper;

Public class MyDBOpenHelper extends SQLiteOpenHelper {

// Construct the help class for opening my data
Public MyDBOpenHelper (Context context ){
Super (context, "cn. sg. mydb", null, 1 );
}

@ Override
Public void onCreate (SQLiteDatabase db ){
Db.exe cSQL ("drop table if not exits person (_ id integer primary key autoincrement, name varchar (40), age integer )");
}

@ Override
Public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion ){
// TODO Auto-generated method stub

}

}

2. Write a test class to see if the database has been implemented.

The code for the MyDBOpenHelperTest class is as follows:

Package cn. sg. mydb. test;

Import cn. sg. mydb. MyDBOpenHelper;
Import android. test. AndroidTestCase;

Public class MyDBOpenHelperTest extends AndroidTestCase {

Public void testOncreateDatabase (){
New MyDBOpenHelper (this. getContext ());
}

}

3. Configure some configuration files, androidmanifest. xml

The androidmanifest. xml Code is as follows:


Package = "cn. sg. mydb"
Android: versionCode = "1"
Android: versionName = "1.0" type = "codeph" text = "/codeph">



Android: label = "Test for my app"
Android: name = "android. test. InstrumentationTestRunner"
Android: targetPackage = "cn. sg. mydb"
>

Android: icon = "@ drawable/ic_launcher"
Android: label = "@ string/app_name">


Android: name = ". MydbActivity"
Android: label = "@ string/app_name">








Red indicates the newly added configuration file.

4. Finally, test whether the database has been created. If yes, the database has been created successfully.

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.