5
    11-27 03:32:04.471: E/AndroidRuntime(23137): Caused by: android.database.sqlite.SQLiteException: near "order": syntax error (code 1): , while compiling: create table order (_id integer primary key autoincrement, origin text not null, quantity integer not null); 

MyDatabase class:

public class MyDatabase extends SQLiteOpenHelper {

    public static final String TABLE_NAME = "order";
    public static final String TABLE_ID = "_id";
    public static final String TABLE_ORIGIN = "origin";
    public static final String TABLE_QUANTITY = "quantity";
    private static final String DATABASE_NAME = "Appple.db";
    private static final int DATABASE_VERSION = 1;

    private static final String DATABASE_CREATE = 
        "create table " + TABLE_NAME + 
            "(" + TABLE_ID + " integer primary key autoincrement, " + 
            TABLE_ORIGIN + " text not null, " +
            TABLE_QUANTITY + " integer not null);";

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

    public void onCreate(SQLiteDatabase db) {
        db.execSQL(DATABASE_CREATE);
    }

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }
}

Operation class:

public class Operation {

    private MyDatabase dbHelper;
    private String[] COLUMNS = { MyDatabase.TABLE_ID, MyDatabase.TABLE_ORIGIN, MyDatabase.TABLE_QUANTITY };
    private SQLiteDatabase database;


    public Operation(Context context) {
        dbHelper = new MyDatabase(context);
    }

    public void open() throws SQLException {
        database = dbHelper.getWritableDatabase();
    }

    public void close() {
        dbHelper.close();
    }

    public void add(String origin, String quantity) {
        ContentValues values = new ContentValues();
        values.put(MyDatabase.TABLE_ORIGIN, origin);
        values.put(MyDatabase.TABLE_QUANTITY, Integer.parseInt(quantity));
        database.insert(MyDatabase.TABLE_NAME, null, values);
    }     

    public int get(String origin) {
        int total = 0;

        Cursor cursor = database.query(MyDatabase.TABLE_NAME, COLUMNS,
                MyDatabase.TABLE_ORIGIN + " = " + origin, null, null, null, null);
        cursor.moveToFirst();
        while(!cursor.isAfterLast()) {
            total += cursor.getInt(2);
            cursor.moveToNext();
        }
        cursor.close();
        return total;
    }
}

In MainActivity start with

Operation op;
op = new Operation(this);
op.open();

I think there is no problem in CREATE TABLE. But I can't find cause of error.

1
  • You should be more clear with what your problem is. Change the question title to something more appropriate and specify the exact problem. Also, include full stacktrace and specify the exact line on which it occurrs. Commented Nov 27, 2014 at 9:17

2 Answers 2

12

order is a keyword in SQL. Either rename the table, or put the table name in double quotes like "order".

Sign up to request clarification or add additional context in comments.

Comments

6

order is a sqlite keyword, and sqlite keywords can not be used as table name. Here you can find a list of those

4 Comments

Tannk you very much! Have a nice day!
I've got the same error for Column name definition. So don't use names from the list of sqlite keywords.
@vovahost it is difficult to say what's going on without seeing the code. Try asking a question
@Blackbelt I fixed my error. I was using "order" as column name definition

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.