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:
<? Xml version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: android = "http://schemas.android.com/apk/res/android"
Package = "cn. sg. mydb"
Android: versionCode = "1"
Android: versionName = "1.0" type = "codeph" text = "/codeph">
<Uses-sdk android: minSdkVersion = "8"/>
<Instrumentation
Android: label = "Test for my app"
Android: name = "android. test. InstrumentationTestRunner"
Android: targetPackage = "cn. sg. mydb"
> </Instrumentation>
<Application
Android: icon = "@ drawable/ic_launcher"
Android: label = "@ string/app_name">
<Uses-library android: name = "android. test. runner"/>
<Activity
Android: name = ". MydbActivity"
Android: label = "@ string/app_name">
<Intent-filter>
<Action android: name = "android. intent. action. MAIN"/>
<Category android: name = "android. intent. category. LAUNCHER"/>
</Intent-filter>
</Activity>
</Application>
</Manifest>
Red indicates the newly added configuration file.
4. Finally, test whether the database has been created. If yes, the database has been created successfully.