android获取数据库查询的结果

天下之事常成于困约,而败于奢靡。这篇文章主要讲述android获取数据库查询的结果相关的知识,希望能为你提供帮助。
参考:https://www.xuebuyuan.com/3244495.html
原来错误的程序,app会崩溃(请大神指点错误的原因是啥?_(:з)∠ )_)

1 btn_search.setOnClickListener(new View.OnClickListener() { 2@Override 3public void onClick(View v) { 4String key = et_username.getText().toString(); 5Cursor cursor = dbOpenHelper.getReadableDatabase().query("tb_user",null,"username=?",new String[]{key}, null,null,null); 6 7if (cursor.getCount() == 0) { 8Toast.makeText(MainActivity.this,"很遗憾,没有相关记录",Toast.LENGTH_SHORT).show(); 9} else { 10 //while (cursor.moveToNext()) { 11 //Log.i("cccccccccc", cursor.getString(1) + " " + cursor.getString(2)); 12 //} 13Log.i("dddddddd",cursor.getString(1)); 14} 15} 16});

【android获取数据库查询的结果】 
改正后的程序,用循环来输出
1 btn_search.setOnClickListener(new View.OnClickListener() { 2@Override 3public void onClick(View v) { 4String key = et_username.getText().toString(); 5Cursor cursor = dbOpenHelper.getReadableDatabase().query("tb_user",null,"username=?",new String[]{key}, null,null,null); 6 7if (cursor.getCount() == 0) { 8Toast.makeText(MainActivity.this,"很遗憾,没有相关记录",Toast.LENGTH_SHORT).show(); 9} else { 10while (cursor.moveToNext()) { 11Log.i("cccccccccc", cursor.getString(1) + " " + cursor.getString(2)); 12} 13 //Log.i("dddddddd",cursor.getString(1)); 14} 15} 16});

 

    推荐阅读