简体   繁体   中英

Is there a way to retreitve a row from an sqlite database in android studio, and put the row into an object?

I know that you can insert into a database, by getting all the data from an object, and putting them into contentvalues, and using that to insert into the database. But I'm wondering if there's something like that in reverse? Where you can retrieve a row from a database, and easily put it into an object?

Here's a quick way to answer your own question. Please don't misinterpret my expediency: I would love to design an app that solves your problems but am a student and want a job myself. It would be a very time consuming proposition for me because I don't have it ready yet, though it does appear to be a common feature of web scrapers. Also, it depends what you mean by "object," since that term seems to encompass a broad variety of, well, objects.

https://docs.oracle.com/en/java/

https://docs.oracle.com/javase/8/docs/technotes/guides/jdbc/

Please find the time to give me some remote career advice.

Of course there is. However not directly but basically the reverse of saving from an object.

That is you extract the data from the database using a SELECT query (as opposed to an INSERT ) into a Cursor (an object that holds the data but not your object).

The Cursor holds (buffers) rows that correspond to the SELECT ed data. You move to the respective row and retrieve the data from that row.

Here's an example of a method that returns a FlightsModel object from an answer provided recently:-

@SuppressLint("Range") // suppress Bug/issue with getColumnIndex
public FlightsModel getFlightById(int id) {
    FlightsModel rv;
    SQLiteDatabase db = this.getWritableDatabase();
    // Uses the query convenience method rather than raw query
    Cursor csr = db.query(FLIGHTS,null,COLUMN_ID+"=?",new String[]{String.valueOf(id)},null,null,null);
    if (csr.moveToFirst()) {
        rv = new FlightsModel(
                csr.getInt(csr.getColumnIndex(COLUMN_ID)),
                csr.getString(csr.getColumnIndex(COLUMN_DESTINATION)),
                csr.getDouble(csr.getColumnIndex(COLUMN_PRICE)),
                csr.getString(csr.getColumnIndex(COLUMN_DEPARTURE_TIME)),
                csr.getString(csr.getColumnIndex(COLUMN_ARRIVAL_TIME)),
                csr.getString(csr.getColumnIndex(COLUMN_DURATION)),
                csr.getInt(csr.getColumnIndex(COLUMN_AVAILABLE_SEATS))
        );
    }  else {
        rv = new FlightsModel();
    }
    csr.close();
    // No need to close the database (inefficient to keep opening and clsoing db)
    return rv;
}

The method is passed an int which is sufficient to identity the data, as it uniquely identifies the row that holds the data.

The SELECT query is built via the convenience query method (it would be SELECT * FROM flights_resolved WHERE column_id_resolved=the_passed_int ).

  • note that
    • flights_resolved is whatever the variable FLIGHTS resolves to.
    • column_id_resolved is whatever the variable COLUMN_ID resolves to.
    • the_passed_int is whatever the value of the int id is, which is passed to the method.
      • note that the value is actually bound ie it safely replaces the? with the number. By safely, it means that SQL injection is prevented.

The query method returns theCursor .

An attempt is made to move to the first row in theCursor (in this case only a single row is expected) using theCursor moveToFirst method.

If there is a row then the Flights model is built by extract the values according to their position (index) in the row. The position/index being calculated/returned by the Cursor getColumnIndex method which takes the column name as the argument.

If no row is returned then an empty FlightsModel object is built instead.

The resultant instantiated FlightsModel object is then returned to the caller. The caller would be expected to check the returned object and handle the situation of the empty FlightsModel.

Android also has Room which provides an abstract layer over SQLite and uses an object orientated approach.

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