-3

I am learning how to use an sql data base and am having trouble. I have added multiple columns to my database however when i try and put one item into a column , its adds it to all the columns.

ContentValues values = new ContentValues();
values.put(MySQLiteHelper.COLUMN_PLACES, "string one");
values.put(MySQLiteHelper.COLUMN_PLACES2, "string two");
System.out.println(values.get(MySQLiteHelper.COLUMN_PLACES));
System.out.println(values.get(MySQLiteHelper.COLUMN_PLACES2));

What im doing here is putting "string one" into COLUMN_PLACES and am putting "string two" into COLUMN_PLACES2. When i call System out print, both calls return "string two". Why is the "string two" being saved into both columns?

EDIT: here is more of the code:

//this is my helper class

public class MySQLiteHelper extends SQLiteOpenHelper {
public static final String TABLE_COMMENTS = "comments";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_COMMENT = "comment";
public static final String COLUMN_COMMENT2 = "comment";
private static final String DATABASE_NAME = "commments.db";
private static final int DATABASE_VERSION = 1;
// Database creation sql statement
private static final String DATABASE_CREATE = "create table "
        + TABLE_COMMENTS + "(" + COLUMN_ID
        + " integer primary key autoincrement, " + COLUMN_COMMENT
        + " text not null, " + COLUMN_COMMENT2
        + " text not null);";
public MySQLiteHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase database) {
    database.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    Log.w(MySQLiteHelper.class.getName(),
            "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_COMMENTS);
    onCreate(db);
}
}

//this is where im trying to call the code

public class objDataSource {

private SQLiteDatabase database;
private MySQLiteHelper dbHelper;
private String[] allColumns = { MySQLiteHelper.COLUMN_ID,
        MySQLiteHelper.COLUMN_COMMENT,
        MySQLiteHelper.COLUMN_COMMENT2};

public objDataSource(Context context) {

    dbHelper = new MySQLiteHelper(context);
}

public void open() throws SQLException {
    database = dbHelper.getWritableDatabase();
}
public void close() {
    dbHelper.close();
}

public objectaaa createObject(String lol) {
    ContentValues values = new ContentValues();
    values.put(MySQLiteHelper.COLUMN_COMMENT, lol);
    values.put(MySQLiteHelper.COLUMN_COMMENT2, "second");
    System.out.println(values.get(MySQLiteHelper.COLUMN_COMMENT));
    System.out.println(values.get(MySQLiteHelper.COLUMN_COMMENT2));

    long insertId = database.insert(MySQLiteHelper.TABLE_COMMENTS, null,
            values);
    Cursor cursor = database.query(MySQLiteHelper.TABLE_COMMENTS,
            allColumns, MySQLiteHelper.COLUMN_ID + " = " + insertId, null,
            null, null, null);
    cursor.moveToFirst();
    objectaaa newObj = cursorToObj(cursor);
    cursor.close();
    return newObj;
}

private objectaaa cursorToObj(Cursor cursor) {
    objectaaa obj = new objectaaa();
    obj.setId(cursor.getLong(0));
    obj.setComment(cursor.getString(1));
    return obj;
}

public List<objectaaa> getAllObjects() {
    List<objectaaa> objects = new ArrayList<objectaaa>();
    Cursor cursor = database.query(MySQLiteHelper.TABLE_COMMENTS,
            allColumns, null, null, null, null, null);
    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
        objectaaa object = cursorToObj(cursor);
        System.out.println("FOUND AN OBJECT AT: " + object.getId() + " with the comment: " + object.getComment());
        objects.add(object);
        cursor.moveToNext();
    }
    // Make sure to close the cursor
    cursor.close();
    return objects;
}

public void deleteObject(objectaaa object) {
    long id = object.getId();
    System.out.println("Comment deleted with id: " + id);
    database.delete(MySQLiteHelper.TABLE_COMMENTS, MySQLiteHelper.COLUMN_ID
            + " = " + id, null);
}

}

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
James
  • 7
  • 2

1 Answers1

1

ContentValues is a kind of Map (it has a HashMap internally) and each entry is referred with its key.

Now you have two equal keys:

public static final String COLUMN_COMMENT = "comment";
public static final String COLUMN_COMMENT2 = "comment";

So obviously equal keys get you the same object here:

values.put(MySQLiteHelper.COLUMN_COMMENT, lol);
values.put(MySQLiteHelper.COLUMN_COMMENT2, "second");
System.out.println(values.get(MySQLiteHelper.COLUMN_COMMENT));
System.out.println(values.get(MySQLiteHelper.COLUMN_COMMENT2));

and the second put() call overwrites the value previously put under the same key.

laalto
  • 150,114
  • 66
  • 286
  • 303