sqlite - Insert the newset value in a table and drop the old ones -
i have method saves somes records machine in table. however, may insert accident multiple times records same machine in day, want day save newest insert , drop old ones. thought 1 solution:
string sq = "select date, idmachine " + "from machines_records date = ? , idmachine = ?"; try { class.forname(typedb); c = drivermanager.getconnection(path); stm = c.preparestatement(sq); resultset rs = stm.executequery(); if (rs.next()) { { //call method drop value } while(rs.next()); }else { //call method insert value } }catch (sqlexception e) { system.out.println(e.getmessage()); } { if (stm != null) stm.close(); if (c != null) c.close(); }
is there better/smartest solution this, can make theses actions in same method?
with unique index (possibly primary key) on date
, idmachine
use insert ... on duplicate key update
, insert when there no entry machine on day or update existing record new data otherwise.
insert machines_records (machineid, date, data) values (@id, @date, @data) on duplicate key update data = @data;
edit: see removed mysql tag. want answer sqlite. in sqlite you'd use insert or replace
:
insert or replace machines_records (machineid, date, data) values (@id, @date, @data);
Comments
Post a Comment