/**
* @author andong
* 数据库帮助类, 用于创建和管理数据库的.
*/
public class PersonSQLiteOpenHelper extends SQLiteOpenHelper {
private static final String TAG = "PersonSQLiteOpenHelper";
/**
* 数据库的构造函数
* @param context
*
* name 数据库名称
* factory 游标工程
* version 数据库的版本号 不可以小于1
*/
public PersonSQLiteOpenHelper(Context context) {
super(context, "itheima28.db", null, 2);
}
/**
* 数据库第一次创建时回调此方法.
* 初始化一些表
*/
@Override
public void onCreate(SQLiteDatabase db) {
// 操作数据库
String sql = "create table person(_id integer primary key, name varchar(20), age integer);";
db.execSQL(sql); // 创建person表
}
/**
* 数据库的版本号更新时回调此方法,
* 更新数据库的内容(删除表, 添加表, 修改表)
*/
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if(oldVersion == 1 && newVersion == 2) {
Log.i(TAG, "数据库更新啦");
// 在person表中添加一个余额列balance
db.execSQL("alter table person add balance integer;");
}
}
}
public class PersonDao {
private PersonSQLiteOpenHelper mOpenHelper; // 数据库的帮助类对象
public PersonDao(Context context) {
mOpenHelper = new PersonSQLiteOpenHelper(context);
}
/**
* 添加到person表一条数据
* @param person
*/
public void insert(Person person) {
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
if(db.isOpen()) { // 如果数据库打开, 执行添加的操作
// 执行添加到数据库的操作
db.execSQL("insert into person(name, age) values(?, ?);", new Object[]{person.getName(), person.getAge()});
db.close(); // 数据库关闭
}
}
/**
* 更据id删除记录
* @param id
*/
public void delete(int id) {
SQLiteDatabase db = mOpenHelper.getWritableDatabase(); // 获得可写的数据库对象
if(db.isOpen()) { // 如果数据库打开, 执行添加的操作
db.execSQL("delete from person where _id = ?;", new Integer[]{id});
db.close(); // 数据库关闭
}
}
/**
* 根据id找到记录, 并且修改姓名
* @param id
* @param name
*/
public void update(int id, String name) {
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
if(db.isOpen()) { // 如果数据库打开, 执行添加的操作
db.execSQL("update person set name = ? where _id = ?;", new Object[]{name, id});
db.close(); // 数据库关闭
}
}
public List<Person> queryAll() {
SQLiteDatabase db = mOpenHelper.getReadableDatabase(); // 获得一个只读的数据库对象
if(db.isOpen()) {
Cursor cursor = db.rawQuery("select _id, name, age from person;", null);
if(cursor != null && cursor.getCount() > 0) {
List<Person> personList = new ArrayList<Person>();
int id;
String name;
int age;
while(cursor.moveToNext()) {
id = cursor.getInt(0); // 取第0列的数据 id
name = cursor.getString(1); // 取姓名
age = cursor.getInt(2); // 取年龄
personList.add(new Person(id, name, age));
}
db.close();
return personList;
}
db.close();
}
return null;
}
/**
* 根据id查询人
* @param id
* @return
*/
public Person queryItem(int id) {
SQLiteDatabase db = mOpenHelper.getReadableDatabase(); // 获得一个只读的数据库对象
if(db.isOpen()) {
Cursor cursor = db.rawQuery("select _id, name, age from person where _id = ?;", new String[]{id + ""});
if(cursor != null && cursor.moveToFirst()) {
int _id = cursor.getInt(0);
String name = cursor.getString(1);
int age = cursor.getInt(2);
db.close();
return new Person(_id, name, age);
}
db.close();
}
return null;
}
}
创建 SqlLiteOpenHelper抽象类的子类,
重写构造函数,onCreate()--新建数据表 onUpgrade()方法
在DAO中,创建SqlLiteOpenHelper子类的实例
使用getWritableDatabase()获取数据库SQLiteDatabase实例,并
使用 exeSQL()执行无返回SQL
对于查询:使用rawQuery()方法获取Cursor实例
create table person(id integer primary key,name varchar(10),age integer);