如何在表中已存在插入名称时替换表中的值

时间:2015-03-21 05:02:26

标签: android sql database sqlite android-sqlite

如果在表中已经存在相同的名称,如何在插入时更新我的​​值。 例如:我的表有值(bala,100,chennai)。如果插入名称相同,我尝试插入值我想将值(bala,100,chennai)替换为(bala,50,mumbai)。而我插入(巴拉,50,孟买)。我是数据库方面的初学者。在此先感谢。

public class HorizantalDBHandler extends SQLiteOpenHelper {

// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME = "hrlist";

// Contacts table name
// private static final String TABLE_CONTACTS = "breakfast";
private static final String TABLE_BREAKFAST = "hrlist";

// Contacts Table Columns names

private static final String KEY_ID = "id";
private static final String KEY_NUMBER = "number";
private static final String KEY_NAME = "name";
private static final String KEY_IMAGE = "image";

public HorizantalDBHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_BREAKFAST_TABLE = "CREATE TABLE " + TABLE_BREAKFAST + "("
            + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
            + KEY_NUMBER + " TEXT," + KEY_IMAGE + " BLOB" + ")";

    db.execSQL(CREATE_BREAKFAST_TABLE);
}

// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_BREAKFAST);

    // Create tables again
    onCreate(db);
}

/**
 * All CRUD(Create, Read, Update, Delete) Operations
 */

public// Adding new contact
void addContact(HorizantalPojo contact) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();

    System.out.println("################");
    System.out.println("################");
    System.out.println("################");
    System.out.println(contact._name);
    System.out.println(contact._number);
    System.out.println(contact._image);

    values.put(KEY_NAME, contact._name); // Contact Name
    values.put(KEY_NUMBER, contact._number);
    values.put(KEY_IMAGE, contact._image); // Contact Phone
    //
    //
    // Inserting Row
    db.insert(TABLE_BREAKFAST, null, values);

    System.out.println("INSERTED SUCCESSSSSSSSSSS");
    db.close(); // Closing database connection
    // }
}
public List<HorizantalPojo> getAllContacts() {
    List<HorizantalPojo> contactList = new ArrayList<HorizantalPojo>();
    // Select All Query
    String selectQuery = "SELECT  * FROM hrlist ORDER BY name";

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);
    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            HorizantalPojo contact = new HorizantalPojo();
            contact.setID(Integer.parseInt(cursor.getString(0)));
            contact.setName(cursor.getString(1));
            contact.setNumber(cursor.getString(2));
            contact.setImage(cursor.getBlob(3));

            System.out.println("^^^^^^^^^^^^^^^^");
            System.out.println("^^^^^^^^^^^^^^^^");
            System.out.println("^^^^^^^^^^^^^^^^");
            System.out.println("^^^^^^^^^^^^^^^^");
            System.out.println(cursor.getString(0));
            System.out.println(cursor.getString(1));
            System.out.println(cursor.getString(2));
            System.out.println(cursor.getBlob(3));

            // Adding contact to list
            contactList.add(contact);
        } while (cursor.moveToNext());
    }
    // close inserting data from database
    db.close();
    // return contact list
    return contactList;

}
}

3 个答案:

答案 0 :(得分:0)

  

如果插入名称已存在,如何替换表中的值   在表格中

需要使用update方法而不是insert来更新现有行中的数据:

1。使用SELECT查询检查记录是否已存在:

Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_BREAKFAST + "
                              where "+KEY_NAME+"= '" +contact._name + "'" , null);
if(cursor!=null){
  if(cursor.getCount()>0){
     // use db.update for updating current columns values
      ContentValues values = new ContentValues();
      values.put(KEY_NUMBER, contact._number);
      values.put(KEY_IMAGE, contact._image);
      db.update(TABLE_BREAKFAST, values,KEY_NAME +
                             " = ?"+contact._name, new String[] {contact._name});
  }else{
     // use db.insert to add new record in db
  }
}

答案 1 :(得分:0)

非常简单的第一个查询具有该名称的行数,如果它没有在之前插入行,那么使用insert,如果它不是0则使用更新查询。

答案 2 :(得分:0)

您可以尝试使用以下代码替换代码:

void addContact(HorizantalPojo contact) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();

    System.out.println("################");
    System.out.println("################");
    System.out.println("################");
    System.out.println(contact._name);
    System.out.println(contact._number);
    System.out.println(contact._image);

    values.put(KEY_NAME, contact._name); // Contact Name
    values.put(KEY_NUMBER, contact._number);
    values.put(KEY_IMAGE, contact._image); // Contact Phone
    //
    //
    // Inserting Row
db.insertWithOnConflict(TABLE_BREAKFAST, null, values, 0);

    System.out.println("INSERTED SUCCESSSSSSSSSSS");
    db.close(); // Closing database connection
    // }
}

注意:insertwithOnConflict方法用于强制插入数据,替换现有数据。