PersonDBOpenHelper.java
package com.demo.dbtest.dbHelper;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
/**
* 1.写一个类,继承SQLiteOpenHelper帮助创建数据库版本的控制
* @author David
*
*/
public class PersonDBOpenHelper extends SQLiteOpenHelper {
/**
* 数据库创建帮助类的构造方法
* @param context
*/
public PersonDBOpenHelper(Context context) {
super(context,"person.db",null,1);
// TODO Auto-generated constructor stub
}
/**
* 当数据库第一次被创建的时候调用的方法
* 适合做数据库表结构的初始化
*/
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table person(id integer primary key autoincrement,name varchar(20),phone varchar(20))");
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
}
PersonDao.java
package com.demo.dbtest.dao;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.demo.dbtest.dbHelper.PersonDBOpenHelper;
public class PersonDao {
//增删改查
//javaweb 1.加载jdbc驱动,连接 2.准备sql 3.查询
private PersonDBOpenHelper helper;
//任何人是使用dao都要传递一个上下文
public PersonDao(Context context){
helper = new PersonDBOpenHelper(context);
}
/**
* 添加一条记录
*/
public void add(String name,String phone){
SQLiteDatabase db = helper.getWritableDatabase();
db.execSQL("insert into person(name,phone) values(?,?)", new Object[]{name,phone});
db.close();
}
/**
* 删除一条记录
*/
public void delete(int id){
SQLiteDatabase db = helper.getWritableDatabase();
db.execSQL("delete from person where id = ?",new Object[]{id});
db.close();
}
/**
* 更新记录
*/
public void update(String newphone,int id){
SQLiteDatabase db = helper.getWritableDatabase();
db.execSQL("update person set phone =? where id=?",new Object[]{newphone,id});
db.close();
}
/**
* 查询记录
* @return 当前记录的id,如果不存在,返回-1
*/
public int find(String name){
int id = -1;
SQLiteDatabase db = helper.getReadableDatabase(); //获取可读的数据库
Cursor cursor = db.rawQuery("select id from person where name=?", new String[]{name});
if(cursor.moveToFirst()){ //移动到结果集的第一行,如果移动成功,证明查询成功了
id = cursor.getInt(0); //获得第一列的内容
}
cursor.close();
db.close();
return id;
}
}
package com.demo.dbtest.test;
import com.demo.dbtest.dao.PersonDao;
import android.test.AndroidTestCase;
public class TestPersonDao extends AndroidTestCase {
private PersonDao dao;
@Override
protected void setUp() throws Exception {
dao = new PersonDao(getContext()); //测试框架,提供一个假的虚拟的上下文
super.setUp();
}
public void testAdd() throws Exception{
dao.add("zhangsan", "1512454645");
}
public void testDelete() throws Exception{
int id = dao.find("zhangsan");
dao.delete(id);
}
public void testUpdate() throws Exception{
int id = dao.find("zhangsan");
dao.update("99999", id);
}
public void testFind() throws Exception{
dao.find("zhangsan");
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.demo.dbtest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:label="Tests for My App"
android:targetPackage="com.demo.dbtest" />
<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.demo.dbtest.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>
数据库写法:
1. 写一个数据库打开的帮助类 继承 sqliteOpenHelper.
2. 重写帮助类的oncreate方法 数据库表结构的初始化
3. 利用 OpenHelper得到数据库.