0

我正在构建一个可以教孩子们如何写作的 Android 应用程序。因此,我需要保存有关每个孩子的某些信息以及带有特定字母的结果。我正处于构建数据库的阶段,并且一直在使用 Youtube 上的教程,但我遇到了很多错误:

1) table studentTable has no column named studentSchool
    Error inserting studentSurname=fdfsfdf studentSchool=fdfdfefe studentForname=gdgdfdf
    android.database.sqlite.SQLiteException: table studentTable has no column named studentSchool (code 1): , while compiling: INSERT INTO studentTable(studentSurname,studentSchool,studentForname) VALUES (?,?,?)
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
        at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
        at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
        at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
        at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
        at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
        at com.example.letterwriter.Database.createStudentEntry(Database.java:153)
        at com.example.letterwriter.RegStudentActivity.register(RegStudentActivity.java:42)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at android.view.View$1.onClick(View.java:3594)
        at android.view.View.performClick(View.java:4204)
        at android.view.View$PerformClick.run(View.java:17355)
        at android.os.Handler.handleCallback(Handler.java:725)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5041)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
        at dalvik.system.NativeStart.main(Native Method)

最大的错误是我目前的主要舞会。我搜索并发现很多有类似问题的人,但没有一个解决方案适合我。我开始怀疑是否还有其他问题导致其他解决方案无法正常工作。

private static final String DATABASE_NAME = "letterwriter"; // Database Name
private static final int DATABASE_VERSION = 4; // Database Version  
private static final String TABLE_STUDENT = "studentTable";  // Student table name
private static final String TABLE_TEACHER = "teacherTable";  // Teacher table name
private static final String TABLE_RESULTS = "resultsTable";  // Results table name

//Student columns
private static final String KEY_sID = "studentId";
private static final String KEY_sSNAME = "studentSurname";
private static final String KEY_sFNAME = "studentForname";
private static final String KEY_sSCHOOL = "studentSchool";
private static final String KEY_t_ID = "teacherId";
private static final String KEY_t_SNAME = "teacherSurname";

//Teacher columns
private static final String KEY_tID = "teacherId";
//private static final String KEY_PW = "password";
private static final String KEY_tSNAME = "teacherSurname";
private static final String KEY_tFNAME = "teacherForename";
private static final String KEY_tSCHOOL = "teahcerSchool";

//Student columns
private static final String KEY_s_ID = "student id";
private static final String KEY_aRes = "A";
private static final String KEY_bRes = "B";
private static final String KEY_cRes = "C";
private static final String KEY_dRes = "D";
private static final String KEY_eRes = "E";
private static final String KEY_fRes = "F";
private static final String KEY_gRes = "G";
private static final String KEY_hRes = "H";
private static final String KEY_iRes = "I";
private static final String KEY_jRes = "J";
private static final String KEY_kRes = "K";
private static final String KEY_lRes = "L";
private static final String KEY_mRes = "M";
private static final String KEY_nRes = "N";
private static final String KEY_oRes = "O";
private static final String KEY_pRes = "P";
private static final String KEY_qRes = "Q";
private static final String KEY_rRes = "R";
private static final String KEY_sRes = "S";
private static final String KEY_tRes = "T";
private static final String KEY_uRes = "U";
private static final String KEY_vRes = "V";
private static final String KEY_wRes = "W";
private static final String KEY_xRes = "X";
private static final String KEY_yRes = "Y";
private static final String KEY_zRes = "Z";

private DbHelper dbHelp;
private final Context context;
private SQLiteDatabase dbase;


private class DbHelper extends SQLiteOpenHelper{

    public DbHelper(Context con){
        super(con, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_STUDENT + " (" +
               KEY_sID + " INTEGER, " + 
               KEY_sSNAME + " TEXT NOT NULL, " + 
               KEY_sFNAME + " TEXT NOT NULL, " +
               KEY_t_ID + " INTEGER, " +
               KEY_t_SNAME + " TEXT NOT NULL, " +
               "FOREIGN KEY (" + KEY_t_ID + ") REFERENCES " + 
               TABLE_TEACHER + " (" + KEY_tID + "), " +
               "FOREIGN KEY (" + KEY_t_SNAME + ") REFERENCES " + 
               TABLE_TEACHER + " (" + KEY_tID + "), " +
               "PRIMARY KEY (" + KEY_sID + ", " + KEY_sSNAME + ", " + KEY_sFNAME + "));"
        );

        db.execSQL("CREATE TABLE " + TABLE_TEACHER + " (" +
               KEY_tSNAME + " TEXT NOT NULL, " + 
               KEY_tFNAME + " TEXT NOT NULL, " + 
               KEY_tSCHOOL + " TEXT NOT NULL, " +
               "PRIMARY KEY (" + KEY_tSNAME + ", " + KEY_tFNAME + "));"
        );

        db.execSQL("CREATE TABLE " + TABLE_RESULTS + " (" +
               KEY_aRes + " TEXT, " + KEY_bRes + " TEXT, " +
               KEY_cRes + " TEXT, " + KEY_dRes + " TEXT, " +
               KEY_eRes + " TEXT, " + KEY_fRes + " TEXT, " +
               KEY_gRes + " TEXT, " + KEY_hRes + " TEXT, " +
               KEY_iRes + " TEXT, " + KEY_jRes + " TEXT, " +
               KEY_kRes + " TEXT, " + KEY_lRes + " TEXT, " +
               KEY_mRes + " TEXT, " + KEY_nRes + " TEXT, " +
               KEY_oRes + " TEXT, " + KEY_pRes + " TEXT, " +
               KEY_qRes + " TEXT, " + KEY_rRes + " TEXT, " +
               KEY_sRes + " TEXT, " + KEY_tRes + " TEXT, " +
               KEY_uRes + " TEXT, " + KEY_vRes + " TEXT, " +
               KEY_wRes + " TEXT, " + KEY_xRes + " TEXT, " +
               KEY_yRes + " TEXT, " + KEY_zRes + " TEXT);"
        );
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_STUDENT);
        onCreate(db);
    }

}

public Database(Context c) {
    context = c;
}

public Database openForWrite() throws SQLException {
    dbHelp = new DbHelper(context);
    dbase = dbHelp.getWritableDatabase();

    return this;
}

public Database openForRead() {
    dbHelp = new DbHelper(context);
    dbase = dbHelp.getReadableDatabase();

    return this;
}

public void close(){
    dbHelp.close();
}

long createStudentEntry(String f, String surname, String school ){
    ContentValues cv = new ContentValues();
    cv.put(KEY_sFNAME, f);
    cv.put(KEY_sSNAME, surname);
    cv.put(KEY_sSCHOOL, school);

    //find way to do insert where
    return dbase.insert(TABLE_STUDENT, null, cv);
}

//look into password issue
long createTeacherEntry(String forename, String surname, String school ){
    ContentValues cv = new ContentValues();
    cv.put(KEY_tFNAME, forename);
    cv.put(KEY_tSNAME, surname);
    cv.put(KEY_tSCHOOL, school);

    //find way to do insert where
    return dbase.insert(TABLE_TEACHER , null, cv);
}

long createResultEntry(String key, String result ){
    ContentValues cv = new ContentValues();
    cv.put(key, result); // may not work change later

    //find way to do insert where?
    return dbase.insert(TABLE_STUDENT , null, cv);
}

public String getData(){
    String [] columns = new String []{KEY_sFNAME, KEY_sSNAME, KEY_sSCHOOL};
    Cursor c = dbase.query(TABLE_STUDENT, columns, null, null, null, null, null);
    String queryRes = "";

    int sFName = c.getColumnIndex(KEY_sFNAME);
    int sSName = c.getColumnIndex(KEY_sSNAME);
    int sSCHOOL = c.getColumnIndex(KEY_sSCHOOL);

    for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
        queryRes = queryRes + c.getString(sFName) + " " + c.getString(sSName) + " " + c.getString(sSCHOOL) + "\n";
    }
    c.close();

    return queryRes;
}
4

1 回答 1

1

在您的onCreate方法中,在用于创建的 sql 字符串中,TABLE_STUDENT您没有声明一个名称与KEY_sSCHOOL您在createStudentEntry方法中使用的名称相对应的列。

Double check that sql string and insert that column and after, either increment the database version or reinstall the app.

于 2013-03-13T17:29:30.227 回答