简体   繁体   English

有没有办法从 android 工作室中的 sqlite 数据库中检索一行,并将该行放入 object 中?

[英]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.我知道您可以插入数据库,方法是从 object 获取所有数据,并将它们放入 contentvalues,然后使用它插入数据库。 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?在哪里可以从数据库中检索一行,并轻松地将其放入 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.对我来说这将是一个非常耗时的提议,因为我还没有准备好它,尽管它似乎是 web 刮刀的共同特征。 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/en/java/

https://docs.oracle.com/javase/8/docs/technotes/guides/jdbc/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.但是不是直接的,而是基本上与从 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).也就是说,您使用SELECT查询(而不是INSERT )从数据库中提取数据到Cursor (一个 object 保存数据但不是您的对象)。

The Cursor holds (buffers) rows that correspond to the SELECT ed data. Cursor 保存(缓冲)对应于SELECT ed 数据的行。 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:-这是从最近提供的答案中返回 FlightModel object 的方法示例:-

@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.该方法传递了一个足以标识数据的 int,因为它唯一地标识了保存数据的行。

The SELECT query is built via the convenience query method (it would be SELECT * FROM flights_resolved WHERE column_id_resolved=the_passed_int ). SELECT查询是通过便捷查询方法构建的(它将是SELECT * FROM flights_resolved WHERE column_id_resolved=the_passed_int )。

  • note that注意
    • flights_resolved is whatever the variable FLIGHTS resolves to. flight_resolved 是变量 FLIGHTS 解析为的任何内容。
    • column_id_resolved is whatever the variable COLUMN_ID resolves to. column_id_resolved 是变量 COLUMN_ID 解析为的任何内容。
    • the_passed_int is whatever the value of the int id is, which is passed to the method. the_passed_int 是传递给方法的 int id的值。
      • note that the value is actually bound ie it safely replaces the?请注意,该值实际上是绑定的,即它安全地替换了? with the number.与号码。 By safely, it means that SQL injection is prevented.通过安全,这意味着防止了 SQL 注入。

The query method returns theCursor .查询方法返回Cursor

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.尝试使用Cursor moveToFirst方法移动到Cursor中的第一行(在这种情况下,预计只有一行)。

If there is a row then the Flights model is built by extract the values according to their position (index) in the row.如果有一行,则通过根据行中的 position(索引)提取值来构建航班 model。 The position/index being calculated/returned by the Cursor getColumnIndex method which takes the column name as the argument.由 Cursor getColumnIndex方法计算/返回的位置/索引,该方法将列名作为参数。

If no row is returned then an empty FlightsModel object is built instead.如果未返回任何行,则改为构建空的 FlightModel object。

The resultant instantiated FlightsModel object is then returned to the caller.然后将生成的实例化的 FlightModel object 返回给调用者。 The caller would be expected to check the returned object and handle the situation of the empty FlightsModel.调用者应检查返回的 object 并处理空 FlightModel 的情况。

Android also has Room which provides an abstract layer over SQLite and uses an object orientated approach. Android 还具有Room ,它在 SQLite 上提供抽象层,并使用 object 面向方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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