I have a problem with these blocks of code. I think its a SQL problem I am having. This code is supposed to create a student table and insert data from my android application into the student table. I am getting a error saying the courseid column doesn't exist. I think the problem has to do with courseid being declared a primary key in my course table.
Creating Course table:
public static String createTable(){
return "CREATE TABLE " + DbSchema.TABLE_Course + "("
+ DbSchema.KEY_CourseId + " PRIMARY KEY ,"
+ DbSchema.KEY_Name_Course + " TEXT )";
}
Creating Student table:
public static String createTable(){
return "CREATE TABLE " + DbSchema.TABLE_Student + "("
+ DbSchema.KEY_StudID + " PRIMARY KEY ,"
+ DbSchema.KEY_Name_Student + " TEXT, "
+ DbSchema.KEY_Year_Student + " TEXT, "
+ DbSchema.KEY_Email_Student+ " TEXT,"
+ DbSchema.KEY_CourseId+" TEXT )";
}
Inserting into table:
public void insert(Student student) {
SQLiteDatabase db = DatabaseManager.getInstance().openDatabase();
ContentValues values = new ContentValues();
values.put(DbSchema.KEY_StudID, student.getid());
values.put(DbSchema.KEY_Name_Student, student.getname());
values.put(DbSchema.KEY_Year_Student, student.getYear());
values.put(DbSchema.KEY_Email_Student, student.getEmail());
values.put(DbSchema.KEY_CourseId, student.getCourseid());
db.insert(DbSchema.TABLE_Student, null, values);
DatabaseManager.getInstance().closeDatabase();
Create Table command:
public DBHelper( ) {
super(App.getContext(), DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
//All necessary tables you like to create will create here
db.execSQL(CourseDbCommands.createTable());
db.execSQL(StudentDbCommands.createTable());
There is no space between DbSchema.TABLE_Course + "("
and DbSchema.KEY_CourseId + "PRIMARY KEY
Student table as also have same spacing problem.
DbSchema.TABLE_Student + "("
or
check that student course id already exist on the course table
I don't see any foreign key reference in your student table creation command. for reference, you can check this enter link description here
I also assume you already have some entry in course table to map with student table. ie before you insert a student with a particular course in student table that course should be present in course table.
I figured it out I had to change the version of my database manually. I thought changes automatically happened.
public static String createTable(){
return "CREATE TABLE " + DbSchema.TABLE_Student + "("
+ DbSchema.KEY_StudID + " INTEGER PRIMARY KEY ,"
+ DbSchema.KEY_Name_Student + " TEXT, "
+ DbSchema.KEY_Year_Student + " TEXT, "
+ DbSchema.KEY_Email_Student+ " TEXT,"
+ DbSchema.KEY_CourseId+" TEXT )";
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.