0

I can't seem to save a stringbuilder to my database file. I can't understand how ContentValues.putAll works as I think this is how I need to solve my problem. This is the code relevant to the issue.

I think what I need to do is have a variable in my main activity that stores both 'etNameString' and 'strReturnedAddress' and then in 'DatabaseOperations.updateDatabase()' .. I need to have the following statement:

args.putAll(values)

But I don't know how to have both the values saved in one variable. Excuse my naivety, i'm pretty new to this. Thanks in advance.

Insert row method:

**DatabaseOperations class**

public void updateDatabase(String Name, StringBuilder Address) {
    SQLiteDatabase myDB = this.getWritableDatabase();
    ContentValues args = new ContentValues();
    args.put(COL_1, Name);
    args.put(COL_2, Address);
    myDB.insert(TABLE_NAME, null, args);
    Log.d("Database Operations", "Entry Successful");
}

Call to updateDatabase with arguments: (strReturnedAddress is defined in unshown code)

**MainActivity class**

//Updates Database with Name // Shows Toast // Sets Text Field to Blank
                    EditText etName = (EditText) findViewById(R.id.etName);
                    String etNameString = etName.getText().toString();
                    db.updateDatabase(etNameString, strReturnedAddress);
                    Toast toast = Toast.makeText(MainActivity.this, "Entry Saved", Toast.LENGTH_SHORT);
                    toast.show();
                    etName.setText(" ");

Setting up my database:

**DatabaseOperations class**

public static final String DATABASE_NAME = "HDL_DB";
public static final String TABLE_NAME = "HDL_Table";
public static final String COL_1 = "Name";
public static final String COL_2 = "Address";


public String CREATE = "CREATE TABLE " + TABLE_NAME + " (" +
                COL_1 + " TEXT" + COL_2 + " TEXT)";
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

1 Answers1

0

Two issues. One that user Yazan mentioned in their comment, you should get the string from your StringBuilder with args.put(COL_2, Address.toString());

The second issue is with your String CREATE in your DatabaseOperations class. You haven't got anything separating your two column names (note the comma and space after the first TEXT):

public String CREATE = "CREATE TABLE " + TABLE_NAME + " (" +
            COL_1 + " TEXT, " + COL_2 + " TEXT)";
Joseph Roque
  • 5,066
  • 3
  • 16
  • 22