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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • PreethiGowri
    New Member
    • Oct 2012
    • 126

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

    Code:
    select '"sl"','"quantity"','"price"'
     union all select sl,quantity,price into outfile 'C:/file.txt' fields terminated by ',' 
    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"','"quanti ty"','"price" ' can someone help me please


    Code:
    Statement st = con.createStatement();
    String query = "select '"sl"','"quantity"','"price"' 
    union all select sl,quantity,price into
     outfile 'C:/file.txt' fields terminated by ',' 
    lines terminated by '\r\n' from "+table+"";
    ResultSet rs = st.executeQuery(query);
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    You need to escape your double quotes.

    Comment

    • PreethiGowri
      New Member
      • Oct 2012
      • 126

      #3
      it works now thank you:)

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Use a PreparedStateme nt not a Statement and it will not only quote for you but also prevent SQL injection.

        Comment

        Working...