如何在Android SQLite中选择一些后选择剩余的行?

时间:2015-07-01 05:15:07

标签: android mysql sqlite

我有一个任务,我必须在一个选项卡中显示使用查询选择的行,而在另一个选项卡中显示剩余的行。我已使用SimpleCursorAdapter显示所选行。当我试图在下一个标签中显示剩余的行时,它会抛出错误。我尝试了NOT IN,但它也没有用。我尝试了NOT EXISTS它也显示了所有行。谁能回答请帮助。我在这里发布了我的代码。提前致谢。 这是显示所选行的第一个选项卡的活动

public class OnlineDevices extends Activity {
ListView listOnline;
DatabaseHelper databaseHelper;
String count;
int conut;
TextView tvOnlineCount;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_online_devices);

    listOnline=(ListView)findViewById(R.id.listView3);
    tvOnlineCount = (TextView) findViewById(R.id.textView59);

    databaseHelper=new DatabaseHelper(this);
    SQLiteDatabase db=databaseHelper.getReadableDatabase();

    String date= DateFormat.getDateInstance().format(new Date());
    String statusQuery="select rowid _id, deviceContact from statusTable where sentTime='"+date+"'";
    Cursor cursor1=db.rawQuery(statusQuery,null);

    if (cursor1.getCount()>0){
        while (cursor1.moveToNext()){
            String deviceNo=cursor1.getString(cursor1.getColumnIndex("deviceContact"));

            String device=deviceNo.substring(2, 12);

            String query="select rowid _id, userName, uSerialNo from bikeTable where uContactNo='"+device+"' AND userName IS NOT NULL";
            Cursor cursor2=db.rawQuery(query, null);
            SimpleCursorAdapter adapter=new SimpleCursorAdapter(this,R.layout.status_item,cursor2,new String[]{"userName","uSerialNo"},new int[]{R.id.textView51,R.id.textView52});
            listOnline.setAdapter(adapter);
        }
    }
    try {
        conut = listOnline.getAdapter().getCount();
        count = String.valueOf(conut);
        tvOnlineCount.setText(count);
    }catch (Exception e){
        e.printStackTrace();
        int i=0;
        Toast.makeText(OnlineDevices.this,"No device is active",Toast.LENGTH_SHORT).show();
        tvOnlineCount.setText(String.valueOf(i));
    }
}

}

显示剩余行的第二个标签的活动是

public class OfflineDevices extends Activity {

ListView listOffline;
DatabaseHelper databaseHelper;
String deviceContact;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_offline_devices);

    listOffline=(ListView)findViewById(R.id.listView4);

    databaseHelper=new DatabaseHelper(this);
    SQLiteDatabase db=databaseHelper.getReadableDatabase();

    String date= DateFormat.getDateInstance().format(new Date());
    String query="select rowid _id, deviceContact from statusTable where sentTime='"+date+"'";
    Cursor cursor1=db.rawQuery(query,null);

    if (cursor1.getCount()>0){
        while (cursor1.moveToNext()) {
            deviceContact = cursor1.getString(cursor1.getColumnIndex("deviceContact"));
        }
            String device=deviceContact.substring(2, 12);
            String query2="select rowid _id, userName, uSerialNo from bikeTable where userName IS NOT NULL AND uContactNo='"+device+"'";
            Cursor cursor3=db.rawQuery(query2,null);
            String query1="select rowid _id,*from bikeTable where userName IS NOT NULL NOT IN('"+query2+"')";
            Cursor cursor2=db.rawQuery(query1,null);
            SimpleCursorAdapter adapter=new SimpleCursorAdapter(this,R.layout.status_item,cursor2,new String[]{"userName","uSerialNo"},new int[]{R.id.textView51,R.id.textView52});
            listOffline.setAdapter(adapter);


    }
}

}

1 个答案:

答案 0 :(得分:0)

您的查询中存在语法错误。从理论上讲,以下内容应该有效:

String query2 = "SELECT userName FROM bikeTable WHERE userName IS NOT NULL "
        + "AND uContactNo = '" + device + "'";
String query1 = "SELECT * FROM bikeTable WHERE userName IS NOT NULL "
        + "AND userName NOT IN(" + query2 + ")";

以下是不同之处:

  1. 您的原始代码在括号内的query2附近有单引号。这使得SQLite将其视为字符串文字,而不是内部查询。
  2. 由于在query2表达式中使用NOT IN,因此需要。{li> 有一个结果列,似乎userName就是那个 你有兴趣。
  3. 我建议花一些时间浏览SQLite language页面。我相当肯定您可以使用具有JOIN的单个查询实际获得所需的结果,而不是进行一次查询,检查结果,然后进行另一次查询。

    顺便说一句,它被认为是在Android中从后台线程上的数据库加载数据的最佳做法。通常,这是使用Loader framework完成的。你完成它们时也应该关闭游标(不是你给适配器的游标,而是你用来检查在线设备的游标)。