0

I use sqlite in android I get this error

android.database.sqlite.SQLiteException: table teacher has no column named password (code 1): , while compiling: INSERT INTO teacher(name,email,password,id) VALUES (?,?,?,?)

and I send the data correctly to DB class

here is mycode

package com.example.cm.myapplication.Model;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;


public class DatabaseHandler extends SQLiteOpenHelper {


    // All Static variables
    // Database Version
    private static final int DATABASE_VERSION = 1;

    // Database Name
    private static final String DATABASE_TEACHER_NAME = "CourseManager";

    Context context;
    // teacher table name
    private static final String TABLE_TEATCHER = "teacher";

    // person Table Columns names
    private static final String TEATCHER_KEY_ID = "id";
    private static final String TEATCHER_KEY_NAME = "name";
    private static final String TEATCHER_KEY_EMAIL = "email";
    private static final String TEATCHER_KEY_PASSWORD = "password";


    // teacher student name
    private static final String TABLE_STUDENT = "student";

    // person Table Columns names
    private static final String STUDENT_KEY_ID = "id";
    private static final String STUDENT_KEY_NAME = "name";
    private static final String STUDENT_KEY_EMAIL = "email";
    private static final String STUDENT_KEY_PASSWORD = "password";

    public DatabaseHandler(Context context) {
        super(context, DATABASE_TEACHER_NAME, null, DATABASE_VERSION);
        this.context = context;
    }

    // Creating Tables
    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_TEATCHER_TABLE = "CREATE TABLE " + TABLE_TEATCHER + "("
                + TEATCHER_KEY_ID + " INTEGER PRIMARY KEY," + TEATCHER_KEY_NAME + " TEXT,"
                + TEATCHER_KEY_EMAIL + " TEXT"
                + TEATCHER_KEY_PASSWORD + " TEXT" +")";


        String CREATE_STUDENT_TABLE = "CREATE TABLE " + TABLE_STUDENT + "("
                + STUDENT_KEY_ID + " INTEGER PRIMARY KEY," + STUDENT_KEY_NAME + " TEXT,"
                + STUDENT_KEY_PASSWORD + " TEXT"
                + STUDENT_KEY_EMAIL + " TEXT" +")";

        db.execSQL(CREATE_TEATCHER_TABLE);
        db.execSQL(CREATE_STUDENT_TABLE);
    }

    // Upgrading database
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_TEATCHER);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_STUDENT);

        // Create tables again
        onCreate(db);
    }


    // Adding new doctor table
    public long addTeacher(Doctor doctor) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(TEATCHER_KEY_ID, doctor.getId());
        values.put(TEATCHER_KEY_EMAIL, doctor.getEmail());
        values.put(TEATCHER_KEY_NAME, doctor.getName());
        values.put(TEATCHER_KEY_PASSWORD, doctor.getPass());

        // Inserting Row
        long i = db.insert(TABLE_TEATCHER, null, values);

        db.close(); // Closing database connection
        return i ;
    }
    // Adding new student
    public long addStudent(Student student) {
        SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
        values.put(STUDENT_KEY_ID, student.getId());
        values.put(STUDENT_KEY_EMAIL, student.getEmail());
        values.put(STUDENT_KEY_NAME, student.getName());
        values.put(STUDENT_KEY_PASSWORD, student.getPass());

        // Inserting Row
        long i = db.insert(TABLE_STUDENT, null, values);

        db.close(); // Closing database connection
        return i;
    }

    public int getDoctorcount(){

        String countQuery = "SELECT COUNT(*) FROM " +TABLE_TEATCHER;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);
        cursor.moveToFirst();
        Integer count= cursor.getCount();
        cursor.close();

        // return count
        return count;
    }
    public int getStudentcount(){
        String countQuery = "SELECT COUNT(*) FROM " +TABLE_STUDENT;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);
        cursor.moveToFirst();
        Integer count= cursor.getCount();
        cursor.close();

        // return count
        return count;
    }
}
1
  • u added this column after creation of table that`s why clear data of app or uninstall app and after run your app. Commented Aug 19, 2017 at 12:06

2 Answers 2

1

I think mistake in the create statement please check this

String CREATE_TEATCHER_TABLE = "CREATE TABLE " + TABLE_TEATCHER + "("
                + TEATCHER_KEY_ID + " INTEGER PRIMARY KEY," + TEATCHER_KEY_NAME + " TEXT,"
                + TEATCHER_KEY_EMAIL + " TEXT"
                + TEATCHER_KEY_PASSWORD + " TEXT" +")";

In above statement after TEATCHER_KEY_EMAIL u have used TEXT for that column but u forget to add "," after TEXT.

Use below statement than try again

String CREATE_TEATCHER_TABLE = "CREATE TABLE " + TABLE_TEATCHER + "("
                + TEATCHER_KEY_ID + " INTEGER PRIMARY KEY," + TEATCHER_KEY_NAME + " TEXT,"
                + TEATCHER_KEY_EMAIL + " TEXT,"
                + TEATCHER_KEY_PASSWORD + " TEXT" +")";
Sign up to request clarification or add additional context in comments.

Comments

1

Add "," after TEXT in TABLE_TEATCHER and TABLE_STUDENT table for TEATCHER_KEY_EMAIL & STUDENT_KEY_PASSWORD field

String CREATE_TEATCHER_TABLE = "CREATE TABLE " + TABLE_TEATCHER + "("
            + TEATCHER_KEY_ID + " INTEGER PRIMARY KEY," + TEATCHER_KEY_NAME + " TEXT,"
            + TEATCHER_KEY_EMAIL + " TEXT,"
            + TEATCHER_KEY_PASSWORD + " TEXT" +")";


String CREATE_STUDENT_TABLE = "CREATE TABLE " + TABLE_STUDENT + "("
        + STUDENT_KEY_ID + " INTEGER PRIMARY KEY," + STUDENT_KEY_NAME + " TEXT,"
        + STUDENT_KEY_PASSWORD + " TEXT,"
        + STUDENT_KEY_EMAIL + " TEXT" +")";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.