user2049371 user2049371 - 1 year ago 127
Java Question

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
.

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

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download