我的SQLite数据库有5列。
首先,我有一个Listview,它处于不同的活动中。我正在使用button从主活动加载数据。我的目的是;我想添加数据但是当我添加数据时,İf在listview中有相同的数据(İt是第二列(单词))。数据删除本身和其他相同的数据进入listview的顶部并将其颜色更改为不同的颜色。
这怎么看: 从 什么(白色) 东西(白色) 3.不同(白色) 东西(白色)
到
我的数据库:
class DatabaseHelper extends SQLiteOpenHelper {
private static final String TAG = "DatabaseHelper";
private static final String TABLE_NAME = "kartlarim";
private static final String COL1 = "ID";
private static final String COL2 = "name";
private static final String COL3 = "anlam";
private static final String COL4 = "akli_göz";
private static final String COL5 = "boya";
public DatabaseHelper(Context context) {
super(context, TABLE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
String createTable = "CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " + COL2 + " TEXT,"+
COL3 + " TEXT,"+ COL4 + " TEXT," +COL5 + " TEXT)";
db.execSQL(createTable);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP IF TABLE EXISTS " + TABLE_NAME);
onCreate(db);
}
public boolean addData(String item , String number , String anlam) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL2, item);
contentValues.put(COL3 , number);
contentValues.put(COL4, anlam);
contentValues.put(COL5 ,"#419FEE");
Log.d(TAG, "addData: Adding " + item + " to " + TABLE_NAME);
long result = db.insert(TABLE_NAME, null, contentValues);
//if date as inserted incorrectly it will return -1
if (result == -1) {
return false;
} else {
return true;
}
}
数据库更新数据:
public String update(String table, ContentValues data, String where, String[] whereArgs) {
SQLiteDatabase db = this.getWritableDatabase();
// String where = "id=?";
// String[] whereArgs = new String[] {String.valueOf(id)};
try {
long returnId = db.update(table, data, where, whereArgs);
if (returnId > 0) {
db.close();
return "Success";
} else {
db.close();
return "fail";
}
} catch (Exception e) {
String error = e.getMessage().toString();
db.close();
return error;
}
}
删除行:
public void deleteAllName(int id, String name ,String anlam, String akli){
SQLiteDatabase db = this.getWritableDatabase();
String query = "DELETE FROM " + TABLE_NAME + " WHERE "
+ COL1 + " = '" + id + "' AND "
+ COL2 + " = '" + name + "' AND "
+ COL3 + " = '"+ anlam +"' AND "
+ COL4 + " = '"+ akli + "'";
Log.d(TAG, "deleteName: query: " + query);
Log.d(TAG, "deleteName: Deleting " + name + " from database.");
db.execSQL(query);
}
我的阵列:
Cursor data = mDatabase.getData();
while (data.moveToNext()){
kelimeler_id_array.add(data.getString(0));
kelimeler_array.add(data.getString(1)) ;
akli_göz_array.add(data.getString(2));
kelimelerin_anlamı_array.add(data.getString(3));
boya_array.add(data.getString(4));
}
按钮声明:
String user_words_unknown = user_unknown_words_edittext.getText().toString();
// simply add to database.
if (!kelimeler_array.contains(user_words_unknown)){
mDatabase.addData(user_words_unknown, "","");
}
//Here , is my problem
if (kelimeler_array.contains(user_words_unknown)) {
mDatabase.addData(user_words_unknown, "","");
}
答案 0 :(得分:1)
如果我正确理解了您的问题,那么您的问题是,如果 COL2
中的值已经存在,则您不想插入(添加)新行。您实际上希望 COL2
唯一。
正确的方法是将列声明为 UNIQUE
以及 NOT NULL
(因为您希望确保null为空永远不会插入带有null的行。
所以而不是: -
@Override
public void onCreate(SQLiteDatabase db) {
String createTable = "CREATE TABLE " +
TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
COL2 + " TEXT,"+
COL3 + " TEXT,"+
COL4 + " TEXT," +
COL5 + " TEXT
)";
db.execSQL(createTable);
}
你可以: -
@Override
public void onCreate(SQLiteDatabase db) {
String createTable = "CREATE TABLE " +
TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
COL2 + " TEXT UNIQUE NOT NULL," + //<<<< changed
COL3 + " TEXT,"+
COL4 + " TEXT," +
COL5 + " TEXT
)";
db.execSQL(createTable);
}
上面的内容将有效,不会插入重复内容。但是,您会收到错误,但没有例外。要禁止输出到日志,您可能需要使用: -
public boolean addData(String item , String number , String anlam) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL2, item);
contentValues.put(COL3 , number);
contentValues.put(COL4, anlam);
contentValues.put(COL5 ,"#419FEE");
Log.d(TAG, "addData: Adding " + item + " to " + TABLE_NAME);
long result = db.insertWithOnConflict(TABLE_NAME,
null,
contentValues,
SQLiteDatabase.CONFLICT_IGNORE
);
//if date as inserted incorrectly it will return -1
if (result == -1) {
return false;
} else {
return true;
}
}
使用insertWithOnConflict
而非insert
允许第4个参数指定如何处理 约束违规行为 ( { {1}} 和 UNIQUE
是 约束 ,表现在已经存在)。在这种情况下,您希望 忽略约束违规行为 。
请注意!为了实现这一点,在修改代码后执行以下操作之一: -