android.database.sqlite.SQLiteException: near "Order": syntax error (code 1):

Viewed 606

My code always gets a syntax exception in sqlite. It always highlights in onCreate of DatabaseHelper. And the error maybe in the creation of table "Order".

I am using Android Studio and developing ordering system by making a class a populating it through adapter in listview.

Here is my code:

package com.example.lenovot420.yumburgers;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
* Created by lenovot420 on 9/24/2017.
*/

public class DatabaseHelper extends SQLiteOpenHelper {

//version number to upgrade database version
//each time if you Add, Edit table, you need to change the
//version number.
private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME = "JollibeeOrder.db";

public DatabaseHelper(Context context ) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    //All necessary tables you like to create will create here

    String CREATE_TABLE_ORDER = "CREATE TABLE " + Order.TABLE + " ("
            + Order.KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + Order.KEY_ORDER_NAME + " TEXT, "
            + Order.KEY_ORDER_EXTRA + " TEXT, "
            + Order.KEY_ORDER_PRICE + " TEXT, "
            + Order.KEY_ORDER_QUANTITY + " TEXT, "
            + Order.KEY_ORDER_TOTAL + " TEXT, "
            + Order.KEY_ORDER_PHOTO + " INTEGER )";

    db.execSQL(CREATE_TABLE_ORDER);

}


@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    // Drop older table if existed, all data will be gone!!!
    db.execSQL("DROP TABLE IF EXISTS " + Order.TABLE);

    // Create tables again
    onCreate(db);

}
}
1 Answers

ORDER is a sqlite keyword and cannot be used as an identifier. You either need to "quote it" or even better, rename it to something that is not a keyword.

Related