android - Insert sqlite record if not exists, with a unique constraint -


i have table this:

create table students(     firstname varchar(64),      lastname varchar(64),      essay longtext,     unique(firstname, lastname) on conflict replace); 

i want insert record - if exists, want update it:

string firstname = "bob"; string lastname = "smith"; string essay = "foo";  contentvalues values = new contentvalues(); values.put("firstname", firstname); values.put("lastname", lastname); values.put("essay", essay);  sqlitedatabase db = ...; db.insert("students", null, values); 

because i'm using "on conflict replace", can rely on insert() method take care of both cases: (1) student entry not exist, insert (2) student entry exists, overwrite it.

thanks

you must read documentation:

the algorithm specified in or clause of insert or update overrides algorithm specified in create table. if no algorithm specified anywhere, abort algorithm used.

from sql understood sqlite: on conflict clause


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

php - joomla get content in onBeforeCompileHead function -