我在Android工作室开发Android应用程序,同时创建我的SQLite数据库。但是,我的代码出现了错误,我已经查看了其他问题和答案,而且我的代码看起来不应该与问题答案相比显示错误。我已在下面添加了我的数据库和实体类,但由于数据库尚未正常工作,因此尚未创建DBhandler类。任何帮助都会被证实
数据库类:
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);
}
}
学生班:
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 + '\'' +
'}';
}
}
职员班:
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 + '\'' +
'}';
}
}
课程类:
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 + '\'' +
'}';
}
}
答案 0 :(得分:4)
您的主要问题是列名称不能包含空格。所以,所有这些都需要修复:
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";
请记住:这些是数据库中的列名,而不是面向用户的字符串。
此外,CREATE_TABLE_STUDENTS
和CREATE_TABLE_STAFF
缺少)
来关闭列列表。例如,在CREATE_TABLE_STUDENTS
中,将+ Student.KEY_COURSEID + " TEXT, "
替换为+ Student.KEY_COURSEID + " TEXT) "