0

I have a simple SQLite DB for my Android app that has to store details such as - username, password, phno, email and account type.

public class DBHelper extends SQLiteOpenHelper {
    public static final String DBNAME = "Login.db";
    public DBHelper(Context context) {
        super(context, "Login.db", null, 1);
    }


    @Override
    public void onCreate(SQLiteDatabase MyDB) {
        MyDB.execSQL("create Table users(username TEXT primary key, password TEXT, phno TEXT, email TEXT,address TEXT, type TEXT)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase MyDB, int i, int i1) {
        MyDB.execSQL("drop Table if exists users");
    }

    public Boolean insertData(String username, String password, String phno, String email, String address, String type){ 
//**Method for inserting date into the Table -> Users**
        SQLiteDatabase MyDB = this.getWritableDatabase();
        ContentValues contentValues= new ContentValues();
        contentValues.put("username", username);
        contentValues.put("password", password);
        contentValues.put("phno", phno);
        contentValues.put("email", email);
        contentValues.put("address", address);
        contentValues.put("type", type);
        long result = MyDB.insert("users", null, contentValues);
        return result != -1;
    }

    public Boolean checkusername(String username) {
        SQLiteDatabase MyDB = this.getWritableDatabase();
        Cursor cursor = MyDB.rawQuery("Select * from users where username = ?", new String[]{username});
        return cursor.getCount() > 0;
    }

    public Boolean checkusernamepassword(String username, String password){
        SQLiteDatabase MyDB = this.getWritableDatabase();
        Cursor cursor = MyDB.rawQuery("Select * from users where username = ? and password = ?", new String[] {username,password});
        return cursor.getCount() > 0;
    }
    public String getData(String username, String target){
        SQLiteDatabase MyDB = this.getWritableDatabase();
        return String.valueOf(MyDB.rawQuery("Select ? from users where username = ?", new String[]{target, username}));
    }
}

When inserting data Through the signup Activity I get an error saying that Address column in the table does not exist

SignupActivity.class

public class SignupActivity extends AppCompatActivity {
    private Button signup;
    private DBHelper DB;
    private FloatingActionButton backtn;
    private Spinner spinner;
    private String type;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_up);
        spinner = findViewById(R.id.typespinner);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,R.array.Type, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        try {
            spinner.setAdapter(adapter);
        }catch(NullPointerException e){

        }
        username = findViewById(R.id.supusername);
        password = findViewById(R.id.suppassword);
        signup = findViewById(R.id.signupbutton);
        phno = findViewById(R.id.supph);
        email = findViewById(R.id.supemail);
        address = findViewById(R.id.supadd);
        DB = new DBHelper(this);
        backtn = findViewById(R.id.backtn);
        backtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        spinner.setOnItemSelectedListener(this);

        signup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String user = username.getText().toString();
                String pass = password.getText().toString();
                String dbpno = phno.getText().toString();
                String dbemail = email.getText().toString();
                String dbaddress = address.getText().toString();
                if(!(user.equals("") && (pass.equals("")))){
                     if(!DB.checkusername(user)){
                         if(DB.insertData(user,pass,dbpno,dbemail,dbaddress,type)){ 
\\**Inserting data here
                             Toast.makeText(SignUpActivity.this, "Registered Successfully", Toast.LENGTH_SHORT).show();
                             finish();
                         }else{
                             Toast.makeText(SignUpActivity.this, "Registration Failed ", Toast.LENGTH_SHORT).show();
                         }
                     }else{
                         Toast.makeText(SignUpActivity.this, "User Already Exists", Toast.LENGTH_SHORT).show();
                     }
                    finish();
                }else{
                    Toast.makeText(SignUpActivity.this, "Fields Cannot be Empty", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

Here are the logs

SQLiteLog: (1) table users has no column named address in "INSERT INTO users(address,username,phno,type,email,password) VALUES (?,?,?,?,?,?)"
E/SQLiteDatabase: Error inserting address=b105 username=1432 phno=8310676220 type=Donator email=sainishwanthraj@gmail.com password=1234
    android.database.sqlite.SQLiteException: table users has no column named address (code 1 SQLITE_ERROR): , while compiling: INSERT INTO users(address,username,phno,type,email,password) VALUES (?,?,?,?,?,?)
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1047)
        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:654)
        at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:590)
        at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:62)
        at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:34)
        at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1700)
        at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1571)
        at com.example.foodwaste.DBHelper.insertData(DBHelper.java:43)
        at com.example.foodwaste.SignUpActivity$2.onClick(SignUpActivity.java:65)
        at android.view.View.performClick(View.java:7455)
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1131)
        at android.view.View.performClickInternal(View.java:7432)
        at android.view.View.access$3700(View.java:835)
        at android.view.View$PerformClick.run(View.java:28810)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loopOnce(Looper.java:201)
        at android.os.Looper.loop(Looper.java:288)
        at android.app.ActivityThread.main(ActivityThread.java:7842)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)

Where it says column address does not exist but according to the above log it should :/

table users has no column named address in "INSERT INTO users(address,username,phno,type,email,password) VALUES (?,?,?,?,?,?)"


android.database.sqlite.SQLiteException: table users has no column named address (code 1 SQLITE_ERROR): , while compiling: INSERT INTO users(address,username,phno,type,email,password)

I have checked all the ID's in my xml and everything seems to be fine. This error has started to occur since I updated the DB from only accepting username and password to all the fields mentioned above. I am assuming I have to run the onUpgrade() method to drop the old table and create a new one? How would I go about doing that.

I'm Very new to Android and Databases so any help is appreciated.

Junaid Khalid
  • 811
  • 2
  • 11
  • 26
Sai Nishwanth
  • 146
  • 1
  • 9

0 Answers0