/**
* user的数据访问对象类-使用数据库语句对数据操进行操作
*
* @author Administrator
*
*/
public class UserDao {
private MySQLiteHelper helper;
// 在构造函数中创建数据库帮助类
public UserDao(Context context) {
helper = new MySQLiteHelper(context, user.db, 1);
}
/**
* 插入数据到数据库
*
* @param id
* @param name
* @param age
*/
public void add(int id, String name, int age) {
SQLiteDatabase db = helper.getWritableDatabase();
db.execSQL(insert into user(id, name, age) values(?, ?, ?),
new Object[] { id, name, age });
db.close();
}
/**
* 查询是否成功
*
* @param name
*/
public boolean select(String name) {
SQLiteDatabase db = helper.getReadableDatabase();
Cursor cursor = db.rawQuery(select * from user where name=?,
new String[] { name });
boolean result = cursor.moveToNext();
cursor.close();
db.close();
return result;
}
/**
* 修改记录
*
* @param name
* @param age
*/
public void update(String name, int age) {
SQLiteDatabase db = helper.getWritableDatabase();
db.execSQL(update user set age=? where name=?, new Object[] { age,
name });
db.close();
}
/**
* 删除记录
*
* @param name
*/
public void delete(String name) {
SQLiteDatabase db = helper.getWritableDatabase();
db.execSQL(delete from user where name=?, new Object[] { name });
db.close();
}
/**
* 查询全部
*
* @return
*/
public List<user> selectAll() {
SQLiteDatabase db = helper.getReadableDatabase();
Cursor cursor = db.rawQuery(select * from user, null);
List<user> users = new ArrayList<user>();
while (cursor.moveToNext()) {
int id = cursor.getInt(cursor.getColumnIndex(id));
String name = cursor.getString(cursor.getColumnIndex(name));
int age = cursor.getInt(cursor.getColumnIndex(age));
User user = new User(id, name, age);
users.add(user);
user = null;
}
cursor.close();
db.close();
return users;
}
}
</user></user></user>
/**
* user的数据访问对象类 ;使用系统的API更新数据
*
* @author Administrator
*
*/
public class UserDaoBySys {
private MySQLiteHelper helper;
// 在构造函数中创建数据库帮助类
public UserDaoBySys(Context context) {
helper = new MySQLiteHelper(context, user.db, 1);
}
/**
* 插入数据到数据库
*
* @param id
* @param name
* @param age
*/
public boolean add(int id, String name, int age) {
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(id, id);
values.put(name, name);
values.put(age, age);
long result = db.insert(user, null, values);
db.close();
if (result != -1)
return true;
else
return false;
}
/**
* 查询是否成功
*
* @param name
*/
public boolean select(String name) {
SQLiteDatabase db = helper.getReadableDatabase();
// Cursor cursor = db.rawQuery(select * from user where name=?,
// new String[] { name });
Cursor cursor = db.query(user, null, name=?, new String[] { name },
null, null, null);
boolean result = cursor.moveToNext();
cursor.close();
db.close();
return result;
}
/**
* 修改记录
*
* @param name
* @param age
*/
public boolean update(String name, int age) {
SQLiteDatabase db = helper.getWritableDatabase();
// db.execSQL(update user set age=? where name=?, new Object[] { age,
// name });
ContentValues values = new ContentValues();
values.put(name, name);
values.put(age, age);
int result = db.update(user, values, name=?, new String[] { name });
db.close();
if (result > 0)
return true;
else
return false;
}
/**
* 删除记录
*
* @param name
*/
public boolean delete(String name) {
SQLiteDatabase db = helper.getWritableDatabase();
// db.execSQL(delete from user where name=?, new Object[] { name });
int result = db.delete(user, name=?, new String[] { name });
db.close();
if (result == 0)
return false;
else
return true;
}
/**
* 查询全部
*
* @return
*/
public List<user> selectAll() {
SQLiteDatabase db = helper.getReadableDatabase();
// Cursor cursor = db.rawQuery(select * from user, null);
Cursor cursor = db.query(user, new String[] { id, name, age },
null, null, null, null, null);
List<user> users = new ArrayList<user>();
while (cursor.moveToNext()) {
int id = cursor.getInt(cursor.getColumnIndex(id));
String name = cursor.getString(cursor.getColumnIndex(name));
int age = cursor.getInt(cursor.getColumnIndex(age));
User user = new User(id, name, age);
users.add(user);
user = null;
}
cursor.close();
db.close();
return users;
}
}
</user></user></user>