android Sqlite3 查询表内容,查询条件由两个EditText控件获取,控件有值的时候,有返回数据,控件空无值
androidSqlite3查询表内容,查询条件由两个EditText控件获取,当两个控件有值的时候,能够返回数据,但只要有一个控件空,就不能的到数据,在query方法中...
android Sqlite3 查询表内容,查询条件由两个EditText控件获取,当两个控件有值的时候,能够返回数据,但只要有一个控件空,就不能的到数据,在query方法中的查询条件我应该怎么写?
String id=et1.getText().toString();
String name=et2.getText().toString();
DatabaseHelper dbHelper =new DatabaseHelper(MainActivity.this, "user",2);
SQLiteDatabase db=dbHelper.getWritableDatabase();
Cursor cursor=db.query("tab", new String[]{"id","name"}, "id=? and name=?", new String[]{id,name}, null, null, null);
while(cursor.moveToNext())
{
String code=cursor.getString(cursor.getColumnIndex("id"));
String username=cursor.getString(cursor.getColumnIndex("name"));
Log.d("query", "id="+code+"name="+username);
} 展开
String id=et1.getText().toString();
String name=et2.getText().toString();
DatabaseHelper dbHelper =new DatabaseHelper(MainActivity.this, "user",2);
SQLiteDatabase db=dbHelper.getWritableDatabase();
Cursor cursor=db.query("tab", new String[]{"id","name"}, "id=? and name=?", new String[]{id,name}, null, null, null);
while(cursor.moveToNext())
{
String code=cursor.getString(cursor.getColumnIndex("id"));
String username=cursor.getString(cursor.getColumnIndex("name"));
Log.d("query", "id="+code+"name="+username);
} 展开
2014-03-17
展开全部
StringBuilder where = new StringBuilder(" 1=1 ");
ArrayList<String> args = new ArrayList<String>();
if(!TextUtils.isEmpty(id)){
where.append(" and id = ?");
args.add(id);
}
if(!TextUtils.isEmpty(name)){
where.append(" and name = ?");
args.add(name);
}
String[] arg = new String[args.size()];
args.toArray(arg);
Cursor cursor=db.query("tab", new String[]{"id","name"},where.toString(), arg, null, null, null);
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询