个人学习整理,如有不足之处,请不吝指教。转载请注明:@CSU-Max
/**
* 查询所有的记录
* @return
*/
public List queryAll() {
List list = new ArrayList();
SQLiteDatabase db = null;
Cursor cursor = null;
try {
//获取只读数据库实例
db = dbOpenHelper.getReadableDatabase();
cursor = db.query("user", null, null, null, null, null, "_id desc");
//遍历 Cursor
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor
.moveToNext()) {
User user = new User();
user.setId(cursor.getInt(cursor.getColumnIndex("_id")));
user.setName(cursor.getString(cursor.getColumnIndex("name")));
list.add(user);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null) {
cursor.close();
}
if (db != null) {
db.close();
}
}
return list;
}
/**
* 根据 id 查询记录
* @param id
* @return
*/
public User queryUserById(int id) {
User user = new User();
SQLiteDatabase db = null;
Cursor cursor = null;
try {
//获取只读数据库
db = dbOpenHelper.getReadableDatabase();
cursor = db.query("user", null, "_id = ?",
new String[] { String.valueOf(id) }, null, null, null);
if (cursor.moveToFirst()) {
user.setId(cursor.getInt(cursor.getColumnIndex("_id")));
user.setName(cursor.getString(cursor.getColumnIndex("name")));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null) {
cursor.close();
}
if (db != null) {
db.close();
}
}
return user;
}
主要代码:
/**
* 根据某一字段的部分值进行模糊查询
*
* @param word
* @return
*/
public List queryByWord(String word) {
List list = new ArrayList();
SQLiteDatabase db = null;
Cursor cursor = null;
try {
// 获取只读数据库
db = dbOpenHelper.getReadableDatabase();
cursor = db.query("user", null, "name like ?", new String[] { word
+ "%" }, null, null, null);
// 遍历 Cursor
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor
.moveToNext()) {
User user = new User();
user.setId(cursor.getInt(cursor.getColumnIndex("_id")));
user.setName(cursor.getString(cursor.getColumnIndex("name")));
list.add(user);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null) {
cursor.close();
}
if (db != null) {
db.close();
}
}
return list;
}
/**
* 向 user 表中 插入一个 user,将 user 的 name 属性插入到 user 表中的 name 列
* @param user
* @return
*/
public long insert(User user) {
SQLiteDatabase db = null;
long id = -1;
try {
db = dbOpenHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name", user.getName());
id = db.insert("user", null, values);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (db != null) {
db.close();
}
}
return id;
}
/**
* 删除指定 id 的数据
* @param id
*/
public void delete(int id) {
SQLiteDatabase db = null;
try {
db = dbOpenHelper.getWritableDatabase();
db.delete("user", "_id = ?", new String[] { String.valueOf(id) });
} catch (Exception e) {
e.printStackTrace();
} finally {
if (db != null) {
db.close();
}
}
}
/**
* 更新数据操作
* @param user
* @return
*/
public long update(User user) {
SQLiteDatabase db = null;
long id = -1;
try {
db = dbOpenHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name", user.getName());
id = db.update("user", values, "_id = ?",
new String[] { String.valueOf(user.getId()) });
} catch (Exception e) {
e.printStackTrace();
} finally {
if (db != null) {
db.close();
}
}
return id;
}
public class Page {
// 每页显示的记录数
private int pageSize = 3;
// 当前打的页数
private int pageNum = 1;
// 总的记录数
private int totalCount = 0;
// 总的页数
private int totalNum = 0;
/**
* 是否有下一页
* @return
*/
public boolean isHasNext() {
if (pageNum < totalNum) {
return true;
} else {
return false;
}
}
/**
* s是否有上一页
* @return
*/
public boolean isHasPre() {
if (pageNum > 1) {
return true;
} else {
return false;
}
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getPageNum() {
return pageNum;
}
public void setPageNum(int pageNum) {
this.pageNum = pageNum;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public int getTotalNum() {
return totalNum;
}
public void setTotalNum(int totalNum) {
this.totalNum = totalNum;
}
}
/**
* 获取某一页的数据
*
* @param pageSize
* 每页显示记录数
* @param pageNum
* 当前的页码
* @return
*/
public List getPageUser(int pageSize, int pageNum) {
List list = new ArrayList();
SQLiteDatabase db = null;
Cursor cursor = null;
try {
// 获取只读数据库实例
db = dbOpenHelper.getReadableDatabase();
// 使用 limit ? offset ? 来进行分页
cursor = db.query("user", null, null, null, null, null,
"_id asc limit " + String.valueOf(pageSize) + " offset "
+ String.valueOf((pageNum - 1) * pageSize));
// 遍历 Cursor
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor
.moveToNext()) {
User user = new User();
user.setId(cursor.getInt(cursor.getColumnIndex("_id")));
user.setName(cursor.getString(cursor.getColumnIndex("name")));
list.add(user);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null) {
cursor.close();
}
if (db != null) {
db.close();
}
}
return list;
}
/**
* 获取表中总的记录数
*
* @return
*/
public int getTotalCount() {
SQLiteDatabase db = null;
Cursor cursor = null;
int totalCount = 0;
try {
// 获取只读数据库实例
db = dbOpenHelper.getReadableDatabase();
cursor = db.query("user", null, null, null, null, null, null);
// 获取记录数
totalCount = cursor.getCount();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null) {
cursor.close();
}
if (db != null) {
db.close();
}
}
return totalCount;
}
/**
* 得到中的页数
*
* @param pageSize
* @return
*/
public int getTotalNum(int pageSize) {
int totalNum = 0;
totalNum = getTotalCount() / pageSize + 1;
return totalNum;
}
/**
* 显示下一页的数据
*
* @param pageSize
* 每页显示记录数
* @param pageNum
* 当前的页码
* @return
*/
public List getNextPage(int pageSize, int pageNum) {
List list = new ArrayList();
//设置Page对象的属性
Page page = new Page();
page.setPageSize(pageSize);
page.setPageNum(pageNum);
page.setTotalCount(getTotalCount());
page.setTotalNum(getTotalNum(pageSize));
// 判断是否有下一页,有下一页则显示下一页的数据
if (page.isHasNext()) {
list = getPageUser(pageSize, pageNum + 1);
}
return list;
}
/**
* 显示上一页的数据
*
* @param pageSize
* 每页显示记录数
* @param pageNum
* 当前的页码
* @return
*/
public List getPrePage(int pageSize, int pageNum) {
List list = new ArrayList();
Page page = new Page();
page.setPageSize(pageSize);
page.setPageNum(pageNum);
page.setTotalCount(getTotalCount());
// 判断是否有上一页,有上一页则显示上一页的数据
if (page.isHasNext()) {
list = getPageUser(pageSize, pageNum - 1);
}
return list;
}
public long insert() {
SQLiteDatabase db = null;
long id = 0;
try {
db = dbOpenHelper.getWritableDatabase();
// 开启事务
db.beginTransaction();
ContentValues values = new ContentValues();
for (int i = 0; i < 5; i++) {
values.put("name", "姓名" + 1);
id = db.insert("user", null, values) + id;
}
// 设置事务标志位为成功
db.setTransactionSuccessful();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (db != null) {
// 结束事务,根据标志位来决定是提交还是回滚
db.endTransaction();
db.close();
}
}
return id;
}
关于 SQLite的简单使用方法就介绍到这里,下一节将总结 Android 中的文件存储数据。
个人学习整理,如有不足之处,请不吝指教。转载请注明:@CSU-Max