I have this database code:
public static final String KEY_ROWID = "_id";
public static final String KEY_FNAME = "fname";
public static final String KEY_LNAME = "lname";
public static final String KEY_MNAME = "mname";
public static final String KEY_CONTACTNUM = "contactNum";
public static final String KEY_LICENSE = "license";
public static final String KEY_USER = "username";
public static final String KEY_PASS = "password";
private static final String DATABASE_NAME = "sampleForFinals";
private static final String DATABASE_TABLE = "drivers";
private static final int DATABASE_VERSION = 1;
private DBHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
private static class DBHelper extends SQLiteOpenHelper {
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(" CREATE TABLE " + DATABASE_TABLE + "(" +
KEY_ROWID + "_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_FNAME + "TEXT NOT NULL, " +
KEY_LNAME + "TEXT NOT NULL, " +
KEY_MNAME + "TEXT NOT NULL, " +
KEY_CONTACTNUM + "TEXT NOT NULL, " +
KEY_LICENSE + "TEXT NOT NULL, " +
KEY_USER + "TEXT NOT NULL, " +
KEY_PASS + "TEXT NOT NULL);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXIST " + DATABASE_NAME);
onCreate(db);
}
}
public DatabaseHelper(Context c) {
ourContext = c;
}
public DatabaseHelper open() {
ourHelper = new DBHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
public void close() {
ourHelper.close();
}
public long savedata(String fname, String lname, String mname, String contactNum, String license, String user, String pass) {
ContentValues cv = new ContentValues();
cv.put(KEY_FNAME, fname);
cv.put(KEY_LNAME, lname);
cv.put(KEY_MNAME, mname);
cv.put(KEY_CONTACTNUM, contactNum);
cv.put(KEY_LICENSE, license);
cv.put(KEY_USER, user);
cv.put(KEY_PASS, pass);
return ourDatabase.insert(DATABASE_TABLE, null, cv);
}
Here is my register.java
register.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
String fname = firstname.getText().toString();
String lname = lastname.getText().toString();
String mname = middlename.getText().toString();
String contact = contactNum.getText().toString();
String plate = license.getText().toString();
String user = username.getText().toString();
String pass = password.getText().toString();
DatabaseHelper save = new DatabaseHelper(Register.this);
save.open();
save.savedata(fname,lname, mname, contact, plate, user, pass);
firstname.setText("");
lastname.setText("");
middlename.setText("");
contactNum.setText("");
license.setText("");
username.setText("");
password.setText("");
}
});
Basically, what the error says is:
table drivers has no column named mname
inserting mname=a username=a lname=a license=a password=a contactNum=4 fname=a
android.database.sqlite.SQLiteException: table drivers has no column named mname (code 1): , while compiling: INSERT INTO drivers(mname,username,lname,license,password,contactNum,fname) VALUES (?,?,?,?,?,?,?)
So, the database i use is from FITACADEMY. It is an sqlite. I can see the database tables and the columns and yes mname column does exist in FITACADEMY. I can see it. However, the error says it doesnt have the column 'mname'. I tried to delete all the codes related to 'mname' and it proceeds to 'user' not having a column. So, i removed all the codes related to 'user', then it has an error again of not having the column 'fname'. Note: This is inserting data into the database.
What is wrong with my code? Ive been finding the error for 3 hours now. I still cant figure it out.