如何为数据库中的类似对象执行太多的多功能?

问题描述 投票:0回答:1

我的SQLite数据库有5列。

  1. ID
  2. 意思
  3. 不同的意思
  4. 颜色

首先,我有一个Listview,它处于不同的活动。我正在使用button从Main Activity加载数据。我的目的是;我想添加数据但是当我添加数据时,İf在listview中有相同的数据(İt是第二列(单词))。数据删除本身和其他相同的数据转到listview的顶部并将其颜色更改为不同的颜色。

这怎么看:从1.什么(白色)2。东西(白色)3。不同(白色)4。东西(白色)

  1. 东西(红色)
  2. 任何东西(白色)
  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, "","");


          }
android listview android-sqlite
1个回答
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个参数指定如何处理约束违规(UNIQUENOT NULL是表现在具有的约束)。在这种情况下,您希望忽略约束违规。
  • 注意!为了实现这一点,在修改代码后执行以下操作之一: - 增加版本号 删除应用程序的数据 卸载应用程序
© www.soinside.com 2019 - 2024. All rights reserved.