1

Error

12-22 14:30:52.329    1261-1261/com.TrackApp.trackapp E/SQLiteLog﹕ (1) near "Throw": syntax error
12-22 14:30:52.329    1261-1261/com.TrackApp.trackapp D/AndroidRuntime﹕ Shutting down VM
12-22 14:30:52.339    1261-1261/com.TrackApp.trackapp W/dalvikvm﹕ threadid=1: thread exiting  with uncaught exception (group=0x40a13300)
12-22 14:30:52.389    1261-1261/com.TrackApp.trackapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
android.database.sqlite.SQLiteException: near "Throw": syntax error (code 1): , while compiling:    INSERT INTO Athletes (ID, First_Name, Last_Name, Age, Gender, Event, Tier) VALUES (0, Joe,  Richards, 20, Male, Discus Throw, 2);

I keep getting this error when trying to add a new athlete to my database. Could someone please figure out what I am doing wrong. I am almost certain all of my sql statements are correct. I think it thinks that it is creating a new column under events for whatever is from the athlete object but I don't know

private static final String DATABASE_NAME = "AthletesDB";
private static final int VERSION = 1;

private static final String TABLE_NAME = "Athletes";
private static final String COLUMN_ID = "ID";
private static final String COLUMN_FIRST_NAME = "First_Name";
private static final String COLUMN_LAST_NAME = "Last_Name";
private static final String COLUMN_AGE = "Age";
private static final String COLUMN_GENDER = "Gender";
private static final String COLUMN_EVENT = "Event";

private static final String COLUMN_TIER = "Tier";
private static final String[] COLUMNS = {"ID", "First_Name", "Last_Name", "Age", "Gender", "Event", "Tier"};

private static final String TABLE_CREATE = "CREATE TABLE " + TABLE_NAME + "( " + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
        + COLUMN_FIRST_NAME + " VARCHAR(20), " + COLUMN_LAST_NAME + " VARCHAR(20), " + COLUMN_AGE + " INTEGER, " +
        COLUMN_GENDER + " VARCHAR(20), " + COLUMN_EVENT + " VARCHAR(20), " + COLUMN_TIER + " INTEGER " + ")";

public MySQLiteHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
    super(context, DATABASE_NAME, null, VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(TABLE_CREATE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_CREATE);
    onCreate(db);
}
public void addAthlete(Athlete athlete){
    SQLiteDatabase db = this.getWritableDatabase();

    /*ContentValues contentValues = new ContentValues();
    contentValues.put(COLUMN_ID, athlete.getID());
    contentValues.put(COLUMN_FIRST_NAME, athlete.getFirstName());
    contentValues.put(COLUMN_LAST_NAME, athlete.getLastName());
    contentValues.put(COLUMN_AGE, athlete.getAge());
    contentValues.put(COLUMN_GENDER, athlete.getGender());
    contentValues.put(COLUMN_EVENT, athlete.getEvent());
    contentValues.put(COLUMN_TIER, athlete.getTier());*/

    // db.insert(TABLE_NAME, null, contentValues);

    db.execSQL("INSERT INTO " + TABLE_NAME + " (" + COLUMN_ID + ", " + COLUMN_FIRST_NAME + ", " + COLUMN_LAST_NAME
    + ", " + COLUMN_AGE + ", " + COLUMN_GENDER + ", " + COLUMN_EVENT + ", " + COLUMN_TIER + ")" +
    " VALUES " + "(" + athlete.getID() + ", " + athlete.getFirstName()
    + ", " + athlete.getLastName() + ", " + athlete.getAge() + ", " + athlete.getGender() + ", " +
    athlete.getEvent() + ", " + athlete.getTier() + ");");
    db.close();
}
1
  • 1
    use the commented code block you have there. Commented Dec 22, 2014 at 20:48

2 Answers 2

4

You should surround the string values you insert with single quotes. The exception is thrown because you have whitespace in your literal "Discus Throw". Try the following:

db.execSQL("INSERT INTO " 
            + TABLE_NAME + " (" 
                + COLUMN_ID + ", " 
                + COLUMN_FIRST_NAME + ", " 
                + COLUMN_LAST_NAME + ", " 
                + COLUMN_AGE + ", " 
                + COLUMN_GENDER + ", " 
                + COLUMN_EVENT + ", " 
                + COLUMN_TIER + ")" +
            " VALUES " + "(" 
                + athlete.getID() + ", " 
                + athlete.getFirstName() + ", " 
                + athlete.getLastName() + ", " 
                + athlete.getAge() + ", " 
                + athlete.getGender() + ", " 
                + "'" + athlete.getEvent() + "'" + ", " 
                + athlete.getTier() + ");");

which gives you

INSERT INTO Athletes (ID, First_Name, Last_Name, Age, Gender, Event, Tier) 
    VALUES (0, Joe,  Richards, 20, Male, 'Discus Throw', 2);
Sign up to request clarification or add additional context in comments.

2 Comments

I think it's better to surround all text entries with single quotes, unless their values are known to be single-word in advance. For example, the first name of "Mary Sue" could cause the same error.
@MattLogan Sure, you're right. I was just pointing to the exact cause of problem.
2

Surround each entry of type TEXT in VALUES with single quotes.

For example,

" VALUES ('" + athlete.getFirstName() + "')"

Or, written out:

" VALUES ('Bob')"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.