【问题标题】:How can I get last ID after inserting record?插入记录后如何获取最后一个 ID?
【发布时间】:2013-06-14 06:52:35
【问题描述】:

给定以下代码:

 sqLiteDatabase.execSQL("create table if not exists ACCOUNT (ID integer primary key autoincrement," +
                        "CASH LONG NOT NULL," +
                        "BANKID INTEGER NOT NULL," +
                        "ACCOUNTNO TEXT ," +
                        "DATE TEXT NOT NULL," +
                        "COMMENT TEXT);");

在 SQLite 中创建了一个表。

添加记录后如何获取lastID?

使用以下代码:

 contentValues.put("CASH", accountTO.getCash());
 contentValues.put("DATA", DatePro.currentDate());
 contentValues.put("BANKID", accountTO.getBankID());
 contentValues.put("ACCOUNTNO", accountTO.getAccountNo());
 contentValues.put("COMMENT", accountTO.getComment());

 Long i = sqLiteDatabase.insert("ACCOUNT", null, contentValues);

我收到-1

【问题讨论】:

标签: java android sqlite insert


【解决方案1】:
public long insert (String table, String nullColumnHack, ContentValues values)

返回 新插入行的rowID,如果发生错误,则为-1

【讨论】:

    猜你喜欢
    • 2018-11-06
    • 1970-01-01
    • 2013-05-02
    • 1970-01-01
    • 1970-01-01
    • 2019-01-27
    • 2012-01-25
    • 2023-03-16
    • 2011-04-30
    相关资源
    最近更新 更多