0

I have 2 columns in sqlite which are name and date,name is already set it to unique to avoid duplicate entry but my problem is I want to insert the same record for column 'name' but different record in column 'date'. I'm also using this to filter the duplicate record and it worked.

try {
    dbOpenHelperss.insertOrThrow("table", values);
} catch (SQLiteConstraintException e)
    //to detect if there is a duplicated item
}

For example in column_name='Peter Xavier' column_date='02/15/14 and i want to insert record of Peter Xavier with different date like column_name ='Peter Xavier' , column_date='02/16/14

What I tried so far is

if(!"column_date".equals(colum_name)) {
    //to compare two columns are not the same then insert                               
    ContentValues values = new ContentValues();
    values.put("column_name",name );
    values.put("column_date", date);

    try {
        dbOpenHelperss.insertOrThrow("table", values);
    } catch (SQLiteConstraintException e) { 
        // to detect if there is a duplicate entry
    }
}
Ashley Medway
  • 7,151
  • 7
  • 49
  • 71
nEwbie
  • 96
  • 1
  • 7
  • 1
    remove the `unique` of `name` column. And use new column for avoid duplicates. ex: id. – Gunaseelan Feb 15 '14 at 06:04
  • as Gunaseelan said..if you want to insert duplicate value in name column then why you define it unique ??just remove unique constraint from name column .. – Ranjit Feb 15 '14 at 06:09
  • thanks for the reply, what i mean is i will not be able to insert record if column_name and column_date are the same..i will be able to insert if column_name and column date are different – nEwbie Feb 15 '14 at 06:15

1 Answers1

2

Add Composite key.that means column_name and column_date together work as a key

Unique (column_name, column_date) ,

user2771059
  • 298
  • 1
  • 4
  • 12
  • thanks, i will try this now, im searching for an hour but no luck,ill post here if this work – nEwbie Feb 15 '14 at 08:16
  • unfortunately i cannot add a primary key =( i hope there is a work around – nEwbie Feb 15 '14 at 09:31
  • i created a new table because the sqlite does not have a function of alter, so basically i created a new one but there is an error when i use ADD PRIMARY KEY (col1,col2) it says cannot add one or more primary key – nEwbie Feb 15 '14 at 10:36
  • http://stackoverflow.com/questions/734689/sqlite-primary-key-on-multiple-columns check this out – user2771059 Feb 15 '14 at 11:48
  • it does not work when the _id is a primary key.. so what i did was create table mytable (_id INTEGER,name TEXT,date TEXT, PRIMARY KEY (name,date)) is this okay it doesnt matter if the _id is not a primary key? anyway thank you for your time sorry i cannot upvote due to low reputation.. – nEwbie Feb 15 '14 at 12:01
  • in ur statement date and name should be not null.have u done it – user2771059 Feb 15 '14 at 13:07
  • i did it like this create table table(_id INTEGER,name TEXT NOT NULL,date TEXT not null, PRIMARY KEY(_id,name,date)); it insert duplicate item.. but when i remove the primary key of _id.. the name and date are work together or should _id also not null? – nEwbie Feb 15 '14 at 13:49
  • u should keep _id not null – user2771059 Feb 16 '14 at 06:45