sqllite数据库查询方法中需要注意的 模糊查询问题

5月3日 于家中编写代码时发现的问题。

BusService.java中的一个方法,用于获取前台选择的出发地和目的地信息,查询数据库得到相应记录,并实现了准分页

点击(此处)折叠或打开

  1.  
  2.    /**
  3.      * 通过出发地busStartplace和到达地busArriveplace查询记录
  4.      *
  5.      * @param busStartplace出发地
  6.      *
  7.      * @param busArriveplace到达地
  8.      *
  9.      * @param offset
  10.      * 起始
  11.      * @param maxResult
  12.      * 长度
  13.      * limit 0,20,适用于分页
  14.      */
  15.     public List<Bus> findAllByPlace(String busStartplace, String busArrayplace,
  16.             Integer offset, Integer maxResult) {

  17.         SQLiteDatabase sqldb = dbOperateHelper.getReadableDatabase();
  18.         Cursor cursor = sqldb
  19.                 .rawQuery(
  20.                         "select * from bus where bus_route like ? and bus_route like ? order by bus_id asc limit ?,?",
  21.                         new String[] {
  22.                                 "%" + String.valueOf(busStartplace) + "%",
  23.                                 "%" + String.valueOf(busArrayplace) + "%",
  24.                                 String.valueOf(offset),
  25.                                 String.valueOf(maxResult)});

  26.         Bus bus = null;
  27.         List<Bus> busList = new ArrayList<Bus>();

  28.         while (cursor.moveToNext()) {
  29.             int busId = cursor.getInt(cursor.getColumnIndex("bus_id"));
  30.             String busCard = cursor
  31.                     .getString(cursor.getColumnIndex("bus_card"));
  32.             String busTeam = cursor
  33.                     .getString(cursor.getColumnIndex("bus_team"));
  34.             String Startplace = cursor.getString(cursor
  35.                     .getColumnIndex("bus_startplace"));
  36.             String Arrayplace = cursor.getString(cursor
  37.                     .getColumnIndex("bus_arrayplace"));
  38.             String busRoute = cursor.getString(cursor
  39.                     .getColumnIndex("bus_route"));

  40.             bus = new Bus(busId, busCard, busTeam, Startplace,
  41.                     Arrayplace, busRoute);
  42.             busList.add(bus);
  43.         }
  44.         cursor.close();
  45.         return busList;
  46.     }
需要注意的问题:

①模糊查询部分,当使用like进行模糊查询时,又使用到?占位符时,可以使用以下方式进行查询

错误的方法是:   "select * from bus where bus_route like '%?%' and bus_route like '%?%' order by bus_id asc limit ?,?"
  1.   Cursor cursor = sqldb
  2.                 .rawQuery(
  3.                         "select * from bus where bus_route like ? and bus_route like ? order by bus_id asc limit ?,?",
  4.                         new String[] {
  5.                                 "%" + String.valueOf(busStartplace) + "%",
  6.                                 "%" + String.valueOf(busArrayplace) + "%",
  7.                                 String.valueOf(offset),
  8.                                 String.valueOf(maxResult)});


②列表遍历部分
 while (cursor.moveToNext()) {
    ...
 }
   //用于遍历需要得到多组数据时

 if (cursor.moveToFirst()) {
    ...
 }
    //仅有单组数据时




<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
阅读(1484) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值