user2049371
user2049371

Reputation: 199

How can I get last ID after inserting record?

Given the following code:

 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);");

A table is created in SQLite.

How can I get a lastID after adding a record?

With the following code:

 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);

I get -1.

Upvotes: 2

Views: 1431

Answers (1)

JaveZh
JaveZh

Reputation: 184

public long insert (String table, String nullColumnHack, ContentValues values)

Returns the rowID of the newly inserted row, or -1 if an error occurred.

Upvotes: 6

Related Questions