简体   繁体   中英

SQLite insert into error (Android)

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

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

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

For example,

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

Or, written out:

" VALUES ('Bob')"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM