在数据库中插入新元素时出现sqlite异常

时间:2021-12-20 20:31:07

I use sqlite in android I get this error

我在android中使用sqlite我得到了这个错误

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

android.database.sqlite.SQLiteException:表老师没有名为password的密码(代码1):,编译时:INSERT INTO老师(姓名,电子邮箱,密码,id)VALUES(?,?,?,?)

and I send the data correctly to DB class

我正确地将数据发送到DB类

here is mycode

这是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;
    }
}

2 个解决方案

#1


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.

在TEATCHER_KEY_EMAIL之后的上述语句中,您已经为该列使用了TEXT,但是忘记在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" +")";

#2


1  

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

在TABLE_TEATCHER和TABLE_STUDENT表中的TEXT之后为TEATCHER_KEY_EMAIL和STUDENT_KEY_PASSWORD字段添加“,”

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" +")";

#1


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.

在TEATCHER_KEY_EMAIL之后的上述语句中,您已经为该列使用了TEXT,但是忘记在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" +")";

#2


1  

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

在TABLE_TEATCHER和TABLE_STUDENT表中的TEXT之后为TEATCHER_KEY_EMAIL和STUDENT_KEY_PASSWORD字段添加“,”

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" +")";