没有这样的表:main.course_name

时间:2018-08-19 19:26:45

标签: java android android-sqlite

起初没有这样的table:teacher_table,读过的帖子告诉我卸载该操作并创建了该表,但是当尝试在其中插入数据时,我遇到了另一个错误

在插入教师密码= 123老师名称= soukat时出现错误教师_电子邮件=a@a.com course_id = 1 Teacher_mobile = 12345432 admin_id = 1 Teacher_fname = galina Teacher_no = tr001 Teacher_username = mali   :没有这样的表:main.course_name(code 1):,编译时:INSERT INTO Teacher_table(teacher_password,teacher_lname,teacher_email,course_id,teacher_mobile,admin_id,teacher_fname,teacher_no,teacher_username)VALUES(?,?,?,?,? ,?,?,?,?)  不幸的是,我没有编写任何查询来创建导致错误的上表。

  

SitmDatabaseHelper.java

public class SitmDatabaseHelper extends SQLiteOpenHelper {
    private static String dbName = "sitm_db.db";
    private static int dbVersion =15;
    private final static String dg = "databaseTest";
    public SitmDatabaseHelper(Context context) {
        super(context, dbName, null, dbVersion);
        Log.d(dg, "SitmDatabaseHelper: db created");
    }
    //this method when called returns an instance of the helper class

    public static SitmDatabaseHelper getDatabaseHelper(Context context){
        SitmDatabaseHelper instance;

            instance = new SitmDatabaseHelper(context);


        return  instance;
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
       try{
           Log.d(dg, "onCreate: ONCREATE METHOD CALLED");
           //creating the admin table
           db.execSQL(AdminTable.adminTableQuery);
           db.execSQL(StaffTable.staffTableQuery);
           db.execSQL(CourseTable.courseTableQuery);
           db.execSQL(BatchTable.batchTableQuery);
           db.execSQL(TeacherTable.teacherTableQuery);

       }catch (Exception e){

           Log.d("TableCreation", "onCreate: table creation failed at "+e.getMessage());
       }

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
       //dropping older table if exists

        try{
            db.execSQL(AdminTable.dropAdminQuery);
            db.execSQL(StaffTable.dropStaffQuery);
            db.execSQL(CourseTable.dropCourseQuery);
            db.execSQL(BatchTable.dropTableQuery);
            db.execSQL(TeacherTable.dropTeacherQuery);
        }catch (Exception e){
            Log.d("dropTables", "onUpgrade: failed to drop table "+e.getMessage());
        }
        onCreate(db);
    }

    @Override //overriden to enable foreign keys
    public void onOpen(SQLiteDatabase db) {
        db.execSQL("PRAGMA foreign_keys=ON");
    }

    @Override
    public void onConfigure(SQLiteDatabase db) {
        db.setForeignKeyConstraintsEnabled(true);
    }
}
  

TeacherTable.java

 public final class TeacherTable {
    //public static final String TEACHER_TABLE,teacherId,teacherNo,teacherFname,teacherLname,teacherUsername,teacherEmail, teacherPassword,
    //
        public static final String TEACHER_TABLE="teacher_table",
        teacherId="teacher_id",
        teacherNo="teacher_no",
        teacherFname="teacher_fname",
        teacherLname="teacher_lname",
        teacherUsername="teacher_username",
        teacherEmail="teacher_email",
        teacherPassword="teacher_password",
        teacherMobile="teacher_mobile",
        teacherDate="teacher_date",
        adminId="admin_id",
        courseId="course_id";




        public static final String teacherTableQuery="CREATE TABLE IF NOT EXISTS "+TeacherTable.TEACHER_TABLE+"("
                +teacherId+" INTEGER PRIMARY KEY AUTOINCREMENT, "
                +teacherNo+" TEXT,"
                +teacherFname+" TEXT, "
                +teacherLname+" TEXT, "
                +teacherUsername+" TEXT,"
                +teacherEmail+" TEXT,"
                +teacherPassword+" TEXT,"
                +teacherMobile+" TEXT,"
                +adminId+" INTEGER NOT NULL,"
                +courseId+" INTEGER NOT NULL, "
                +"FOREIGN KEY("+adminId+")"+" REFERENCES "+AdminTable.adminTable+"("+AdminTable.sId +"),"
                +"FOREIGN KEY("+courseId+")"+" REFERENCES "+CourseTable.courseName+"("+CourseTable.courseID+")"
                +")";



        public final static String dropTeacherQuery = "DROP TABLE IF EXISTS "+ TEACHER_TABLE;

    }

>**TeacherDao.java ("add method")**
public class TeacherDao extends DBconnectionHelper {
    public TeacherDao(Context tcontext) {
        super(tcontext);
    }

    //add teacher
    public void addTeacher(TeacherModal teacherModal){
        long status;
        //connection
        openWritableDatabase();
        ContentValues contentValues = new ContentValues();

        contentValues.put(TeacherTable.adminId , teacherModal.getAdmin().getSuper_admin_id());
        contentValues.put(TeacherTable.courseId,teacherModal.getCourse().getCourse_id());
        contentValues.put(TeacherTable.teacherNo,teacherModal.getTeacher_no());
        contentValues.put(TeacherTable.teacherFname , teacherModal.getTeacher_fname());
        contentValues.put(TeacherTable.teacherLname , teacherModal.getTeacher_lname());
        contentValues.put(TeacherTable.teacherUsername,teacherModal.getTeacher_username());
        contentValues.put(TeacherTable.teacherEmail , teacherModal.getTeacher_email());
        contentValues.put(TeacherTable.teacherPassword,teacherModal.getTeacher_password());
        contentValues.put(TeacherTable.teacherMobile ,teacherModal.getTeacher_mobile());

          try{
               database.insert(TeacherTable.TEACHER_TABLE, null , contentValues);
          }catch (Exception e){
              Log.d("TeacherTable" , "reason "+e.getMessage());
              e.printStackTrace();
          }

       // close connection
        closeDatabase();
        //return status;
    }
  

AddTeacherFragment.java

 public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //DAOs
        coursesDao = new CoursesDao(getActivity());
        teacherDao = new TeacherDao(getActivity());
        teacherDao.addTeacher(new TeacherModal("tr001","galina","soukat","mali",
                "a@a.com","123","12345432",new SuperAdminModal(1),new CourseModal(1)));
       }

1 个答案:

答案 0 :(得分:0)

  

不幸的是,我没有编写任何查询来创建上表   导致错误。

假设您真的要说我没有编写任何查询以使用上表;您可以通过以下方式隐式地定义 teacher_table 表:-

    +"FOREIGN KEY("+courseId+")"+" REFERENCES "+CourseTable.courseName+"("+CourseTable.courseID+")"

要在 teacher_table 中插入 courseId 列中的值必须与指定列匹配(无论 {{1 }} 解析为 course_name 表中的内容(即 CourseTable.courseID 解析为 course_name )。

但是,由于表 course_name 不存在,甚至无法检查FOREIGN KEY约束。因此失败了。

您必须定义 course_name 表,并且还必须使用适当的值填充该表,除非您删除FOREIGN KEY约束。