Luke Stuemke
Luke Stuemke

Reputation: 545

Android SQLiteDatabase Insert Syntax Error

I've searched around and found people with similar issues, yet not quite the same. I'm receiving an SQLite syntax error, but I cannot locate the bad syntax. Please help!

I created an SQLite Database in DatabaseAdapter.java, seen here:

static class DatabaseHelper extends SQLiteOpenHelper{
        private static final String DATABASE_NAME = "mealdatabase";
        private static final String TABLE_INGREDIENT = "INGREDIENTS";
        private static final int DATABASE_VERSION = 1;
        private static final String UID = "_id";
        private static final String INGREDIENT_NAME = "Ingredient Name";
        private static final String SERVING_UNITS = "Serving Units";
        private static final String SPC = "Servings Per Container";
        private static final String CREATE_TABLE_INGREDIENT = "CREATE TABLE "+TABLE_INGREDIENT+
                " ("+UID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+INGREDIENT_NAME+" VARCHAR(255), "
                +SERVING_UNITS+" VARCHAR(255), "+SPC+" VARCHAR(255));";

        private static final String DROP_TABLE_INGREDIENT = "DROP TABLE IF EXISTS "+TABLE_INGREDIENT;
        private Context context;

        public DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
            this.context=context;
            Message.message(context, "Constructor Called");
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            try {
                db.execSQL(CREATE_TABLE_INGREDIENT);
                Message.message(context, "onCreate Called");
            } catch (SQLException e) {
                Message.message(context, ""+e);
            }

        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            try {
                Message.message(context, "onUpgrade Called");
                db.execSQL(DROP_TABLE_INGREDIENT);
                //context.deleteDatabase(DATABASE_NAME);
                onCreate(db);
            } catch (SQLException e) {
                Message.message(context, ""+e);
            }

        }
    } 

I'm trying to insert new data into an existing SQLite Database. The database was created with no errors. However, when my insert function is called, I get the following syntax error:

03-06 20:44:27.559: E/SQLiteLog(968): (1) near "Per": syntax error
03-06 20:44:27.619: E/SQLiteDatabase(968): Error inserting Servings Per Container=8.0 Serving Units=oz Ingredient Name=Spaghetti Noodle

Here's where I call the insert function that is in NewIngredient.java:

public void saveIngredientData() {

        try {
            String ingredient = ingredient_editText.getText().toString();
            String servingUnits = spinner.getSelectedItem().toString();
            double servPerContainer = Double.parseDouble(SPC_editText.getText().toString());

            long id = databaseHelper.insertIngredientData(ingredient, servingUnits, servPerContainer);
            if (id < 0)
            {
                Message.message(this, "Insert was Unsuccessful");
            }
            else
                Message.message(this, "Successfully entered a row");
        } catch (NumberFormatException e) {
            Message.message(getApplicationContext(), ""+e);
        }

And here's the insertIngredientData function that contains a syntax error, located in DatabaseAdapter.java:

public long insertIngredientData(String ingredientName, String units, double spc){
    SQLiteDatabase db = helper.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(DatabaseHelper.INGREDIENT_NAME, ingredientName);
    contentValues.put(DatabaseHelper.SERVING_UNITS, units);
    contentValues.put(DatabaseHelper.SPC, spc);
    long id = db.insert(DatabaseHelper.TABLE_INGREDIENT, null, contentValues);
    db.close();
    return id;
}

Upvotes: 0

Views: 179

Answers (1)

Phant&#244;maxx
Phant&#244;maxx

Reputation: 38098

You are using spaces in field names.

You better use underscores (_) instead. I.e.: private static final String SPC = "Servings_Per_Container";.
OR (if you really want to insist using spaces), you must include the field names in brackets ([ and ]). I.e.: private static final String SPC = "[Servings Per Container]";.

Correct ALL your field names, not only the one in the example.

Upvotes: 1

Related Questions