Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'(' , ')', <column constraint> or comma expected, got 'TEXT' SQLite database error in android studio

I'm developing an Android app in Android studio and whilst creating my SQLite database. However I've run into an error with my code, I've looked around at other questions and answer and my code doesn't look like it should be presenting the error in comparison to the answers on questions. I've added my database and entity class below but haven't created a DBhandler class as the database isn't yet working. any help would be appericated

Database class:

    package com.example.ajhillie.btc_ucs_ma.Database;

    import android.content.Context;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteOpenHelper;

    import com.example.ajhillie.btc_ucs_ma.Entities.Course;
    import com.example.ajhillie.btc_ucs_ma.Entities.Staff;
    import com.example.ajhillie.btc_ucs_ma.Entities.Student;

    public class BTC_UCS_Database extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "BTCUCSdatabase";
    private Context c;

    public BTC_UCS_Database(Context context) {
       super(context, DATABASE_NAME, null, DATABASE_VERSION);
       this.c = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
       String CREATE_TABLE_STUDENTS = "CREATE TABLE " + Student.TABLE + "("
            + Student.KEY_STUDENTID + " TEXT PRIMARY KEY, "
            + Student.KEY_PASSWORD + " TEXT, "
            + Student.KEY_FIRSTNAME + " TEXT, "
            + Student.KEY_SURNAME + " TEXT, "
            + Student.KEY_ADDRESSONE + " TEXT, "
            + Student.KEY_ADDRESSTWO + " TEXT, "
            + Student.KEY_TOWNCITY + " TEXT, "
            + Student.KEY_POSTCODE + " TEXT, "
            + Student.KEY_MOBILENO + " BIGINT, "
            + Student.KEY_LANDLINENO + " BIGINT,"
            + Student.KEY_PEMAIL + " TEXT, "
            + Student.KEY_CEMAIL + " TEXT, "
            + Student.KEY_COURSEID + " TEXT, "
            + " FOREIGN KEY (KEY_COURSEID) REFERENCES " + Course.TABLE + " 
            (KEY_COURSEID))";

    String CREATE_TABLE_STAFF = "CREATE TABLE " + Staff.TABLE + "("
            + Staff.KEY_STAFFID + " TEXT PRIMARY KEY, "
            + Staff.KEY_PASSWORD + " TEXT, "
            + Staff.KEY_FIRSTNAME + " TEXT, "
            + Staff.KEY_SURNAME + " TEXT, "
            + Staff.KEY_ADDRESSONE + " TEXT, "
            + Staff.KEY_ADDRESSTWO + " TEXT, "
            + Staff.KEY_TOWNCITY + " TEXT, "
            + Staff.KEY_POSTCODE + " TEXT, "
            + Staff.KEY_MOBILENO + " BIGINT, "
            + Staff.KEY_LANDLINENO + " BIGINT, "
            + Staff.KEY_PEMAIL + " TEXT, "
            + Staff.KEY_CEMAIL + " TEXT, "
            + Staff.KEY_COURSEID + " TEXT REFERENCES " + Course.TABLE + " 
            (KEY_COURSEID))";

    String CREATE_TABLE_COURSES = "CREATE TABLE " + Course.TABLE + "("
            + Course.KEY_COURSEID + " TEXT PRIMARY KEY, "
            + Course.KEY_COURSENAME + " TEXT, "
            + Course.KEY_COURSEDES + " TEXT, "
            + Course.KEY_COURSECONTENTONE + " TEXT, "
            + Course.KEY_COURSECONTENTTWO + " TEXT, "
            + Course.KEY_COURSECONTENTTHREE + " TEXT, "
            + Course.KEY_COURSECONTENTFOUR + " TEXT, "
            + Course.KEY_COURSEPRICE + " TEXT, "
            + Course.KEY_COURSELENGTH + " TEXT)";

    db.execSQL(CREATE_TABLE_STUDENTS);
    db.execSQL(CREATE_TABLE_STAFF);
    db.execSQL(CREATE_TABLE_COURSES);


    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
    {
       if (oldVersion < newVersion) {
          db.execSQL("DROP TABLE IF EXISTS " + Student.TABLE);
          db.execSQL("DROP TABLE IF EXISTS " + Staff.TABLE);
          db.execSQL("DROP TABLE IF EXISTS " + Course.TABLE);
          onCreate(db);
      }while (newVersion < oldVersion);
     }
    }

Student class:

package com.example.ajhillie.btc_ucs_ma.Entities;

import android.content.Context;
import android.widget.Toast;

public class Student {

private String StudentID;
private String  Password;
private String  FirstName;
private String  Surname;
private String  Address1;
private String  Address2;
private String  Town_City;
private String  Postcode;
private long  MobileNo;
private long  LandlineNo;
private String  pEmail;
private String  cEmail;
private String  CourseID;



public String getStudentID() {
    return StudentID;
}

public void setStudentID(String studentID) {
    StudentID = studentID;
}

public String getPassword() {
    return Password;
}

public void setPassword(String password) {
    Password = password;
}

public String getFirstName() {
    return FirstName;
}

public void setFirstName(String firstName) {
    FirstName = firstName;
}

public String getSurname() {
    return Surname;
}

public void setSurname(String surname) {
    Surname = surname;
}

public String getAddress1() {
    return Address1;
}

public void setAddress1(String address1) {
    Address1 = address1;
}

public String getAddress2() {
    return Address2;
}

public void setAddress2(String address2) {
    Address2 = address2;
}

public String getTown_City() {
    return Town_City;
}

public void setTown_City(String town_City) {
    Town_City = town_City;
}

public String getPostcode() {
    return Postcode;
}

public void setPostcode(String postcode) {
    Postcode = postcode;
}

public long getMobileNo() {
    return MobileNo;
}

public void setMobileNo(long mobileNo) {
    MobileNo = mobileNo;
}

public long getLandlineNo() {
    return LandlineNo;
}

public void setLandlineNo(long landlineNo) {
    LandlineNo = landlineNo;
}

public String getpEmail() {
    return pEmail;
}

public void setpEmail(String PEmail) {
    this.pEmail = PEmail;
}

public String getcEmail() {
    return cEmail;
}

public void setcEmail(String CEmail) {
    this.cEmail = CEmail;
}

public String getCourseID() {
    return CourseID;
}

public void setCourseID(String courseID) {
    CourseID = courseID;
}

public static String TABLE = "Students";

public static final String KEY_STUDENTID = "Student ID";
public static final String KEY_PASSWORD = "Student Password";
public static final String KEY_FIRSTNAME = "First Name";
public static final String KEY_SURNAME = "Surname";
public static final String KEY_ADDRESSONE = "Address Line 1";
public static final String KEY_ADDRESSTWO = "Address Line 2";
public static final String KEY_TOWNCITY = "Town or City";
public static final String KEY_POSTCODE = "Postcode";
public static final String KEY_MOBILENO = "Mobile Number";
public static final String KEY_LANDLINENO = "Home Number";
public static final String KEY_PEMAIL = "Personal Email";
public static final String KEY_CEMAIL = "College Email";
public static final String KEY_COURSEID = "Course ID";

public Student(String studentID, String password, String firstName, String 
surname, String address1, String address2, String town_City, String 
postcode, long mobileNo, long landlineNo, String PEmail, String CEmail, 
String courseID, Context context) {

    StudentID = studentID;;
    Password = password;
    FirstName = firstName;
    Surname = surname;
    Address1 = address1;
    Address2 = address1;
    Town_City = town_City;
    Postcode = postcode;
    MobileNo = mobileNo;
    LandlineNo = landlineNo;
    pEmail = PEmail;
    cEmail = CEmail;
    CourseID = courseID;

    Toast.makeText(context, "Student account successfully created!!!!", 
    Toast.LENGTH_SHORT).show();
}

@Override
public String toString() {
    return "Student{" +
            "StudentID='" + StudentID + '\'' +
            ", Password='" + Password + '\'' +
            ", FirstName='" + FirstName + '\'' +
            ", Surname='" + Surname + '\'' +
            ", Address1='" + Address1 + '\'' +
            ", Address2='" + Address2 + '\'' +
            ", Town_City='" + Town_City + '\'' +
            ", Postcode='" + Postcode + '\'' +
            ", MobileNo=" + MobileNo +
            ", LandlineNo=" + LandlineNo +
            ", pEmail='" + pEmail + '\'' +
            ", cEmail='" + cEmail + '\'' +
            ", CourseID='" + CourseID + '\'' +
            '}';
 }
}

Staff class:

package com.example.ajhillie.btc_ucs_ma.Entities;

import android.content.Context;
import android.widget.Toast;

public class Staff {


private String StaffID;
private String Username;
private String  Password;
private String  FirstName;
private String  Surname;
private String  Address1;
private String  Address2;
private String  Town_City;
private String  Postcode;
private long  MobileNo;
private long  LandlineNo;
private String  pEmail;
private String  cEmail;
private String  CourseID;

public String getStaffID() {
    return StaffID;
}

public void setStafftID(String staffID) {
    StaffID = staffID;
}

public String getPassword() {
    return Password;
}

public void setPassword(String password) {
    Password = password;
}

public String getFirstName() {
    return FirstName;
}

public void setFirstName(String firstName) {
    FirstName = firstName;
}

public String getSurname() {
    return Surname;
}

public void setSurname(String surname) {
    Surname = surname;
}

public String getAddress1() {
    return Address1;
}

public void setAddress1(String address1) {
    Address1 = address1;
}

public String getAddress2() {
    return Address2;
}

public void setAddress2(String address2) {
    Address2 = address2;
}

public String getTown_City() {
    return Town_City;
}

public void setTown_City(String town_City) {
    Town_City = town_City;
}

public String getPostcode() {
    return Postcode;
}

public void setPostcode(String postcode) {
    Postcode = postcode;
}

public long getMobileNo() {
    return MobileNo;
}

public void setMobileNo(long mobileNo) {
    MobileNo = mobileNo;
}

public long getLandlineNo() {
    return LandlineNo;
}

public void setLandlineNo(long landlineNo) {
    LandlineNo = landlineNo;
}

public String getpEmail() {
    return pEmail;
}

public void setpEmail(String PEmail) {
    this.pEmail = PEmail;
}

public String getcEmail() {
    return cEmail;
}

public void setcEmail(String CEmail) {
    this.cEmail = CEmail;
}

public String getCourseID() {
    return CourseID;
}

public void setCourseID(String courseID) {
    CourseID = courseID;
}

public static String TABLE = "Staff";

public static final String KEY_STAFFID = "Student ID";
public static final String KEY_PASSWORD = "Staff Password";
public static final String KEY_FIRSTNAME = "First Name";
public static final String KEY_SURNAME = "Surname";
public static final String KEY_ADDRESSONE = "Address Line 1";
public static final String KEY_ADDRESSTWO = "Address Line 2";
public static final String KEY_TOWNCITY = "Town or City";
public static final String KEY_POSTCODE = "Postcode";
public static final String KEY_MOBILENO = "Mobile Number";
public static final String KEY_LANDLINENO = "Home Number";
public static final String KEY_PEMAIL = "Personal Email";
public static final String KEY_CEMAIL = "College Email";
public static final String KEY_COURSEID = "Course ID";

public Staff(String staffID, String password, String firstName, String 
surname, String address1, String address2, String town_City, String 
postcode, long mobileNo, long landlineNo, String PEmail, String CEmail, 
String courseID, Context context) {

    StaffID = staffID;
    Password = password;
    FirstName = firstName;
    Surname = surname;
    Address1 = address1;
    Address2 = address1;
    Town_City = town_City;
    Postcode = postcode;
    MobileNo = mobileNo;
    LandlineNo = landlineNo;
    pEmail = PEmail;
    cEmail = CEmail;
    CourseID = courseID;

    Toast.makeText(context, "Staff account successfully created!!!!", 
    Toast.LENGTH_SHORT).show();
}

@Override
public String toString() {
    return "Staff{" +
            "StaffID='" + StaffID + '\'' +
            ", Password='" + Password + '\'' +
            ", FirstName='" + FirstName + '\'' +
            ", Surname='" + Surname + '\'' +
            ", Address1='" + Address1 + '\'' +
            ", Address2='" + Address2 + '\'' +
            ", Town_City='" + Town_City + '\'' +
            ", Postcode='" + Postcode + '\'' +
            ", MobileNo=" + MobileNo +
            ", LandlineNo=" + LandlineNo +
            ", pEmail='" + pEmail + '\'' +
            ", cEmail='" + cEmail + '\'' +
            ", CourseID='" + CourseID + '\'' +
            '}';
  }
}

Course class:

package com.example.ajhillie.btc_ucs_ma.Entities;

import android.content.Context;
import android.widget.Toast;

public class Course {

private String CourseID;
private String CourseName;
private String CourseDes;
private String CourseContent1;
private String CourseContent2;
private String CourseContent3;
private String CourseContent4;
private String CoursePrice;
private String CourseLength;

public String getCourseID() {
    return CourseID;
}

public void setCourseID(String courseID) {
    CourseID = courseID;
}

public String getCourseName() {
    return CourseName;
}

public void setCourseName(String courseName) {
    CourseName = courseName;
}

public String getCourseDes() {
    return CourseDes;
}

public void setCourseDes(String courseDes) {
    CourseDes = courseDes;
}

public String getCourseContent1() {
    return CourseContent1;
}

public void setCourseContent1(String courseContent1) {
    CourseContent1 = courseContent1;
}

public String getCourseContent2() {
    return CourseContent2;
}

public void setCourseContent2(String courseContent2) {
    CourseContent2 = courseContent2;
}

public String getCourseContent3() {
    return CourseContent3;
}

public void setCourseContent3(String courseContent3) {
    CourseContent3 = courseContent3;
}

public String getCourseContent4() {
    return CourseContent4;
}

public void setCourseContent4(String courseContent4) {
    CourseContent4 = courseContent4;
}

public String getCoursePrice() {
    return CoursePrice;
}

public void setCoursePrice(String coursePrice) {
    CoursePrice = coursePrice;
}

public String getCourseLength() {
    return CourseLength;
}

public void setCourseLength(String courseLength) {
    CourseLength = courseLength;
}

public static String TABLE = "Courses";

public static final String KEY_COURSEID = "Course ID";
public static final String KEY_COURSENAME = "Course NAme";
public static final String KEY_COURSEDES = "Course Description";
public static final String KEY_COURSECONTENTONE = "Course Content 1";
public static final String KEY_COURSECONTENTTWO = "Course Content 2";
public static final String KEY_COURSECONTENTTHREE = "Course Content 3";
public static final String KEY_COURSECONTENTFOUR = "Course Content 4";
public static final String KEY_COURSEPRICE = "Course Price";
public static final String KEY_COURSELENGTH = "Course Length";

public Course(String courseID, String courseName, String courseDes, String 
courseContent1, String courseContent2, String courseContent3, String 
courseContent4, String coursePrice, String courseLength, Context context){
    CourseID = courseID;
    CourseName = courseName;
    CourseDes = courseDes;
    CourseContent1 = courseContent1;
    CourseContent2 = courseContent2;
    CourseContent3 = courseContent3;
    CourseContent4 = courseContent4;
    CoursePrice = coursePrice;
    CourseLength= courseLength;

    Toast.makeText(context, "Course successfully added to the database", 
    Toast.LENGTH_SHORT).show();
}

@Override
public String toString() {
    return "Course{" +
            "CourseID='" + CourseID + '\'' +
            ", CourseName='" + CourseName + '\'' +
            ", CourseDes='" + CourseDes + '\'' +
            ", CourseContent1='" + CourseContent1 + '\'' +
            ", CourseContent2='" + CourseContent2 + '\'' +
            ", CourseContent3='" + CourseContent3 + '\'' +
            ", CourseContent4='" + CourseContent4 + '\'' +
            ", CoursePrice='" + CoursePrice + '\'' +
            ", CourseLength='" + CourseLength + '\'' +
            '}';
   }
 }

Error Screenshot

like image 783
Jordan Southey Avatar asked Jan 29 '23 00:01

Jordan Southey


1 Answers

Your primary problem is that column names cannot contains spaces. So, all of these need to be fixed:

public static final String KEY_STUDENTID = "Student ID";
public static final String KEY_PASSWORD = "Student Password";
public static final String KEY_FIRSTNAME = "First Name";
public static final String KEY_ADDRESSONE = "Address Line 1";
public static final String KEY_ADDRESSTWO = "Address Line 2";
public static final String KEY_TOWNCITY = "Town or City";
public static final String KEY_MOBILENO = "Mobile Number";
public static final String KEY_LANDLINENO = "Home Number";
public static final String KEY_PEMAIL = "Personal Email";
public static final String KEY_CEMAIL = "College Email";
public static final String KEY_COURSEID = "Course ID";

Remember: these are column names in a database, not user-facing strings.

Also, CREATE_TABLE_STUDENTS and CREATE_TABLE_STAFF are missing a ) to close the list of columns. For example, in CREATE_TABLE_STUDENTS, replace + Student.KEY_COURSEID + " TEXT, " with + Student.KEY_COURSEID + " TEXT) "

like image 103
CommonsWare Avatar answered Jan 31 '23 09:01

CommonsWare