Reputation: 460
In my android application, when i insert repeated values it doesnt show in the database, essentially what i am doing is splitting the srting in 2 halves and inserting one string in one column by insertdata() and second string in second column by insertdata1() and following is my code
main
public void AddData() {
btnAddData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Spinner dropdown = findViewById(R.id.spinner1);
Spinner dropdown1 = findViewById(R.id.spinner2);
Spinner dropdown2 = findViewById(R.id.spinner3);
Spinner dropdown3 = findViewById(R.id.spinner4);
String join = "-";
String ConcatedString = dropdown.getSelectedItem().toString().concat(join).concat(dropdown1.getSelectedItem().toString());
String ConcatedString1 = dropdown2.getSelectedItem().toString().concat(join).concat(dropdown3.getSelectedItem().toString());
TextView h = (TextView) findViewById(R.id.text_view_id);
h.setText(ConcatedString);
TextView h1 = (TextView) findViewById(R.id.text_view_id1);
h1.setText(ConcatedString1);
String search = h.getText().toString();
myDb.insertData(h.getText().toString());
myDb.insertData1(h1.getText().toString());
Toast.makeText(MainActivity.this,"showing"+search,Toast.LENGTH_LONG).show();
}
}
);
}
Database helper
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME + " ( name VARCHAR UNIQUE, name1 VARCHAR UNIQUE)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public void insertData(String name) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2,name);
long result = db.insert(TABLE_NAME,null ,contentValues);
}
public void insertData1(String name1) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_3,name1);
long result = db.insert(TABLE_NAME,null ,contentValues);
}
public Cursor getAllData() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from " + TABLE_NAME, null);
return res;
}
}
Upvotes: 0
Views: 43
Reputation: 929
it's written in your table description:
db.execSQL("create table " + TABLE_NAME + " ( name VARCHAR UNIQUE, name1 VARCHAR UNIQUE)");
Your two columns are uniques, so they don't accept repeated values. You should remove UNIQUE for those two columns and add a ID column wich will be auto generated.
db.execSQL("CREATE TABLE " + TABLE_NAME + " (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, name VARCHAR, name1 VARCHAR)");
You 've got an error in your insert methods. You can't insert into COL_1 or COL_3 because they are not your columns names. You should write this =>
In your first method =>
contentValues.put("name",name);
In your second method =>
contentValues.put("name1",name1);
And why don't you write only one method to insert data ? This way you should have only one line in your database. But maybe that's what you want ? I would write this method :
public void insertData(String name, String name1) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("name",name);
contentValues.put("name1",name1);
long result = db.insert(TABLE_NAME,null ,contentValues);
}
Upvotes: 2
Reputation: 680
In your Database helper class modify onCreate method. Remove UNIQUE in name & name1. if your column is UNIQUE, you can't insert same value twice. Create a ID column to give a primary key and auto increment ID. code should be like below.
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME + " ( ID INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, name1 TEXT)");
}
Upvotes: 0