so I'm creating an app using android studio for college, its very basic, i have a login and register pages set and working, I have a page where I want to add customer details to a database using android studio built in sqlite, its currently set up on a button click event but for some reason it is not working, I used the same method for creating the registration page and storing user login details and that works fine. I'm not sure what the problem is or maybe should be doing it a different way?
btn_submit.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
String fname = et_name.getText().toString().trim();
String surname = et_surname.getText().toString().trim();
String address1 = et_addressline.getText().toString().trim();
String address2 = et_addressline2.getText().toString().trim();
String address3 = et_addressline3.getText().toString().trim();
String postcode = et_postcode.getText().toString().trim();
String phoneNo = et_phonenumber.getText().toString().trim();
String email = et_customeremail.getText().toString().trim();
if(fname.equals("") || surname.equals("") || address1.equals("") || address2.equals("") || address3.equals("") || postcode.equals("") || phoneNo.equals("") || email.equals(""))
{
Toast.makeText(Add_Customer.this, "Please Fill out all sections", Toast.LENGTH_SHORT).show();
}
else
{
long val = Cdb.addCustomer(fname, surname, address1, address2, address3, postcode, phoneNo, email);
if(val > 0)
{
Toast.makeText(Add_Customer.this, "Customer added Successfully", Toast.LENGTH_SHORT).show();
Intent moveToAddJobs = new Intent(Add_Customer.this, add_job_details.class);
startActivity(moveToAddJobs);
}
else
{
Toast.makeText(Add_Customer.this, "Failed To Add Customer", Toast.LENGTH_SHORT).show();
}
}
}
that's the code am using for the button click event, everything runs fine except that it doesnt seem to add the data to the database and just shows me the message for failing to add customer that i set up
public class CustomerDB extends SQLiteOpenHelper
{
// defines the database structure
public static final String DATABASE_NAME = "customerDB.db";
public static final String TABLE_NAME = "tbl_Customers";
public static final String COL_1 = "Customer_ID";
public static final String COL_2 = "Customer_First_Name";
public static final String COL_3 = "Customer_Surname";
public static final String COL_4 = "Customer_Address_Line_1";
public static final String COL_5 = "Customer_Address_Line_2";
public static final String COL_6 = "Customer_Address_Line_3";
public static final String COL_7 = "Customer_Postcode";
public static final String COL_8 = "Customer_Phone_Number";
public static final String COL_9 = "Customer_Email";
// creates the database
public CustomerDB(Context context){ super(context, DATABASE_NAME, null, 1);}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase)
{
sqLiteDatabase.execSQL("CREATE TABLE tbl_Customers " +
"(Customer_ID INTEGER PRIMARY KEY AUTOINCREMENT," +
"Customer_First_Name TEXT," +
"Customer_Surname TEXT, " +
"Customer_Address_Line_1 TEXT," +
"Customer_Address_Line_2 TEXT, " +
"Customer_Address_Line_3 TEXT," +
"Customer_Postcode TEXT, " +
"Customer_Phone_Number TEXT," +
"Customer_Email TEXT) ");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1)
{
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(sqLiteDatabase);
}
public long addCustomer (String fname, String surname, String address1, String address2, String address3, String postcode, String phoneNo, String email)
{
SQLiteDatabase Cdb = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("Customer_First_Name", fname);
contentValues.put("Customer_Surname", surname);
contentValues.put("Customer_Address_Line_1", address1);
contentValues.put("Customer_Address_Line_2", address2);
contentValues.put("Customer_Address_Line_3", address3);
contentValues.put("Customer_Postcode", postcode);
contentValues.put("Customer_Phone_Number", phoneNo);
contentValues.put("Customer_Email", email);
long result = Cdb.insert("tbl_Customers", null, contentValues);
Cdb.close();
return result;
}
}
That's the code I have used in creating my database class for adding customers.
question from:
https://stackoverflow.com/questions/65892318/android-studio-how-to-add-customer-details-to-database