-3

I'm taking an intro to android programming course running into some issues adding to the sqlite database

Any suggestions what might be causing this error. I think it is related to not having the proper assignment to the mDatabase field variable.

It seems like the issue is that mDatabase variable in the addCustomer function needs different context.

  private static ContentValues getContentValues(Customer customer) { 
     ContentValues values = new ContentValues(); 
     values.put(UUID, customer.getId().toString()); 
     values.put(FULL_NAME, customer.getName()); 
     values.put(ADDRESS, customer.getAddress()); 
     values.put(CREDIT_CARD_NUMBER, customer.getCreditCardNumber()); 
     values.put(EMAIL, customer.getEmail()); 
     values.put(SESSIONS_REMAINING, customer.getSessionsRemaining()); 
     values.put(PRINT_RECEIPT, customer.getPrintReceipt() ? 1 : 0); 
     values.put(EMAIL_RECEIPT, customer.getEmailReceipt() ? 1 : 0); 
     return values; 
 } 

 public void addCustomer(Customer c) { 
    ContentValues values = getContentValues(c); 
    mDatabase.insert(CustomerDbSchema.CustomerTable.NAME, null, values); 
    Log.d("DATABASE", "Customer Added"); 
 } 

File Links on Github below:

CustomerDataBase.java

CustomerCursorWrapper.java

CustomerDBSchema.java

Customer.java

AddCustomerActivity.java

Zachary B
  • 1
  • 3

1 Answers1

0

1) I think you are not using the SQLiteDatabase mDatabase = getWritableDatabase(); properly. 2) See the ContentValues for more info. 3) Use an if to make sure it was inserted.

    if (mDatabase.insert(CustomerDbSchema.CustomerTable.NAME, null, contentValues) > 0) {
        return true;
    }
    return false;
Gaspar
  • 1,515
  • 13
  • 20