2

I'm Working on Android project and I'm using SQLite database in that, I have written one insert query as :

sqliteDB_Obj.execSQL("INSERT INTO tbl_order_master (user_id, order, amount)" + "VALUES(" + strUserId +","+ strOrderString +","+ dblAmount +");");

But I'm getting following error

android.database.sqlite.SQLiteException: near "order": syntax error (code 1): , while compiling: INSERT INTO tbl_order_master (user_id, order, amount) VALUES(dny, my test order, 160.0);

some people have asked same question before so according to the answer mentioned their I have tried following way :

String strTableName="tbl_order_master";
String strQuery= String.format("INSERT INTO %s (user_id, order, amount) VALUES(%s, %s, %s);", strTableName, strUserId, strOrderString, ""+dblAmount);
sqliteDB_Obj.execSQL(strQuery);

but still getting same error Please help.. Thanks..!

4
  • 3
    probably the problem is that the order name is a reserved word? try using sort_order or similar as column name or write it with backticks. Commented Jun 12, 2014 at 12:07
  • Sqlite keywords and "order" is one of them Commented Jun 12, 2014 at 12:08
  • possible duplicate of Syntax error due to using a reserved word as a table or column name in MySQL Commented Jun 12, 2014 at 12:11
  • change the column name "order" or using order Commented Jun 12, 2014 at 12:17

5 Answers 5

3

If you use a reserved name such as order as an identifier, put it in "double quotes". Or just rename the identifier so it isn't a reserved keyword.

Also in SQL, string literals need to be written in 'single quotes'. Yours are not quoted.

It's better to use parameters instead though, e.g.

execSQL("INSERT INTO tbl_order_master (\"user_id\", \"order\", \"amount\") VALUES (?, ?, ?)",
    new String[] { strUserId, strOrderString, Double.toString(dblAmount) } );

passing the parameter values in the second arg.

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

Comments

1

try this

 sqliteDB_Obj.execSQL("INSERT INTO tbl_order_master (`user_id`, `order`, `amount`)" + "VALUES('" + strUserId +"','"+ strOrderString +"',"+ dblAmount +");");

change the column name "order" or using `order`

Comments

0

ORDER is reserved word in SQL so you can not use as field name

sqliteDB_Obj.execSQL("INSERT INTO tbl_order_master (user_id, order, amount)" + "VALUES(" + strUserId +","+ strOrderString +","+ dblAmount +");");

change to

sqliteDB_Obj.execSQL("INSERT INTO tbl_order_master (user_id, s_order, amount)" + "VALUES(" + strUserId +","+ strOrderString +","+ dblAmount +");");

Comments

0

Try changing the name of your order column to something else. order is a reserved word in sqlite (i.e. order by).And Also single quotes are missing

So try this (Change order like m_order)

sqliteDB_Obj.execSQL("INSERT INTO tbl_order_master (user_id, m_order, amount)" + "VALUES('" + strUserId +"','"+ strOrderString +"','"+ dblAmount +"')");

Comments

0

Try this. As a good practice, always use quotes to wrap string field values.

sqliteDB_Obj.execSQL("INSERT INTO tbl_order_master (user_id, order, amount) " + " VALUES ( '"    + strUserId +"', '"+ strOrderString +"',"+ dblAmount +") ");

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.