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..!

Pragnesh Ghoda シ
  • 8,318
  • 3
  • 25
  • 40
Namo
  • 157
  • 1
  • 6
  • 17
  • 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. – Olli Jun 12 '14 at 12:07
  • [Sqlite keywords](http://www.sqlite.org/lang_keywords.html) and "order" is one of them – Pararth Jun 12 '14 at 12:08
  • possible duplicate of [Syntax error due to using a reserved word as a table or column name in MySQL](http://stackoverflow.com/questions/23446377/syntax-error-due-to-using-a-reserved-word-as-a-table-or-column-name-in-mysql) – vhu Jun 12 '14 at 12:11
  • change the column name "order" or using `order` – Gabber Jun 12 '14 at 12:17

5 Answers5

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.

laalto
  • 150,114
  • 66
  • 286
  • 303
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`

Gabber
  • 7,169
  • 3
  • 32
  • 46
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 +");");
MilapTank
  • 9,988
  • 7
  • 38
  • 53
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 +"')");
Giru Bhai
  • 14,370
  • 5
  • 46
  • 74
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 +") ");
ngrashia
  • 9,869
  • 5
  • 43
  • 58