关于SQLite数据列表的读取——写博客时不好的风气

最近由于功能需求,用到了sqlite数据库。在网上找到了很多有用的知识,尽管很多文章都是一样的,这里大家都懂的!

在数据读取的功能中,不管是哪个版本的文章,统一使用了while的方法读取,代码如下:

/** 
     * 查询列表 
     * @throws Exception 
     */  
    public void selectList()throws Exception{  
        DBHelper dbHelper = new DBHelper(this.getContext());  
        dbHelper.open();  
        Cursor returnCursor = dbHelper.findList("user",new String[] {"id","username", "password"}, "username = 'test'", null,null, null, "id desc");  
        while(returnCursor.moveToNext()){  
            String id = returnCursor.getString(returnCursor.getColumnIndexOrThrow("id"));  
            String username = returnCursor.getString(returnCursor.getColumnIndexOrThrow("username"));  
            String password = returnCursor.getString(returnCursor.getColumnIndexOrThrow("password"));  
        }  
    }  


也许大家在自己的应用程序中,已经修改过以上代码,但估计也有不少的如我一般的糊涂虫。原封不动的copy了!那么问题就出现了,我做的是安卓的文件断点续传。测试结果,总有那么一个文件传不到服务器。原因就在这个while上,while的条件直接是读取下一条是否成功,如果成功了,那么此刻游标的位置就在下一条数据的位置上了,那么就造成了永远读取不到第一条数据。
本来这个问题无可厚非,大家估计调试后都改正过来了,但是网上竟然是铺天盖地,以讹传讹,看了十几篇文章中,竟然没有一篇改正过来的。虽然很赞同大家的不吝分享,但是这种风气毕竟是不好的。
贴上修改后代码:

/** 
     * 查询列表 
     * @throws Exception 
     */  
    public void selectList()throws Exception{  
        DBHelper dbHelper = new DBHelper(this.getContext());  
        dbHelper.open();  
        Cursor returnCursor = dbHelper.findList("user",new String[] {"id","username", "password"}, "username = 'test'", null,null, null, "id desc");  
       if(returnCursor.moveToFirst())
       {
         do{  
            String id = returnCursor.getString(returnCursor.getColumnIndexOrThrow("id"));  
            String username = returnCursor.getString(returnCursor.getColumnIndexOrThrow("username"));  
            String password = returnCursor.getString(returnCursor.getColumnIndexOrThrow("password"));  
           } while(returnCursor.moveToNext());
       }  
    }  


 

上面代码中没有返回值,仅作演示使用!


其实文章转载出现错误没有什么可计较的,但是希望大家在发现错误后及时修改自己的原创博客,养成良好的风气。对自己的博客负责,从自身做起!

 

转载于:https://www.cnblogs.com/majier/archive/2012/09/25/2781804.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值