开发者

android sqlite union, how to retrive results from the tables?

开发者 https://www.devze.com 2023-02-10 16:41 出处:网络
I have this SQLite query: Cursor orgiCursor = orgiDb.rawQuery(\"select task,taskDate from meeting where exists

I have this SQLite query:

Cursor orgiCursor = orgiDb.rawQuery("select task,taskDate from meeting where exists 
(select `meeting.taskId from Task, meeting where meeting.taskId = Task.taskId and task.name='"+profName+"')
UNION  SELECT subject,date from taskDeadline where exists (select taskDeadline.taskId from taskDeadline,Task where taskDeadline.taskId=Task.taskId and Task.name='"+profName+"') union select 
specificOccassion,setDate from AnnualTask where exists (select taskId from Task,AnnualTask where Task.taskId = AnnualTask.taskId and Task.name = '"+profName+"' and AnnualTask.setDate<>'--')", null);

It's quite long really, but it works flawlessly on SQLite Manager. My problem is that I get an error in retrieving the data. Here is a snippet of my code:

try {
    if (orgiCursor != null ) {
        if (orgiCursor.moveToFirst()) {
            do {
                String annualTask = orgiCursor.getString(orgiCursor.getColumnIndex("specificOccassion"));
                String annualDate = orgiCursor.getString(orgiCursor.getColumnIndex("setDate"));
                String meetingTask = orgiCursor.getString(orgiCursor.getColumnIndex("task"));
                String meetingDate = orgiCursor.getString(orgiCursor.getColumnIndex("taskDate"));
                String deadlineTask = orgiCursor.getString(orgiCursor.getColumnIndex("subject"));
                String deadlineDate = orgiCursor.getString(orgiCursor.getColumnIndex("date"));

                SearchResults sr2=new SearchResults();
                sr2.setName(meetingTask);
                sr2.settDate(meetingDate);
                results.add(sr2);

                SearchResults sr3 = new SearchResults();
                sr3.setName(deadlineTask);
                sr3.settDate(deadlineDate);
                results.add(sr3);

                SearchResults开发者_如何学JAVA sr1 = new SearchResults();
                sr1.setName(annualTask);
                sr1.settDate(annualDate);
                Log.i("Home","added"+annualTask + " & " +annualDate);
                results.add(sr1);
            } while (orgiCursor.moveToNext());
        }
    }
} catch (SQLiteException se) {
    displayError1(se.toString());
    Log.e(getClass().getSimpleName(), se.toString());
} finally {
    if (orgiDb != null) {
        orgiDb.close();
        Log.e(getClass().getSimpleName(),"orgiDB closed()");
    }
}


    return results;

It returns this error. :( Please help me.

02-12 19:28:00.809: INFO/Database(14515): sqlite returned: error code = 1, msg = ambiguous column name: taskId
02-12 19:28:00.849: ERROR/AndroidRuntime(14515): Caused by: android.database.sqlite.SQLiteException: ambiguous column name: taskId: , while compiling: select task,taskDate from meeting where exists (select meeting.taskId from Task, meeting where meeting.taskId = Task.taskId and task.name='Maria Ozawa')UNION  SELECT subject,date from taskDeadline where exists (select taskDeadline.taskId from taskDeadline,Task where taskDeadline.taskId=Task.taskId and Task.name='Maria Ozawa') union select specificOccassion,setDate from AnnualTask where exists (select taskId from Task,AnnualTask where Task.taskId = AnnualTask.taskId and Task.name = 'Maria Ozawa' and AnnualTask.setDate<>'--')

02-12 19:28:01.059: ERROR/Database(14515): close() was never explicitly called on database '/data/data/orgy.organizer.home/databases/orgi' 
02-12 19:28:01.059: ERROR/Database(14515): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here

Any help would be greatly appreciated... :( So my questions are:

1. Which part of the code do I exactly need to close the connection/cursor?

2. Why does Eclipse give me this error? I tried running this same statement in SQLite Manager and it gave me flawless results.


it seems the error is in the query, the fact it does not close the cursor is because he does not compute the query correctly i think

try to use alias when you use the attribute taskId, you use it many times and maybe it confuses the compiler

hope this helped!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号