i deleted my old table with the statement: db.execSQL("DROP TABLE IF EXISTS " + TABLE_REGISTER); written in method addUser();
i think table is deleted. But now when i register new User. User registration failed. I think new table is not creating. I also have deleted the above statement from addUser() method.
public class DBHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "agent.db";
private static final int DATABASE_VERSION = 1;
private static final String KEY_ID = "[ID]";
private static final String KEY_FNAME = "[FirstName]";
private static final String KEY_LNAME = "[LastName]";
private static final String KEY_REGDATE = "[RegistrationDate]";
private static final String KEY_USERID = "[UserID]";
private static final String KEY_PASSWORD = "[Password]";
private static final String TABLE_REGISTER = "tbRegister";
private static final String CREATE_TABLE_REGISTER = "CREATE TABLE " + TABLE_REGISTER + "("
+ KEY_ID + "INTEGER PRIMARY KEY,"
+ KEY_FNAME + "TEXT NOT NULL,"
+ KEY_LNAME + "TEXT NOT NULL,"
+ KEY_REGDATE + "TEXT NOT NULL,"
+ KEY_USERID + "TEXT NOT NULL,"
+ KEY_PASSWORD + "TEXT NOT NULL )";
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_REGISTER);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_REGISTER);
onCreate(db);
}
public long addUser(User user) {
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_ID, user.getId());
values.put(KEY_FNAME, user.getfName());
values.put(KEY_LNAME, user.getlName());
values.put(KEY_REGDATE, user.getRegDate());
values.put(KEY_USERID, user.getUserID());
values.put(KEY_PASSWORD, user.getPassword());
return db.insert(TABLE_REGISTER, null, values);
}
public List<User> getAllUsers() {
List<User> userList = new ArrayList<>();
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT * from " + TABLE_REGISTER, null);
if (cursor.moveToFirst()){
do {
int id = cursor.getInt(0);
String fName = cursor.getString(1);
String lName = cursor.getString(2);
String regDate = cursor.getString(3);
String userID = cursor.getString(4);
String password = cursor.getString(5);
User user = new User(id, fName, lName, regDate, userID, password);
userList.add(user);
}while (cursor.moveToNext());
cursor.close();
}
return userList;
}
}
04-07 17:05:40.344 28263-28263/? E/Zygote: v2
04-07 17:05:40.344 28263-28263/? E/Zygote: accessInfo : 0
04-07 17:06:09.354 28263-28263/pk.edu.vu.agentpawnbroker E/SQLiteLog: (1) table tbRegister has no column named FirstName
04-07 17:06:09.364 28263-28263/pk.edu.vu.agentpawnbroker E/SQLiteDatabase: Error inserting [Password]=ppp [FirstName]=Majid [UserID]=munir64 [RegistrationDate]=07042019 [LastName]=Munir
android.database.sqlite.SQLiteException: table tbRegister has no column named FirstName (code 1): , while compiling: INSERT INTO tbRegister([Password],[FirstName],[UserID],[RegistrationDate],[LastName]) VALUES (?,?,?,?,?)
#################################################################
Error Code : 1 (SQLITE_ERROR)
Caused By : SQL(query) error or missing database.
(table tbRegister has no column named FirstName (code 1): , while compiling: INSERT INTO tbRegister([Password],[FirstName],[UserID],[RegistrationDate],[LastName]) VALUES (?,?,?,?,?))
#################################################################
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1058)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:623)