Chinaunix首页 | 论坛 | 博客
  • 博客访问: 294252
  • 博文数量: 30
  • 博客积分: 732
  • 博客等级: 军士长
  • 技术积分: 439
  • 用 户 组: 普通用户
  • 注册时间: 2012-05-10 00:13
文章分类

全部博文(30)

文章存档

2015年(1)

2013年(5)

2012年(24)

我的朋友

分类: Android平台

2013-05-03 14:37:44

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()) {
    ...
 }
    //仅有单组数据时




阅读(3435) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~