热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Android数据库操作:表清理与CRUD功能详解笔记

在本文中,我们将深入探讨Android数据库操作中的表清理及CRUD(创建、读取、更新、删除)功能。具体涉及表结构的设计,包括字段如`store_id`、`store_name`、`store_area_id`和`store_area_name`等,并详细讲解如何通过SQL语句实现这些操作,以提高数据管理的效率和准确性。此外,我们还将讨论如何优化数据库性能,确保应用在处理大量数据时仍能保持流畅运行。

表结构

private final String storeColumns = "store_id,store_name,store_area_id,store_area_name,user_id,user_name";

清空表数据,并根据表中的字段入库

/**

* @param storeList

*/

public void insertStore(List storeList) {

if (storeList.size() == 0) {

Log.i(TAG, "BasicDataManager---insertStore--数据为空,不执行插入");

return;

}

SQLiteDatabase db = dbOpenHelper.getWritableDatabase();

try {

db.beginTransaction();

//先删除

db.execSQL("delete from wd_store", new Object[]{});

for (int i = 0; i

StoreEntity entity = storeList.get(i);

db.execSQL(

"insert into wd_store(" + storeColumns + ") values(?,?,?,?,?,?)",

new Object[]{entity.getStoreId(), entity.getStoreName(), entity.getAreaId(), entity.getAreaName(),

UserManager.getUser().getUserId(),

UserManager.getUser().getUserName()});

}

db.setTransactionSuccessful();

Log.i(TAG, "BasicDataManager---insertStore成功!");

} catch (Exception e) {

e.printStackTrace();

Log.i(TAG, "BasicDataManager---insertStore错误:" + e.getMessage());

} finally {

db.endTransaction();

}

}

查询并

/**

* @return

*/

public List getArea() {

Cursor cursor = null;

List list = new ArrayList();

SQLiteDatabase db = dbOpenHelper.getWritableDatabase();

try {

cursor = db.rawQuery(

"select store_area_id,store_area_name from wd_store where user_id = ? group by store_area_name",

new String[]{UserManager.getUser().getUserId()});

while (cursor.moveToNext()) {

String areaId = cursor.getString(cursor

.getColumnIndex("store_area_id"));

String areaName = cursor.getString(cursor

.getColumnIndex("store_area_name"));

StoreEntity entity = new StoreEntity();

entity.setAreaId(areaId);

entity.setAreaName(areaName);

list.add(entity);

}

Log.i(TAG, "BasicDataManager---getArea成功!");

} catch (Exception e) {

e.printStackTrace();

Log.i(TAG, "BasicDataManager---getArea错误:" + e.getMessage());

} finally {

if (cursor != null) {

cursor.close();

cursor = null;

}

}

return list;

}

/**

* 查询该用户的所有区域

*

* @return

*/

public List getSXTTArea() {

Cursor cursor = null;

List list = new ArrayList();

SQLiteDatabase db = dbOpenHelper.getWritableDatabase();

try {

// cursor = db.rawQuery(

// "select distinct store_id,store_name from sxtt_store where user_id = ? and store_flag =?",

// new String[]{UserManager.getUser().getUserId(),"市"});

// cursor = db.rawQuery(

// "select store_id,store_name from sxtt_store where user_id = ? and store_flag ='省' or store_flag = '市' order by store_name",

// new String[]{UserManager.getUser().getUserId()});

cursor = db.rawQuery("select distinct store_id,store_name from sxtt_store where user_id = ? and store_flag in('省','市') ", new String[]{UserManager.getUser().getUserId()}

);

while (cursor.moveToNext()) {

String areaId = cursor.getString(cursor

.getColumnIndex("store_id"));

String areaName = cursor.getString(cursor

.getColumnIndex("store_name"));

StoreEntity entity = new StoreEntity();

entity.setAreaId(areaId);

entity.setAreaName(areaName);

list.add(entity);

}

Log.i(TAG, "BasicDataManager---getArea成功!");

} catch (Exception e) {

e.printStackTrace();

Log.i(TAG, "BasicDataManager---getArea错误:" + e.getMessage());

} finally {

if (cursor != null) {

cursor.close();

cursor = null;

}

}

return list;

}

多个条件查询,采用and并使用distinct去重

/**

* @return

*/

public List getSXTTStore(String storeId,String storeFlag) {

Cursor cursor = null;

List list = new ArrayList();

SQLiteDatabase db = dbOpenHelper.getWritableDatabase();

try {

cursor = db.rawQuery("select distinct store_id,store_name from sxtt_store where user_id = ? and store_area_id=? and store_flag=?", new String[]{UserManager.getUser().getUserId(), storeId,storeFlag});

while (cursor.moveToNext()) {

String areaId = cursor.getString(cursor

.getColumnIndex("store_id"));

String areaName = cursor.getString(cursor

.getColumnIndex("store_name"));

StoreEntity entity = new StoreEntity();

entity.setAreaId(areaId);

entity.setAreaName(areaName);

list.add(entity);

}

Log.i(TAG, "BasicDataManager---getArea成功!");

} catch (Exception e) {

e.printStackTrace();

Log.i(TAG, "BasicDataManager---getArea错误:" + e.getMessage());

} finally {

if (cursor != null) {

cursor.close();

cursor = null;

}

}

return list;

}



推荐阅读
author-avatar
香烟迷住双眼
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有