如何在android sqlit中添加主键

时间:2015-08-09 15:27:43

标签: android

我创建了一个存储学生记录的数据库。 但是存在重复的问题。 我向不同的学生输入了相同的卷号。 但我希望在以下代码中添加主键。 如何将卷号作为主键。 并且还显示消息"该号码已经存在"

     db=openOrCreateDatabase("StudentDB", Context.MODE_PRIVATE, null);
     db.execSQL("CREATE TABLE IF NOT EXISTS student(rollno VARCHAR,name 
     name VARCHAR,marks VARCHAR);");
     public void onClick(View view)
    { 
         if(view==btnAdd)
    {
        if(editRollno.getText().toString().trim().length()==0||
           editName.getText().toString().trim().length()==0||
           editMarks.getText().toString().trim().length()==0)
        {
            showMessage("Error", "Please enter all values");
            return;
        }
        db.execSQL("INSERT INTO student 
        VALUES('"+editRollno.getText()+"','"+editName.getText()+
               "','"+editMarks.getText()+"');");
        showMessage("Success", "Record added");
        clearText();
      }

        if(view==btnDelete)
       {
       if(editRollno.getText().toString().trim().length()==0)
         {
        showMessage("Error", "Please enter Rollno");
        return;
        }
       Cursor c=db.rawQuery("SELECT * FROM student WHERE
         rollno='"+editRollno.getText()+"'", null);     
       if(c.moveToFirst())
       {
        db.execSQL("DELETE FROM student WHERE 
        rollno='"+editRollno.getText()+"'");  
        showMessage("Success", "Record Deleted");
       }
       else
    {
        showMessage("Error", "Invalid Rollno");
    }
    clearText();
    }
    if(view==btnModify)
      {
      if(editRollno.getText().toString().trim().length()==0)
      {
        showMessage("Error", "Please enter Rollno");
        return;
      }
     Cursor c=db.rawQuery("SELECT * FROM student WHERE   
      rollno='"+editRollno.getText()+"'", null);
     if(c.moveToFirst())
      {
        db.execSQL("UPDATE student SET    
        name='"+editName.getText()+"',marks='"+editMarks.getText()+
                "' WHERE rollno='"+editRollno.getText()+"'");
        showMessage("Success", "Record Modified");
        }
    else
    {
        showMessage("Error", "Invalid Rollno");
    }
    clearText();
    }
    if(view==btnView)
    {
    if(editRollno.getText().toString().trim().length()==0)
    {
        showMessage("Error", "Please enter Rollno");
        return;
    }
    Cursor c=db.rawQuery("SELECT * FROM student WHERE   
     rollno='"+editRollno.getText()+"'", null);
      if(c.moveToFirst())
       {
        editName.setText(c.getString(1));
        editMarks.setText(c.getString(2));
      }
       else
       {
        showMessage("Error", "Invalid Rollno");
        clearText();
       }
      }
        if(view==btnViewAll)
       {
     Cursor c=db.rawQuery("SELECT * FROM student", null);
     if(c.getCount()==0)
       {
        showMessage("Error", "No records found");
        return;
        }
    StringBuffer buffer=new StringBuffer();
     while(c.moveToNext())
     {
        buffer.append("Rollno: "+c.getString(0)+"\n");
        buffer.append("Name: "+c.getString(1)+"\n");
        buffer.append("Marks: "+c.getString(2)+"\n\n");
      }
      showMessage("Student Details", buffer.toString());
      }
     if(view==btnShowInfo)
      {
        showMessage("Student Management Application", "Developed By princ");
       }
     }
     public void showMessage(String title,String message)
        {
      Builder builder=new Builder(this);
     builder.setCancelable(true);
    builder.setTitle(title);
    builder.setMessage(message);
    builder.show();
    }
    public void clearText()
    {
     editRollno.setText("");
     editName.setText("");
     editMarks.setText("");
     editRollno.requestFocus();
    }

所以在这段代码中我想把它作为主键,如果我使用相同的rollno那么它应该显示消息" rollno已经存在"

2 个答案:

答案 0 :(得分:0)

只需将uniq字段添加到表中,就像这样

uid integer primary key autoincrement

这将解决您的问题

答案 1 :(得分:0)

public class FoodDbHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "PKFkM.DB";
private static final int DATABASE_VERSION = 1;
private static final String CREATE_QUERY =
        "CREATE TABLE "+ Food.NewDishInfo.TABLE_NAME+"("
                + Food.NewDishInfo.DISH_NAME+" TEXT unique,"
                + Food.NewDishInfo.DISH_QUANTITY+" TEXT NOT NULL,"
                + Food.NewDishInfo.DISH_CALORIE+" INTEGER,"
                + Food.NewDishInfo.DISH_FAT+" TEXT NOT NULL,"
                + Food.NewDishInfo.DISH_PROTEIN+" TEXT NOT NULL,"
                + Food.NewDishInfo.DISH_SUGAR+" TEXT NOT NULL,"
                + Food.NewDishInfo.DISH_CARBOHYDRATES+" TEXT NOT NULL);";
public FoodDbHelper(Context context)
{
    super(context,DATABASE_NAME,null,DATABASE_VERSION);
    Log.e("DATABASE OPERATION","Database created / opened...");
}
@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(CREATE_QUERY);
    Log.e("DATABASE OPERATION","Table created...");


}

我认为因此对您有所帮助