android内置数据库的简单实现

WBOY
Release: 2016-06-07 15:54:19
Original
1168 people have browsed it

1.首先创建一个数据的帮助类,继承SQLiteOpenHelper。 MyDBOpenHelper 代码如下: package cn.sg.mydb; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; im

1.首先创建一个数据的帮助类,继承SQLiteOpenHelper。

MyDBOpenHelper

代码如下:

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 {

//我的数据打开帮助类的构造方法
public MyDBOpenHelper(Context context) {
super(context, "cn.sg.mydb", null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("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.写一个测试类,看看数据库有没有实现了。

MyDBOpenHelperTest 类的代码如下:

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.配置一些配置文件,androidmanifest.xml

androidmanifest.xml的代码如下:


package="cn.sg.mydb"
android:versionCode="1"
android:versionName="1.0" >



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" >








红色标记的是新添加的配置文件。

4.最后测试一下,如果看看有没生成这个数据库,如果有的话,说明你创建的数据库已经创建成功了。

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!