rawqueryandroid
① Android GreenDao rawQuery 之 like 、 or、 = 拼接查询条件
private void queryLocomotiveList() {
List<Locomotive> locomotiveList = new ArrayList<>();
String locomotiveModeName = tvEngineMode.getText().toString().trim();
String locomotive = tvEngineNo.getText().toString().trim();
try {
//请求参数
ArrayList<String> strParamLt = new ArrayList<String>();
String queryString =
"SELECT DISTINCT * FROM " + LocomotiveDao.TABLENAME + " where 1==1 ";
if (!TextUtils.isEmpty(locomotiveModeName)) {
queryString = queryString + " and " + LocomotiveDao.Properties.TrainModelName.columnName + " = ?";
strParamLt.add(locomotiveModeName);
}
if (!TextUtils.isEmpty(locomotive)) {
queryString = queryString + " and " + LocomotiveDao.Properties.TrainNo.columnName + " like '%" + locomotive + "%' ";
}
if (!TextUtils.isEmpty(healthy) || !TextUtils.isEmpty(attention) || !TextUtils.isEmpty(repair)){
String healthyTypeStr = " and " + "(";
if (!TextUtils.isEmpty(healthy)) {
healthyTypeStr += LocomotiveDao.Properties.HealthAnalysisLevel.columnName + " = ?" + " or ";
strParamLt.add(healthy);
}
if (!TextUtils.isEmpty(attention)) {
healthyTypeStr += LocomotiveDao.Properties.HealthAnalysisLevel.columnName + " = ?" + " or ";
strParamLt.add(attention);
}
if (!TextUtils.isEmpty(repair)) {
healthyTypeStr += LocomotiveDao.Properties.HealthAnalysisLevel.columnName + " = ?";
strParamLt.add(repair);
}
if (healthyTypeStr.endsWith("or ")){
healthyTypeStr = healthyTypeStr.substring(0,healthyTypeStr.length() - 3);
}
healthyTypeStr += ")";
queryString = queryString + healthyTypeStr;
}
Log.e("queryString",queryString);
Object[] objs = strParamLt.toArray();
String[] strs = new String[objs.length];
for (int i = 0; i < objs.length; i++) {
strs[i] = objs[i].toString();
}
Log.e("strs",new Gson().toJson(strs));
Cursor cursor = DaoManager.getInstance().getDaoSession().getDatabase().rawQuery(queryString, strs);
int trainNoIndex = cursor.getColumnIndex(LocomotiveDao.Properties.TrainNo.columnName);
int trainModeNameIndex = cursor.getColumnIndex(LocomotiveDao.Properties.TrainModelName.columnName);
int healthIndex = cursor.getColumnIndex(LocomotiveDao.Properties.HealthAnalysisLevel.columnName);
while (cursor.moveToNext()) {
Locomotive bean = new Locomotive();
bean.setTrainNo(cursor.getString(trainNoIndex));
bean.setTrainModelName(cursor.getString(trainModeNameIndex));
bean.setHealthAnalysisLevel(cursor.getString(healthIndex));
locomotiveList.add(bean);
}
Log.e("locomotiveList",new Gson().toJson(locomotiveList));
} catch (Exception e) {
e.printStackTrace();
}
mAdapter.setNewData(locomotiveList);
}
② android sqlite中查询数据rawQuery方法中String[] selectionArgs的详解,谢谢!!!
既然你选择了有条件的sql语句,假如你有一个data表有下面这样的数据
id name number
1 lisi 5554
2 wangwu 5556
rawQuery的sql语句select * from data where name=?,后面selectionArgs是?条件值,如果?这个占位符为null的话就表示把所有的name查出来(lisi,wangwu都会返回到Cursor结果集里),如果这个?占位符有值为wangwu,就表示只把wangwu这条数据查出来,所以?就是selectionArgs占位符,而selectionArgs就是具体条件,为null就表示全部都要,如果指定的要选wangwu就要new String[] {"wangwu"}这个占位符?的值给出来。
③ android rawQuery内的参数是什么鬼
你好,rawQuery里面的参数是数据库查询语句。就是标准的SQL语句,如果不了解,可以找点数据库的书籍看看,SQL语句是数据库最基本的,也是非常重要的知识点。这语句意思是,在dict数据库表中查询word字段或者detail字段中包含key的项。其中的*代表所有的,就是数据库中的所有字段都要查询出来,%是通配符,%key%,代表只要字符串有key就符合(key%代表以key开头,%key代表以key结尾),问号是传入参数的意思,就是将后面的两个"%" + key + "%"按顺序传入问号处
④ android怎么实现sqlite的增删改查
SQL语句大全
--语 句 功 能
--数据操作
SELECT --从数据库表中检索数据行和列
INSERT --向数据库表添加新数据行
DELETE --从数据库表中删除数据行
UPDATE --更新数据库表中的数据
--数据定义
CREATE TABLE --创建一个数据库表
DROP TABLE --从数据库中删除表
ALTER TABLE --修改数据库表结构
CREATE VIEW --创建一个视图
DROP VIEW --从数据库中删除视图
CREATE INDEX --为数据库表创建一个索引
DROP INDEX --从数据库中删除索引
CREATE PROCEDURE --创建一个存储过程
DROP PROCEDURE --从数据库中删除存储过程
CREATE TRIGGER --创建一个触发器
DROP TRIGGER --从数据库中删除触发器
CREATE SCHEMA --向数据库添加一个新模式
DROP SCHEMA --从数据库中删除一个模式
CREATE DOMAIN --创建一个数据值域
ALTER DOMAIN --改变域定义
DROP DOMAIN --从数据库中删除一个域
--数据控制
GRANT --授予用户访问权限
DENY --拒绝用户访问
REVOKE --解除用户访问权限
--事务控制
COMMIT --结束当前事务
ROLLBACK --中止当前事务
SET TRANSACTION --定义当前事务数据访问特征
--程序化SQL
DECLARE --为查询设定游标
EXPLAN --为查询描述数据访问计划
OPEN --检索查询结果打开一个游标
FETCH --检索一行查询结果
CLOSE --关闭游标
PREPARE --为动态执行准备SQL 语句
EXECUTE --动态地执行SQL 语句
DESCRIBE --描述准备好的查询
---局部变量
declare @id char(10)
--set @id = '10010001'
select @id = '10010001'
---全局变量
---必须以@@开头
--IF ELSE