简体   繁体   中英

SQLite query - Stop returning rows from database once a specific item has been found

I am creating an Android app to help actors rehearse from a script. The user can select a character from the script to rehearse as. The script will then be shown to the user up until his selected character's first line. When the user decides to proceed, their current line will appear, as well as the rest of the script up to their next line. The user can proceed again and so on.

I have the whole play stored in an SQLite database and I populate custom ListViews by executing queries on the database.

My question is how can we execute a query on the database, but stop returning items once we have found a certain item (eg the character's name)? I store the results into a Cursor then populate the ListView with the following method:

    private void fillData() {

    mFrom = new String[] { PlayDbAdapter.KEY_CHARACTER,
            PlayDbAdapter.KEY_LINE };
    mTo = new int[] { R.id.textCharacter, R.id.textLine };

    // Now create an array adapter and set it to display using our row
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
            R.layout.play_list_layout, mCursor, mFrom, mTo);

    setListAdapter(adapter);
}

If you require anymore information or code, please let me know.

Thank you!

It's far more efficient to return chunks of the script in a Cursor and then search the Cursor for the actor's first line.

Another way to do it would be to return line "context" for a character. Have the database store an index into the lines. Return a Cursor containing only the character's lines, then do a second query that returns 10 lines before and after the character's lines.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM