By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,654 Members | 1,519 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,654 IT Pros & Developers. It's quick & easy.

How to SELECT INTO OUTFILE with header labels of the fields?

100+
P: 126
Expand|Select|Wrap|Line Numbers
  1. select '"sl"','"quantity"','"price"'
  2.  union all select sl,quantity,price into outfile 'C:/file.txt' fields terminated by ',' 
  3. lines terminated by '\r\n' from table
the above query works perfect at command line prompt but when i use as below i get error at the quotes used near sl,quantity and price that is it does not accept '"sl"','"quantity"','"price"' can someone help me please


Expand|Select|Wrap|Line Numbers
  1. Statement st = con.createStatement();
  2. String query = "select '"sl"','"quantity"','"price"' 
  3. union all select sl,quantity,price into
  4.  outfile 'C:/file.txt' fields terminated by ',' 
  5. lines terminated by '\r\n' from "+table+"";
  6. ResultSet rs = st.executeQuery(query);   
Apr 18 '13 #1
Share this Question
Share on Google+
3 Replies


Rabbit
Expert Mod 10K+
P: 12,421
You need to escape your double quotes.
Apr 18 '13 #2

100+
P: 126
it works now thank you:)
Apr 18 '13 #3

10K+
P: 13,264
Use a PreparedStatement not a Statement and it will not only quote for you but also prevent SQL injection.
Apr 18 '13 #4

Post your reply

Sign in to post your reply or Sign up for a free account.