简体   繁体   English

如何从sqlite,android获取插入行的详细信息

[英]How to get details of the inserted row from sqlite, android

I have created a table (Detail) with two columns one column (ID) is primary and auto-increment while the other column (EmpName) is varchar type. 我创建了一个具有两列的表(Detail),其中一列(ID)是主列并且自动递增,而另一列(EmpName)是varchar类型。 While inserting data in database i am only inserting data in EmpName column using the following code. 在数据库中插入数据时,我仅使用以下代码在EmpName列中插入数据。 But I want to know the details of that record (ID and name but i know only name and ID is created by database). 但是我想知道该记录的详细信息(ID和名称,但我只知道名称和ID由数据库创建)。 I can write another select statement and get the last record of the cursor after executing the insert query but if another insertion will be done before executing the select statements then my database will give me wrong result. 我可以编写另一个select语句,并在执行插入查询后获取游标的最后一条记录,但是如果在执行select语句之前要进行另一次插入,则我的数据库将给我错误的结果。 How can i get the details? 我如何获得详细信息?

ContentValues initialvalues = new ContentValues();
initialvalues.put("EmpName", "John");
db.insert("Detail", null, initialvalues);// db is database

The above code is working fine for insertion of data in database but How to get that details? 上面的代码可以很好地将数据插入数据库,但是如何获取详细信息?

please the API:SQLiteStatement.executeInsert() 请使用API​​:SQLiteStatement.executeInsert()

According to the source code: 根据源代码:

/**
     * Execute this SQL statement and return the ID of the row inserted due to this call.
     * The SQL statement should be an INSERT for this to be a useful call.
     *
     * @return the row ID of the last row inserted, if this insert is successful. -1 otherwise.
     *
     * @throws android.database.SQLException If the SQL string is invalid for
     *         some reason
     */
    public long executeInsert() {
        BlockGuard.getThreadPolicy().onWriteToDisk();
        if (!mDatabase.isOpen()) {
            throw new IllegalStateException("database " + mDatabase.getPath() + " already closed");
        }
        long timeStart = SystemClock.uptimeMillis();
        mDatabase.lock();

        acquireReference();
        try {
            native_execute();
            mDatabase.logTimeStat(mSql, timeStart);
            return (mDatabase.lastChangeCount() > 0) ? mDatabase.lastInsertRow() : -1;
        } finally {
            releaseReference();
            mDatabase.unlock();
        }
    }

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

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