您的位置:首页 > 移动开发 > Android开发

android封装好的方法写增删改查

2014-02-23 17:11 232 查看
PersonDBOpenHelper.java

package com.demo.introductiontothedb;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
* 1 .写一个类 继承 SQLiteOpenHelper 帮助创建数据库 版本的控制
* @author Administrator
*
*/
public class PersonDBOpenHelper extends SQLiteOpenHelper {

/**
* 数据库创建帮助类的构造方法
* @param context
*/
public PersonDBOpenHelper(Context context) {
//name 数据库文件的名称
//factory 访问数据库一个数据库的游标工厂
// version 数据库的版本
super(context, "person.db", null, 2);
}
/**
* 当数据库第一次被创建的是 调用的方法.
* 适合做数据库表结构的初始化
* 注意:数据库被创建后,onCreate不会再被调用
* 如果想更改数据库,比如更改表结构,更新版本等
* 使用下面的onUpgrade()方法,这里要想添加money列,
* 还必须更改数据库版本,onUpgrade()才会执行
*/
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table personInfo (id integer primary key autoincrement, name varchar(20), phone varchar(20),address varchar(50)) ");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("alter table personInfo add money varchar(20)");
Log.i("TiShi", "数据库被更改啦");
}

}
PersonDao.java

package com.demo.introductiontothedb.dao;

import com.demo.introductiontothedb.PersonDBOpenHelper;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class PersonDao {
//增删改查
//javaweb 1.加载jdbc驱动.连接 2.准备sql 3.查询

private PersonDBOpenHelper helper;

//任何人使用 dao 都要传递一个上下文
public PersonDao(Context context) {
helper = new PersonDBOpenHelper(context);
}
/**
* 添加一条记录
*/
public boolean add(String name,String phone,String address){
SQLiteDatabase db = helper.getWritableDatabase();
//	db.execSQL("insert into personInfo (name,phone,address) values (?,?,?)",
//		new Object[]{name,phone,address});
ContentValues values = new ContentValues();	//map集合
values.put("name", name);
values.put("phone", phone);
values.put("address", address);
long result = db.insert("personInfo", null, values);
db.close();
if(result != -1){
return true;
}else{
return false;
}

}

/**
* 根据名字查找一条记录
*/
public int find(String name){
int id = -1;
SQLiteDatabase db = helper.getReadableDatabase();
//		Cursor cursor = db.rawQuery("select id from personInfo where name=?",
//				new String[]{name});
Cursor cursor = db.query("personInfo", new String[]{"id"}, "name=?", new String[]{name}, null, null, null);
if(cursor.moveToFirst()){
id = cursor.getInt(0);
}
cursor.close();
db.close();
return id;
}

/**
* 删除一条记录
*/
public boolean delete(int id){
SQLiteDatabase db = helper.getWritableDatabase();
//db.execSQL("delete from personInfo where id=?", new Object[]{id});
int result = db.delete("personInfo", "id=?", new String[]{id + ""});
db.close();
if(result > 0){
return true;
}else{
return false;
}
}

/**
* 更改一条记录
*/
public boolean update(String name,String phone,int id){
SQLiteDatabase db = helper.getWritableDatabase();
//		db.execSQL("update personInfo set name=?,phone=? where id=?",
//				new Object[]{name,phone,id});
//String table, ContentValues values, String whereClause, String[] whereArgs)
ContentValues values = new ContentValues();
values.put("name", name);
values.put("phone", phone);
int result = db.update("personInfo", values, "id=?", new String[]{id + ""});
db.close();
if(result > 0){
return true;
}else{
return false;
}

}

}
TestPerson.java

package com.demo.introductiontothedb.test;

import com.demo.introductiontothedb.dao.PersonDao;
import android.test.AndroidTestCase;

public class TestPerson extends AndroidTestCase {
private PersonDao dao;
@Override
protected void setUp() throws Exception {
dao = new PersonDao(getContext());
super.setUp();
}

public void testAdd(){

assertEquals(true,dao.add("美女","7777", "中国北京"));
}

public void testDelete(){
int id = dao.find("麻生希");
//dao.delete(id);
assertEquals(true, dao.delete(id));
}

public void testUpdate(){
//dao.update("景甜", "888888", 10);
assertEquals(true, dao.update("景甜", "888888", 10));
}

public void testFind(){
//dao.find("麻生希");
assertEquals(8, dao.find("麻生希"));
}
}


AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.demo.introductiontothedb"
android:versionCode="1"
android:versionName="1.0" >

<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:label="Tests for My App"
android:targetPackage="com.demo.introductiontothedb" />

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

<!-- 在application节点下添加 使用的测试library -->
<uses-library android:name="android.test.runner" />

<activity
android:name="com.demo.introductiontothedb.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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Sqlite数据库