2

So I have pre defined sql database where I have a method that gets only 4 data and inserts it into the database. And I have another method that only gets 1 data and inserts it into the database but all I get is another row in the database.

Here's my code

`public class DBHelper extends SQLiteOpenHelper {

public static final String USERS_TABLE = "USERS_TABLE";
public static final String COLUMN_ID = "ID";
public static final String COLUMN_NAME = "NAME";
public static final String COLUMN_USERNAME = "USERNAME";
public static final String COLUMN_PASSWORD = "PASSWORD";
public static final String COLUMN_AGE = "AGE";
public static final String COLUMN_CONTACT = "CONTACT";
public static final String COLUMN_DIARY = "DIARY";   


public DBHelper(Context context) {
    super(context, "Login.db", null, 1);
}


@Override
public void onCreate(SQLiteDatabase MyDataBase) {
    String createTableStatement = "CREATE TABLE " + USERS_TABLE +
            " (" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            COLUMN_NAME + " TEXT, " +
            COLUMN_USERNAME + " TEXT, " +
            COLUMN_PASSWORD + " TEXT, " +
            COLUMN_AGE + " TEXT, " +
            COLUMN_CONTACT + " TEXT, " +
            COLUMN_DIARY + " TEXT)";

    MyDataBase.execSQL(createTableStatement);
}

public Boolean insertData (int id, String name, String username, String password, String age){ SQLiteDatabase MyDataBase = this.getWritableDatabase(); ContentValues contentValues = new ContentValues();

    contentValues.put(COLUMN_NAME, name);
    contentValues.put(COLUMN_USERNAME, username);
    contentValues.put(COLUMN_PASSWORD, password);
    contentValues.put(COLUMN_AGE, age);

    long result = MyDataBase.insert(USERS_TABLE,null,contentValues);
    if (result == -1) {
        return false;
    }
    else {
        return true;
    }
}
public Boolean insertContact (String contact){
    SQLiteDatabase MyDataBase = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COLUMN_CONTACT, contact);

    long result = MyDataBase.insert("users",null,contentValues);
    if (result == -1) return false;
    else
        return true;

}`

This the database

AbubuJeje
  • 21
  • 3
  • Your inserts are inserting into two different tables right? – Scary Wombat Jul 21 '22 at 01:27
  • My inserts just inserts into two different rows instead of the same row as the insertData – AbubuJeje Jul 21 '22 at 01:31
  • If you want the second insert to update the row you inserted before you will need to use an `update` statement. – tgdavies Jul 21 '22 at 01:38
  • @tgdavies how to do that? I really don't know how to operate since I'm a beginner at android studio. – AbubuJeje Jul 21 '22 at 01:49
  • Does this answer your question? [SQLite in Android How to update a specific row](https://stackoverflow.com/questions/9798473/sqlite-in-android-how-to-update-a-specific-row) – tgdavies Jul 21 '22 at 01:51

0 Answers0