package com.itheima.db;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class PersonSQLiteOpenHelper extends SQLiteOpenHelper {
/**
* 数据库的构造方法 用来定义数据库的名称 数据库查询的结果集 数据库的版本
* @param context
*/
public PersonSQLiteOpenHelper(Context context) {
super(context, "person.db",null,1);//1 必须是大于等于1的
}
/**
* 数据库第一次创建的时候调用的方法
* db 被创建的数据库
*/
@Override
public void onCreate(SQLiteDatabase db) {
//初始化数据库的表结构
String sql = " create table person (id integer primary key autoincrement,name varchar(50),number varchar(20))";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
}
}
package com.itheima.test;
import com.itheima.db.PersonSQLiteOpenHelper;
import android.test.AndroidTestCase;
public class TestPersonDb extends AndroidTestCase {
public void testCreateDb(){
PersonSQLiteOpenHelper helper = new PersonSQLiteOpenHelper(getContext());
helper.getWritableDatabase();
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.itheima.db"
android:versionCode="1"
android:versionName="1.0" >
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.itheima.db"
></instrumentation>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<uses-library android:name="android.test.runner"/>
<activity
android:name="com.itheima.db.MainActivity"
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>