-1

DatabaseHandler.Java

    private static final int DATABASE_Version=4;
    private static final String DATABASE_Name="UserManager";
    private static final String TABLE_USERS="Users";
    private SQLiteDatabase db;
    private static final String USER_NAME ="Name";
    private static final String USER_PASSWRD ="Passwrod";
    private static final String USER_REPASSWRD ="ReEnterPassword";
    private static final String USER_EMAIL ="Email";
    private static final String USER_AGE ="Age";
    private static final String USER_PHONENO ="PhoneNumber";
    private static final String USER_COLLEGE ="College";
    private static final String USER_COURSE ="Course";
    public DatabaseHandler(Context context){
        super(context, DATABASE_Name, null, DATABASE_Version);
    }
    public void onCreate(SQLiteDatabase db) {
        String CREATE_USERS_TABLE = "CREATE TABLE " + TABLE_USERS + "("
                + USER_NAME + " TEXT," + USER_PASSWRD + " TEXT,"
                + USER_REPASSWRD + " TEXT," + USER_EMAIL + " TEXT PRIMARY KEY,"  + USER_PHONENO +" INTEGER," + USER_COLLEGE + " TEXT,"
                 + USER_COURSE + " TEXT);" ;
        db.execSQL(CREATE_USERS_TABLE);
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_USERS);

        // Create tables again
        onCreate(db);
    }
    public void addUser(signup users) {
        db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(USER_NAME, users.getName()); // Contact Name
        values.put(USER_PASSWRD, users.getPasswrd());
        values.put(USER_REPASSWRD, users.getRepaswrd());
        values.put(USER_EMAIL, users.getEmail());
        values.put(USER_AGE, users.getAge());
        values.put(USER_PHONENO, users.getPhoneno());
        values.put(USER_COLLEGE, users.getCollege());
        values.put(USER_COURSE, users.getCourse()); // Contact Phone Number
        // Inserting Row
        db.insert(TABLE_USERS, null, values);
        db.close(); // Closing database connection
    }


    public void open()
    {
       db = this.getWritableDatabase();
    }


    public void close()
    {
        this.close();
    }

    public boolean Login(String username, String password) throws SQLException
    {
        db = null;
        Cursor mCursor = db.rawQuery("SELECT * FROM " + TABLE_USERS + " WHERE ``username=? AND password=?", new String[]{username,password});
        if (mCursor != null) {
            if(mCursor.getCount() > 0)
            {
                return true;
            }
        }
        return false;
    }

RegisterActivity.Java

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        final DatabaseHandler db=new DatabaseHandler(this);

        final TextView name1 = (TextView) findViewById(R.id.name);


        final TextView pwd1 = (TextView) findViewById(R.id.paswrd);
        final TextView cnfrm1 = (TextView) findViewById(R.id.repaswrd);
        final TextView email1 = (TextView) findViewById(R.id.email);
        final TextView age1 = (TextView) findViewById(R.id.age);
        final TextView phone1 = (TextView) findViewById(R.id.phone);
        final TextView college1 = (TextView) findViewById(R.id.college);
        final TextView course1 = (TextView) findViewById(R.id.course);



        Button signup = (Button) findViewById(R.id.button);
        View.OnClickListener sp = new View.OnClickListener()

        {
            @Override
            public void onClick(View v) {

                String name = name1.getText().toString();
                String pwd = pwd1.getText().toString();
                String cnfrm = cnfrm1.getText().toString();
                String email = email1.getText().toString();
                String age = age1.getText().toString();
                String phone = phone1.getText().toString();
                String college = college1.getText().toString();
                String course = course1.getText().toString();

                if (name.isEmpty()) {
                    name1.setError("Enter Name");
                } else if (pwd.isEmpty()) {
                    pwd1.setError("Enter Password");
                } else if (cnfrm.isEmpty()) {
                    cnfrm1.setError("Enter Confirm Password ");
                } else if (email.isEmpty()) {
                    email1.setError("Enter Email");
                } else if (age.isEmpty()) {
                    age1.setError("Enter Age");
                } else if (phone.isEmpty()) {
                    phone1.setError("Enter Phone Number");
                } else if (college.isEmpty()) {
                    college1.setError("Enter College name");
                } else if (course.isEmpty()) {
                    course1.setError("Enter Course");
                } else if (!(pwd.equals(cnfrm))) {
                    cnfrm1.setError("Password doesn't Match");
                } else {

                    String name_1= name1.getText().toString();
                    String pass_1= pwd1.getText().toString();
                    String repass_1= cnfrm1.getText().toString();
                    String email_1= email1.getText().toString();
                    String age_1= age1.getText().toString();
                    String phoneno_1= phone1.getText().toString();
                    String clg_1= college1.getText().toString();
                    String course_1= course1.getText().toString();
                    Log.d("Insert: ", "Inserting ..");
                    db.addUser(new signup(name_1, pass_1, repass_1, email_1, age_1, phoneno_1, clg_1, course_1));

                     Toast.makeText(getApplicationContext(), "Successfully Signed in", Toast.LENGTH_LONG).show();
                    Intent intent=new Intent(RegisterActivity.this,FirstActivity.class);

                    startActivity(intent);
                }
            }
        };

        signup.setOnClickListener(sp);
        Button reset=(Button) findViewById(R.id.rest);
        View.OnClickListener rs= new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((TextView) findViewById(R.id.name)).setText(null);
                ((TextView) findViewById(R.id.course)).setText(null);
                ((TextView) findViewById(R.id.email)).setText(null);
                ((TextView) findViewById(R.id.age)).setText(null);
                ((TextView) findViewById(R.id.college)).setText(null);
                ((TextView) findViewById(R.id.paswrd)).setText(null);
                ((TextView) findViewById(R.id.repaswrd)).setText(null);
                ((TextView) findViewById(R.id.phone)).setText(null);
            }
        };
        reset.setOnClickListener(rs);
    }

signup.Java

public class signup {
    String name;
    String passwrd;
    String repaswrd;
    String email;
    String age;
    String phoneno;
    String college;
    String course;


    public signup(String name,String passwrd,String repaswrd,String email,String age,String phoneno,String college,String course){
        this.name=name;
        this.passwrd=passwrd;
        this.repaswrd=repaswrd;
        this.email=email;
        this.age=age;
        this.phoneno=phoneno;
        this.college=college;
        this.course=course;

    }

    public signup(String name,String passwrd,String repaswrd,String email,String college,String course){
        this.name=name;
        this.passwrd=passwrd;
        this.repaswrd=repaswrd;
        this.email=email;
        this.college=college;
        this.course=course;

    }


    public String getName(){
        return this.name;
    }
    public String getPasswrd(){
        return this.passwrd;
    }
    public String getRepaswrd(){
        return this.repaswrd;
    }
    public String getEmail(){
        return this.email;
    }
    public String getAge(){
        return this.age;
    }
    public String getPhoneno(){
        return this.phoneno;
    }
    public String getCollege(){
        return this.college;
    }
    public String getCourse(){
        return this.course;
    }
}

Error:

E/SQLiteLog: (1) table Users has no column named Course 01-25 22:45:31.414 12064-12064/com.pixel.sri.justdoit E/SQLiteDatabase: Error inserting Course=B.E ReEnterPassword=sri Passwrod=sri Email=sriram.mdu31@gmail.com College=APEC PhoneNumber=7402043073 Age=21 Name=sriram android.database.sqlite.SQLiteException: table Users has no column named Course (code 1): , while compiling: INSERT INTO Users(Course,ReEnterPassword,Passwrod,Email,College,PhoneNumber,Age,Name) VALUES (?,?,?,?,?,?,?,?) at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887) at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498) at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) at android.database.sqlite.SQLiteProgram.(SQLiteProgram.java:58) at android.database.sqlite.SQLiteStatement.(SQLiteStatement.java:31) at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469) at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341) at com.pixel.sri.justdoit.DatabaseHandler.addUser(DatabaseHandler.java:66) at com.pixel.sri.justdoit.RegisterActivity$1.onClick(RegisterActivity.java:81) at android.view.View.performClick(View.java:5201) at android.view.View$PerformClick.run(View.java:21163) at android.os.Handler.handleCallback(Handler.java:746) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5443) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 01-25 22:45:31.572 12064-12089/com.pixel.sri.justdoit V/RenderScript: 0xb8b446f0 Launching thread(s), CPUs 8

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Sriram
  • 1
  • 1

2 Answers2

1

You probably added that column after a previous run, when the database was already created.

Solution: Uninstall and reinstall your app.
Alternate solution: Increase the DATABASE_VERSION constant value.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
1

This is common occurrence if the Course column is a recent addition to your database, but you forgot to increment the database version.

With the code you have, bumping your database version will wipe the database and recreate the database with the correct columns.

Alternatively, uninstalling and reinstalling your app should fix it.

Bryan Herbst
  • 66,602
  • 10
  • 133
  • 120