Android SQLite add duplicate item -



Android SQLite add duplicate item -

i working on sqllite. i've created database successfully, , can add together new items it.

// adding new contact public void add_contact(contact contact) { sqlitedatabase db = this.getwritabledatabase(); contentvalues values = new contentvalues(); values.put(key_tittle, contact.gettitle()); // contact title values.put(key_description, contact.getdescription()); // contact // description values.put(key_price, contact.getprice()); // contact cost values.put(key_image, contact.getimage()); // contact image values.put(key_counter, contact.getcounter()); // contact counter // inserting row db.insert(table_contacts, null, values); log.e("table result isss", string.valueof(values)); db.close(); // closing database connection }

this code working perfect, want check if can save same information, example, if first save "hello android" , in sec time seek save same information, want show example: toast message or other.

first create function this:

public boolean somethingexists(string x) { cursor cursor = database.rawquery("select 1 " + table_contacts + " key_tittle '%" + x + "%'", null); boolean exists = (cursor.getcount() > 0); cursor.close(); homecoming exists; }

and before every insert phone call funcition homecoming if exists or not this:

if (!somethingexists("hello world")) { //here insertion if not in table e.g phone call addcontact function here insertion etc } else { //display toast message here "record exists" }

p.s wanted highlight logic - actual table names, schema & logic might different. anyways, seek - should work. working supposed follow database constraints, construction , schema of design.

android sqlite

Comments

Popular posts from this blog

model view controller - MVC Rails Planning -

ruby on rails - Devise Logout Error in RoR -

html - Submenu setup with jquery and effect 'fold' -