将数据插入sqlite数据库时如何避免重复值?
How to avoid duplicate value while inserting data into sqlite database?
我试图避免在 android 中将重复值插入到 sqlite 中,但我无法弄清楚避免插入重复值的方法是什么,因为我是 android [=20 中的新手=] 试图在网上找到解决方案,但我没有得到任何满意的答案。
请帮我解决这个问题。
我的代码如下。
# 我的 MainActivity 代码:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText nameEditText, ageEditText, genderEditText;
MyDatabaseHelper myDatabaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nameEditText = findViewById(R.id.nameEditTextId);
ageEditText = findViewById(R.id.ageEditTextId);
genderEditText = findViewById(R.id.genderEditTextId);
Button addButton = findViewById(R.id.addButtonId);
myDatabaseHelper = new MyDatabaseHelper(this);
addButton.setOnClickListener(this);
}
@Override
public void onClick(View view) {
String name = nameEditText.getText().toString();
String age = ageEditText.getText().toString();
String gender = genderEditText.getText().toString();
if (view.getId() == R.id.addButtonId) {
if(TextUtils.isEmpty(name) && TextUtils.isEmpty(age) && TextUtils.isEmpty(gender) ){
Toast.makeText(getApplicationContext(), "Enter details and submit ", Toast.LENGTH_LONG).show();
}
else if (TextUtils.isEmpty(name)) {
Toast.makeText(getApplicationContext(), "Insert the name first ", Toast.LENGTH_LONG).show();
}else if ( TextUtils.isEmpty(age)){
Toast.makeText(getApplicationContext(), "Insert an age ", Toast.LENGTH_LONG).show();
}else if (TextUtils.isEmpty(gender)){
Toast.makeText(getApplicationContext(), "Insert the gender ", Toast.LENGTH_LONG).show();
}else {
long rowId = myDatabaseHelper.insertData(name, age, gender);
if (rowId == -1) {
Toast.makeText(getApplicationContext(), "unsuccessfull ", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Row " + rowId + " is sucessfully inserted ", Toast.LENGTH_LONG).show();
}
//clear screen after inserting value;
nameEditText.setText("");
ageEditText.setText("");
genderEditText.setText("");
}
}
}
}
我的 MyDataBaseHelper 代码:
public class MyDatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "Student.db";
private static final String TABLE_NAME = "student_details";
private static final String ID = "_id";
private static final String NAME = "Name";
private static final String AGE = "Age";
private static final String GENDER = "Gender";
private static final int VERSION_NUMBER = 3 ;
private static final String CREATE_TABLE = "CREATE TABLE "+TABLE_NAME+" ("+ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+NAME+" VARCHAR(255), "+AGE+" INTEGER, "+GENDER+"); ";
private static final String DROP_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME;
private Context context;
MyDatabaseHelper(@Nullable Context context) {
super(context, DATABASE_NAME, null,VERSION_NUMBER);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
try {
Toast.makeText(context,"onCreate is called ", Toast.LENGTH_LONG).show();
sqLiteDatabase.execSQL( CREATE_TABLE);
}catch (Exception e){
Toast.makeText(context,"Exceptin : " +e, Toast.LENGTH_LONG).show();
}
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
try {
Toast.makeText(context,"onUpgrade is called " , Toast.LENGTH_LONG).show();
sqLiteDatabase.execSQL(DROP_TABLE);
onCreate(sqLiteDatabase);
}catch (Exception e){
Toast.makeText(context,"Exceptin : " +e, Toast.LENGTH_LONG).show();
}
}
long insertData(String name, String age, String gender){
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(NAME,name);
contentValues.put(AGE,age);
contentValues.put(GENDER,gender);
return sqLiteDatabase.insert(TABLE_NAME,null,contentValues);
}
}
我不知道是否有简单的方法可以做到这一点,但我知道它可能会有所帮助,
你为什么不创建另一个列来组合你想要防止它们重复的值,并在添加任何新行之前检查它
在找到最佳实践之前,您可以将此答案单独保存
为 Name
、Age
和 Gender
列的组合在 table 中添加 UNIQUE constraint
。
将您的 CREATE
声明更改为:
private static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " (" +
ID +" INTEGER PRIMARY KEY AUTOINCREMENT, " +
NAME + " TEXT, " +
AGE + " INTEGER, "+
GENDER + " TEXT, " +
"UNIQUE(" + NAME + ", " + AGE + ", " + GENDER + ")" +
");";
如果您只希望 Name
是唯一的:
private static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " (" +
ID +" INTEGER PRIMARY KEY AUTOINCREMENT, " +
NAME + " TEXT UNIQUE, " +
AGE + " INTEGER, "+
GENDER + " TEXT +
");";
如果您尝试插入重复项,insert()
方法将 return -1
.
我试图避免在 android 中将重复值插入到 sqlite 中,但我无法弄清楚避免插入重复值的方法是什么,因为我是 android [=20 中的新手=] 试图在网上找到解决方案,但我没有得到任何满意的答案。 请帮我解决这个问题。
我的代码如下。
# 我的 MainActivity 代码:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText nameEditText, ageEditText, genderEditText;
MyDatabaseHelper myDatabaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nameEditText = findViewById(R.id.nameEditTextId);
ageEditText = findViewById(R.id.ageEditTextId);
genderEditText = findViewById(R.id.genderEditTextId);
Button addButton = findViewById(R.id.addButtonId);
myDatabaseHelper = new MyDatabaseHelper(this);
addButton.setOnClickListener(this);
}
@Override
public void onClick(View view) {
String name = nameEditText.getText().toString();
String age = ageEditText.getText().toString();
String gender = genderEditText.getText().toString();
if (view.getId() == R.id.addButtonId) {
if(TextUtils.isEmpty(name) && TextUtils.isEmpty(age) && TextUtils.isEmpty(gender) ){
Toast.makeText(getApplicationContext(), "Enter details and submit ", Toast.LENGTH_LONG).show();
}
else if (TextUtils.isEmpty(name)) {
Toast.makeText(getApplicationContext(), "Insert the name first ", Toast.LENGTH_LONG).show();
}else if ( TextUtils.isEmpty(age)){
Toast.makeText(getApplicationContext(), "Insert an age ", Toast.LENGTH_LONG).show();
}else if (TextUtils.isEmpty(gender)){
Toast.makeText(getApplicationContext(), "Insert the gender ", Toast.LENGTH_LONG).show();
}else {
long rowId = myDatabaseHelper.insertData(name, age, gender);
if (rowId == -1) {
Toast.makeText(getApplicationContext(), "unsuccessfull ", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Row " + rowId + " is sucessfully inserted ", Toast.LENGTH_LONG).show();
}
//clear screen after inserting value;
nameEditText.setText("");
ageEditText.setText("");
genderEditText.setText("");
}
}
}
}
我的 MyDataBaseHelper 代码:
public class MyDatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "Student.db";
private static final String TABLE_NAME = "student_details";
private static final String ID = "_id";
private static final String NAME = "Name";
private static final String AGE = "Age";
private static final String GENDER = "Gender";
private static final int VERSION_NUMBER = 3 ;
private static final String CREATE_TABLE = "CREATE TABLE "+TABLE_NAME+" ("+ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+NAME+" VARCHAR(255), "+AGE+" INTEGER, "+GENDER+"); ";
private static final String DROP_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME;
private Context context;
MyDatabaseHelper(@Nullable Context context) {
super(context, DATABASE_NAME, null,VERSION_NUMBER);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
try {
Toast.makeText(context,"onCreate is called ", Toast.LENGTH_LONG).show();
sqLiteDatabase.execSQL( CREATE_TABLE);
}catch (Exception e){
Toast.makeText(context,"Exceptin : " +e, Toast.LENGTH_LONG).show();
}
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
try {
Toast.makeText(context,"onUpgrade is called " , Toast.LENGTH_LONG).show();
sqLiteDatabase.execSQL(DROP_TABLE);
onCreate(sqLiteDatabase);
}catch (Exception e){
Toast.makeText(context,"Exceptin : " +e, Toast.LENGTH_LONG).show();
}
}
long insertData(String name, String age, String gender){
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(NAME,name);
contentValues.put(AGE,age);
contentValues.put(GENDER,gender);
return sqLiteDatabase.insert(TABLE_NAME,null,contentValues);
}
}
我不知道是否有简单的方法可以做到这一点,但我知道它可能会有所帮助, 你为什么不创建另一个列来组合你想要防止它们重复的值,并在添加任何新行之前检查它 在找到最佳实践之前,您可以将此答案单独保存
为 Name
、Age
和 Gender
列的组合在 table 中添加 UNIQUE constraint
。
将您的 CREATE
声明更改为:
private static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " (" +
ID +" INTEGER PRIMARY KEY AUTOINCREMENT, " +
NAME + " TEXT, " +
AGE + " INTEGER, "+
GENDER + " TEXT, " +
"UNIQUE(" + NAME + ", " + AGE + ", " + GENDER + ")" +
");";
如果您只希望 Name
是唯一的:
private static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " (" +
ID +" INTEGER PRIMARY KEY AUTOINCREMENT, " +
NAME + " TEXT UNIQUE, " +
AGE + " INTEGER, "+
GENDER + " TEXT +
");";
如果您尝试插入重复项,insert()
方法将 return -1
.