person类属性有Intenger id,String name,Intenger age,相应的构造方法和set get方法。
package com.xh.tx.dao;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import com.xh.tx.bean.Person;
import com.xh.tx.utils.MySQLiteHelper;
public class PersonDao1
{
private static final String TAG = "PersonDao1";
private MySQLiteHelper helper;
public PersonDao1(Context context)
{
helper = new MySQLiteHelper(context, null, null, -1);//前者创建的时候版本已经固定了,后面的-1没作用,与前面的用Eclipse创建数据库文章相结合
}
//增加数据
public void savePerson(Person p)
{
SQLiteDatabase db = helper.getWritableDatabase();
if(db.isOpen())
{
//nullColumnHack 如果数据库里面的name字段设计的时候不允许为空,但是你传递过来的参数是空
// 如果不设置这个nullColumnHack参数那么就会报错
// 如果你设置nullColumnHack这个参数的值为name那么不会报错
ContentValues values = new ContentValues();
values.put("name", p.getName());
values.put("age", p.getAge());
Long id = db.insert("person", null, values);
Log.d(TAG, "================:" + id);
db.close();
}
}
//删除数据
public void deletePerson(Integer id)
{
SQLiteDatabase db = helper.getWritableDatabase();
if(db.isOpen())
{
//select * from person where id=? and name = ?;
String whereClause = "_id=?";
String[] whereArgs = new String[]{String.valueOf(id)};
int _id = db.delete("person", whereClause, whereArgs);
Log.d(TAG, "================:" + _id);
db.close();
}
}
//修改数据
public void updatePerson(Person p)
{
SQLiteDatabase db = helper.getWritableDatabase();
if(db.isOpen())
{
ContentValues values = new ContentValues();
values.put("name", "xintx");
values.put("age", "2");
String whereClause = "_id=?";
String[] whereArgs = new String[]{String.valueOf(p.get_id())};
db.update("peson", values, whereClause, whereArgs);
db.close();
}
}
//查询单个数据
public void queryItem(Integer id)
{
SQLiteDatabase db = helper.getReadableDatabase();
if(db.isOpen())
{
String[] columns = new String[]{"_id","name","age"};
String selection = "_id=?";
String[] selectionArgs = new String[]{String.valueOf(id)};
String groupBy = null; //按什么什么分组
String having = null; //如果select里面包含了组函数的时候,不能用where去查询 就只有用having
String orderBy = null; //按什么排序 order by id desc;
Cursor cursor = db.query("person", columns, selection, selectionArgs, groupBy, having, orderBy);
if(null != cursor && cursor.moveToFirst())
{
Integer _id = cursor.getInt(0);
String name = cursor.getString(1);
Integer age = cursor.getInt(2);
Log.d(TAG, "_id=" + _id + " name=" + name + " age = " + age);
cursor.close()
db.close();
}
}
}
//查询所有数据
public List<Person> queryAll() {
List<Person> list = null;
db = helper.getReadableDatabase();
if (db.isOpen()) {
list = new ArrayList<Person>();
String[] columns = new String[] { "_id", "name", "age" };
String selection = null;
String selectionArgs[] = null;
String groupBy = null;
String having = null;
String orderBy = null;
Cursor cursor = db.query("person", columns, selection, selectionArgs, groupBy, having, orderBy);
while (cursor != null && cursor.moveToNext()) {
Integer _id = cursor.getInt(0);
String name = cursor.getString(1);
Integer age = cursor.getInt(2);
list.add(new Person(_id, name, age));
}
cursor.close();
db.close();
}
return list;
}
}