I can add records with the same ID's which i want to stop... I want an error message if the ID already exists... I've tried to make it a primary key but didn't work out all to well... I've also tried some other stuff, but nothing seems to work.. see code below, everthing works except for the check if id already exist when adding new record Code:
public class MainActivity extends ActionBarActivity implements View.OnClickListener {
EditText editId,editName;
Button btnAdd,btnDelete,btnModify,btnView,btnViewAll,btnShowInfo;
SQLiteDatabase db;
/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editId =(EditText)findViewById(R.id.editRollno);
editName=(EditText)findViewById(R.id.editName);
btnAdd=(Button)findViewById(R.id.btnAdd);
btnDelete=(Button)findViewById(R.id.btnDelete);
btnModify=(Button)findViewById(R.id.btnModify);
btnView=(Button)findViewById(R.id.btnView);
btnViewAll=(Button)findViewById(R.id.btnViewAll);
btnShowInfo=(Button)findViewById(R.id.btnShowInfo);
btnAdd.setOnClickListener(this);
btnDelete.setOnClickListener(this);
btnModify.setOnClickListener(this);
btnView.setOnClickListener(this);
btnViewAll.setOnClickListener(this);
btnShowInfo.setOnClickListener(this);
db=openOrCreateDatabase("Top2000", Context.MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS artist(artistid VARCHAR,name VARCHAR);");
}
public void onClick(View view) {
if(view==btnAdd) {
if(editId.getText().toString().trim().length()==0||
editName.getText().toString().trim().length()==0) {
showMessage("Error", "Please enter all values");
return;
}
db.execSQL("INSERT INTO artist VALUES('"+ editId.getText()+"','"+editName.getText()+"');");
showMessage("Success", "Artist added");
clearText();
}
if(view==btnDelete) {
if(editId.getText().toString().trim().length()==0) {
showMessage("Error", "Please enter ArtistId");
return;
}
Cursor c=db.rawQuery("SELECT * FROM artist WHERE artistid='"+ editId.getText()+"'", null);
if(c.moveToFirst()) {
db.execSQL("DELETE FROM artist WHERE artistid='"+ editId.getText()+"'");
showMessage("Success", "Record Deleted");
} else {
showMessage("Error", "Invalid ArtistId");
}
clearText();
}
if(view==btnModify) {
if(editId.getText().toString().trim().length()==0) {
showMessage("Error", "Please enter ArtistId");
return;
}
Cursor c=db.rawQuery("SELECT * FROM artist WHERE artistid='"+ editId.getText()+"'", null);
if(c.moveToFirst()) {
db.execSQL("UPDATE artist SET name='"+editName.getText()+"' WHERE artistid='"+ editId.getText()+"'");
showMessage("Success", "Record Modified");
} else {
showMessage("Error", "Invalid ArtistId");
}
clearText();
}
if(view==btnView) {
if(editId.getText().toString().trim().length()==0) {
showMessage("Error", "Please enter ArtistId");
return;
}
Cursor c=db.rawQuery("SELECT * FROM artist WHERE artistid='"+ editId.getText()+"'", null);
if(c.moveToFirst()) {
editName.setText(c.getString(1));
} else {
showMessage("Error", "Invalid ArtistId");
clearText();
}
}
if(view==btnViewAll) {
Cursor c=db.rawQuery("SELECT * FROM artist", null);
if(c.getCount()==0) {
showMessage("Error", "No records found");
return;
}
StringBuffer buffer=new StringBuffer();
while(c.moveToNext()) {
buffer.append("ArtistId: "+c.getString(0)+"\n");
buffer.append("Name: "+c.getString(1)+"\n");
}
showMessage("artist Details", buffer.toString());
}
if(view==btnShowInfo) {
showMessage("artist detail app", "Made by Mike");
}
}
Aucun commentaire:
Enregistrer un commentaire