Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <ol> <li>You're closing your database when returning cursors -- thus the cursor will point to a closed database which means you can't use it (you might be able to use the first record since you moved to that before closing it). Not to mention opening/closing takes up some time. You should just open the DB once in <code>onCreate</code> and then close it once in <code>onDestroy</code>.</li> <li>Android only supports a single SQL statement in <code>rawQuery</code>, so what you want cannot be achieved by nesting SQL statements</li> </ol> <p>Try something like:</p> <pre><code>String sql = "SELECT CityType.rowid, Location.locationName FROM Location, CityType " + "WHERE Location.cityId = CityType.rowid " + "AND CityType.cityName = " + saa; </code></pre> <p>Also please post the error output from LogCat.</p> <p>Change your DatabaseHelper to something like the following (notice I'm not opening the database for each query - you need to make sure to call the <code>open</code> and <code>close</code> methods in the <code>onCreate</code> and <code>onDestroy</code> methods of your Activity).</p> <pre><code>public class DataBaseHelper extends SQLiteOpenHelper { ... private SQLiteDatabase mDb; // change openDatabase to this: // call it ONCE in onCreate of your activity public SQLiteDatabase open() throws SQLException { if (mDb != null) return; // already opened mDb = this.getReadableDatabase(); return mDb; } // call this in the onDestroy of your activity public void close() { if (mDb == null) return; // db is not open mDb.close(); mDb = null; } // Change getLoc and your other methods to be something like this public Cursor getLocation() { if (mDb == null) throw new NullPointerException("Database has not been opened by activity"); String saa = cabbookingapplication.Selection; String sql = "SELECT CityType.rowid, Location.locationName FROM Location, CityType " + "WHERE Location.cityId = CityType.rowid " + "AND CityType.cityName = " + saa; return mDb.rawQuery(sql); } ... } </code></pre> <p>Also</p> <ul> <li>Make sure to call <code>startManagingCursor</code> on any cursors you don't close pretty much straight away to avoid memory leaks (i.e. cursors you use in adapters should be managed)</li> <li>I haven't called <code>Cursor.moveToFirst()</code> in my above example, so call that if you want. </li> </ul> <p>I usually call it outside of my adapter to so I can do something like</p> <pre><code>if (cursor.getCount() &gt; 0) { // we have at least 1 record while (cursor.moveToNext()) { // do something with current row } } else { // no rows, show an empty message or something } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload