5月3日 于家中编写代码时发现的问题。
BusService.java中的一个方法,用于获取前台选择的出发地和目的地信息,查询数据库得到相应记录,并实现了准分页
需要注意的问题:
①模糊查询部分,当使用like进行模糊查询时,又使用到?占位符时,可以使用以下方式进行查询
错误的方法是: "select * from bus where bus_route like '%?%' and bus_route like '%?%' order by bus_id asc limit ?,?"
②列表遍历部分
while (cursor.moveToNext()) {
...
} //用于遍历需要得到多组数据时
if (cursor.moveToFirst()) {
...
} //仅有单组数据时
阅读(1484) | 评论(0) | 转发(0) |
<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>
BusService.java中的一个方法,用于获取前台选择的出发地和目的地信息,查询数据库得到相应记录,并实现了准分页
点击(此处)折叠或打开
- /**
- * 通过出发地busStartplace和到达地busArriveplace查询记录
- *
- * @param busStartplace出发地
- *
- * @param busArriveplace到达地
- *
- * @param offset
- * 起始
- * @param maxResult
- * 长度
- * limit 0,20,适用于分页
- */
- public List<Bus> findAllByPlace(String busStartplace, String busArrayplace,
- Integer offset, Integer maxResult) {
-
- SQLiteDatabase sqldb = dbOperateHelper.getReadableDatabase();
- Cursor cursor = sqldb
- .rawQuery(
- "select * from bus where bus_route like ? and bus_route like ? order by bus_id asc limit ?,?",
- new String[] {
- "%" + String.valueOf(busStartplace) + "%",
- "%" + String.valueOf(busArrayplace) + "%",
- String.valueOf(offset),
- String.valueOf(maxResult)});
-
- Bus bus = null;
- List<Bus> busList = new ArrayList<Bus>();
-
- while (cursor.moveToNext()) {
- int busId = cursor.getInt(cursor.getColumnIndex("bus_id"));
- String busCard = cursor
- .getString(cursor.getColumnIndex("bus_card"));
- String busTeam = cursor
- .getString(cursor.getColumnIndex("bus_team"));
- String Startplace = cursor.getString(cursor
- .getColumnIndex("bus_startplace"));
- String Arrayplace = cursor.getString(cursor
- .getColumnIndex("bus_arrayplace"));
- String busRoute = cursor.getString(cursor
- .getColumnIndex("bus_route"));
-
- bus = new Bus(busId, busCard, busTeam, Startplace,
- Arrayplace, busRoute);
- busList.add(bus);
- }
- cursor.close();
- return busList;
- }
①模糊查询部分,当使用like进行模糊查询时,又使用到?占位符时,可以使用以下方式进行查询
错误的方法是: "select * from bus where bus_route like '%?%' and bus_route like '%?%' order by bus_id asc limit ?,?"
- Cursor cursor = sqldb
- .rawQuery(
- "select * from bus where bus_route like ? and bus_route like ? order by bus_id asc limit ?,?",
- new String[] {
- "%" + String.valueOf(busStartplace) + "%",
- "%" + String.valueOf(busArrayplace) + "%",
- String.valueOf(offset),
- String.valueOf(maxResult)});
-
②列表遍历部分
while (cursor.moveToNext()) {
...
} //用于遍历需要得到多组数据时
if (cursor.moveToFirst()) {
...
} //仅有单组数据时
相关热门文章
给主人留下些什么吧!~~
评论热议