防止用户将相同的数据输入数据库我使用此
db.execSQL("CREATE UNIQUE INDEX DIFFERENT ON user(name, gender)");
我的问题是如何向最终用户显示错误,告诉他们他们输入的数据已经存在?或者还有另一种方法可以阻止用户输入相同的数据并向用户显示错误?
DatabaseUsername.java(我把唯一索引放在哪里)
public class DatabaseUsername extends SQLiteOpenHelper {
private static final String DATABASE_NAME="rikartoiy.db";
private static final int SCHEMA_VERSION=1;
public DatabaseUsername(Context context) {
super(context, DATABASE_NAME, null, SCHEMA_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE user (_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, gender TEXT);");
db.execSQL("CREATE TABLE score (_id INTEGER PRIMARY KEY AUTOINCREMENT, score INTEGER, dateTaken TEXT, userId INTEGER NOT NULL, FOREIGN KEY (userId) REFERENCES user(_id) ON DELETE CASCADE);"); //create table score
db.execSQL("CREATE UNIQUE INDEX DIFFERENT ON user(name, gender)")
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// no-op, since will not be called until 2nd schema
// version exists
}
public Cursor getAll() {
return(getReadableDatabase()
.rawQuery("SELECT _id, name, gender FROM user ORDER BY name",
null));
}
public Cursor getCurScore() {
return(getReadableDatabase()
.rawQuery("SELECT alm._id, alm.name, alm.gender, sc._id, sc.score, sc.userId FROM user alm, score sc WHERE sc.userId = alm._id AND alm._id = ?",
null));
}
public Cursor getById(String id) {
String[] args={id};
return(getReadableDatabase()
.rawQuery("SELECT _id FROM user WHERE _ID=?",
args));
}
public void insert(String name, String gender) {
ContentValues cv=new ContentValues();
cv.put("name", name);
cv.put("gender", gender);
getWritableDatabase().insert("user", name, cv);
}
public void insertMark(int score, int userId, String dateTaken) {
ContentValues cv=new ContentValues();
cv.put("score", score);
cv.put("userId", userId);
cv.put("dateTaken", dateTaken);
getWritableDatabase().insert("score", null, cv);
}
public String getName(Cursor c) {
return(c.getString(1));
}
public String getGender(Cursor c) {
return(c.getString(2));
}
public int getScore(Cursor c) {
return(c.getInt(3));
}}
UsernameRegister.java(用户输入数据的地方)
public class UsernameRegister extends Activity {
EditText name=null;
RadioGroup gender=null;
DatabaseUsername helper=null;
String almagId=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.usernameregister);
helper=new DatabaseUsername(this);
name=(EditText)findViewById(R.id.name);
gender=(RadioGroup)findViewById(R.id.gender);
Button save=(Button)findViewById(R.id.save);
save.setOnClickListener(onSave);
almagId=getIntent().getStringExtra(UsernameList.ID_EXTRA);
if (almagId!=null) {
load();
}
}
@Override
public void onDestroy() {
super.onDestroy();
helper.close();
}
private void load() {
Cursor c=helper.getById(almagId);
c.moveToFirst();
name.setText(helper.getName(c));
if (helper.getGender(c).equals("Pria")) {
gender.check(R.id.pria);
}
else if (helper.getGender(c).equals("Perempuan")) {
gender.check(R.id.perempuan);
}
c.close();
}
private View.OnClickListener onSave=new View.OnClickListener() {
public void onClick(View v) {
String type=null;
switch (gender.getCheckedRadioButtonId()) {
case R.id.pria:
type="Pria";
break;
case R.id.perempuan:
type="Perempuan";
break;
}
String namanama = name.getText().toString();
if (almagId==null) {
helper.insert(namanama, type);
if(TextUtils.isEmpty(namanama))
{
name.setError("Your name can't be empty");
return;
}
else if (namanama.length() < 3 )
{
name.setError("Your name can't less than 3");
return;
}
else if (namanama.length() > 10 )
{
name.setError("Your name can't more than 3");
return;
}
}
finish();
}
};}
答案 0 :(得分:2)
您可以使用mDb.insert();
Returns
the row ID of the newly inserted row, or -1 if an error occurred
如果它返回-1,并且您确定错误不是来自重复值之外的其他内容,请告诉用户值已经存在。
或者您可以创建一个查找用户在数据库中输入的值的函数,如果这些值存在,则在插入之前向用户显示一条消息。
答案 1 :(得分:0)
你可以通过在插入之前检查数据库中的数据来实现相同的功能,我想。