Reputation: 157
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..!
Upvotes: 2
Views: 4337
Reputation: 7249
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`
Upvotes: 1
Reputation: 9894
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 +") ");
Upvotes: 0
Reputation: 14398
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 +"')");
Upvotes: 0
Reputation: 10076
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 +");");
Upvotes: 0
Reputation: 152817
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.
Upvotes: 3