Andy Shearer
Andy Shearer

Reputation: 187

Android error "SQLiteException:near "1": syntax error (code1)

Here is my code, I have looked at others who are having the same problem and they are generally missing a space or something, but I have spent a while scouring through the SQL and can't find what it would be. Thanks in advance, here's my code:

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

}

Upvotes: 0

Views: 225

Answers (1)

laalto
laalto

Reputation: 152927

1 is not a valid column name:

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);");

Use column names that start with a letter, such as week and lesson.

Upvotes: 3

Related Questions