2013-03-13 45 views
0

我正在構建一個Android應用程序,將教孩子們如何寫作。因此,我需要保存關於每個孩子的某些信息以及他們的具體信件的結果。我在構建數據庫,並使用教程在Youtube被的階段,但我得到了很多的錯誤:表x沒有列y錯誤。搜索並找不到工作解決方案

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; 
} 

回答

1

在你onCreate方法,用於創建SQL字符串的TABLE_STUDENT你不對應KEY_sSCHOOL您在createStudentEntry方法使用一個名稱聲明的列。

仔細檢查該sql字符串並插入該列,然後增加數據庫版本或重新安裝應用程序。

+0

非常感謝。我怎麼沒看到自己呢? – Unem 2013-03-14 21:47:48

+0

@Unem有時你只是想念明顯。退一步,放鬆,然後回來嘗試解決問題總是一個好主意。 – Luksprog 2013-03-15 04:49:18

相關問題