dimanche 1 février 2015

creating a primary key for already existing code

here is my code... i am trying to create primary key... pls anyone help me...



package com.example.login;

import android.app.Activity;
public class Addstudents extends Activity implements OnClickListener {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_addstudents);

Button save = (Button) findViewById(R.id.less60);
save.setOnClickListener(this);

Button clear = (Button) findViewById(R.id.less75);
clear.setOnClickListener(this);

Button back = (Button) findViewById(R.id.less85);
back.setOnClickListener(this);


SQLiteDatabase db;
db = openOrCreateDatabase( "Student.db" , SQLiteDatabase.CREATE_IF_NECESSARY , null );
try {
final String StudentDetails = "CREATE TABLE IF NOT EXISTS StudentDetails ("
+ "REG_NO,"
+ "ROLL_NO,"
+ "NAME,"
+ "SEM,"
+ "CONTACT_NO);";
db.execSQL(StudentDetails);

// Toast.makeText(Addstudents.this, "table created ", Toast.LENGTH_LONG).show();



}
catch (Exception e) {
Toast.makeText(Addstudents.this, "ERROR "+e.toString(), Toast.LENGTH_LONG).show();
}
}

public void onClick(View view){

SQLiteDatabase db;


if(view.getId()==R.id.less60)
{
try
{
EditText e1 = (EditText) findViewById(R.id.code1);

EditText e2 = (EditText) findViewById(R.id.rollno);

EditText e3 = (EditText) findViewById(R.id.name1);
String s3=e3.getText().toString();

EditText e4 = (EditText) findViewById(R.id.editText2);

EditText e5 = (EditText) findViewById(R.id.phno);

String sql =
"INSERT or replace INTO StudentDetails (" +
"Reg_no, " +
"Roll_no, " +
"Name, " +
"Sem,"+
"Contact_no)"+
"VALUES('"+e1+"','"+e2+"','"+s3+"','"+e4+"','"+e5+"')" ;

db = openOrCreateDatabase( "Student.db" , SQLiteDatabase.CREATE_IF_NECESSARY , null );

db.execSQL(sql);

}
catch (Exception e) {
Toast.makeText(Addstudents.this, "ERROR "+e.toString(), Toast.LENGTH_LONG).show();
}
AlertDialog alertDialog = new AlertDialog.Builder(Addstudents.this).create();
alertDialog.setTitle("Alert Dialog");

alertDialog.setMessage("students details have successfully added to the datebase");

alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to execute after dialog closed
// Toast.makeText(getApplicationContext(), "You clicked on OK", Toast.LENGTH_SHORT).show();
}
});
alertDialog.show();


}
else if(view.getId()==R.id.less75)
{
Intent stu = new Intent(this, Addstudents.class);
startActivity(stu);

}
else if(view.getId()==R.id.less85)
{
Intent att = new Intent(this, Attendence.class);
startActivity(att);

}
}

}


please anyon help me to create primary key for regno and rollno


Aucun commentaire:

Enregistrer un commentaire