2014-03-04 204 views
0

這是我的代碼,我看過其他人有同樣的問題,他們通常缺少空間或東西,但我花了一段時間。通過SQL沖刷並不能找到那會是什麼在此先感謝,這是我的代碼:Android error「SQLiteException:near」1「:syntax error(code1)

public class TimingsDb { 

public static final String KEY_ID = "_id"; 
public static final String KEY_SUBJECT = "subject_name"; 
public static final String KEY_WEEK = "1"; 
public static final String KEY_DAY = "day_name"; 
public static final String KEY_LESSON = "1"; 

private static final String DATABASE_NAME = "TimingsDB"; 
private static final String DATABASE_TABLE = "TimingsTable"; 
private static final int DATABASE_VERSION = 1; 

private DbHelper ourHelper; 
private final Context ourContext; 
private SQLiteDatabase ourDatabase; 

private static class DbHelper extends SQLiteOpenHelper { 

    public DbHelper(Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
     // TODO Auto-generated constructor stub 

    }// end Constructor 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     // TODO Auto-generated method stub 
     db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ID 
       + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_SUBJECT 
       + " TEXT, " + KEY_WEEK + " INTEGER, " + KEY_DAY 
       + " TEXT, " + KEY_LESSON + " INTEGER);"); 
    }// end onCreate 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     // TODO Auto-generated method stub 
     db.execSQL("DROP TABLE IF EXISTS "+DATABASE_TABLE); 
     onCreate(db); 
    }//end onUpgrade 
}// end class 

public TimingsDb(Context c){ 
    ourContext = c; 
} 

public TimingsDb open() throws SQLException{ 
    ourHelper = new DbHelper(ourContext); 
    ourDatabase = ourHelper.getWritableDatabase(); 
    return this; 
}//end open 

public void close(){ 
    ourHelper.close(); 
}//end close 

public long createEntry(String subjectToAdd, int week, String day, int lesson) { 
    // TODO Auto-generated method stub 
    ContentValues cv = new ContentValues(); 
    cv.put(KEY_SUBJECT, subjectToAdd); 
    cv.put(KEY_WEEK, week); 
    cv.put(KEY_DAY, day); 
    cv.put(KEY_LESSON, lesson); 
    return ourDatabase.insert(DATABASE_TABLE, null, cv); 
}//end createEntry 

public String getData() { 
    // TODO Auto-generated method stub 
    String[] columns = new String[]{KEY_ID, KEY_SUBJECT, KEY_WEEK, KEY_DAY, KEY_LESSON}; 
    Cursor c = ourDatabase.query(DATABASE_TABLE,columns,null,null,null,null,null); 
    String result = ""; 
    int iId = c.getColumnIndex(KEY_ID); 
    int iSubject = c.getColumnIndex(KEY_SUBJECT); 
    int iWeek = c.getColumnIndex(KEY_WEEK); 
    int iDay = c.getColumnIndex(KEY_DAY); 
    int iLesson = c.getColumnIndex(KEY_LESSON); 

    for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){ 
     result = result + c.getString(iSubject) + c.getString(iWeek) + 
       c.getString(iDay) + c.getString(iLesson) + "\n"; 
    }//end for 

    return result; 
} 

} 

回答

3

1不是一個有效的列名:

public static final String KEY_WEEK = "1"; 
public static final String KEY_DAY = "day_name"; 
public static final String KEY_LESSON = "1"; 

...

db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ID 
      + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_SUBJECT 
      + " TEXT, " + KEY_WEEK + " INTEGER, " + KEY_DAY 
      + " TEXT, " + KEY_LESSON + " INTEGER);"); 

使用以字母開頭的列名,例如weeklesson

+0

你是對的。 –

+0

非常感謝!它是固定的 – user3379139