Download database data in csv format

Asked

Viewed 80 times

1

I am trying to download the database data in csv format but it is indicating that the SQL syntax is wrong.

public void exportarBaseRespostaPI() {
    String banco = "b2wentregascorreios";
    String tabela = "baserespmanifest";
    String caminho = "C:\\Users\\wesley.costa\\Desktop\\wesley.costa\\backup-correios\\base_resposta_pi\\base_resposta_pi.csv";
    new File(caminho).delete();
    try (Connection conn = connBD.Conectar(banco); Statement stmt = conn.createStatement()) {
        try {
            stmt.execute("SELECT `codCorreio`, `codPI`, `respostaMsg`, `dataResp`\r\n" + " FROM " + tabela
                    + "INTO OUTFILE '" + caminho + "' \n"
                    + "FIELDS TERMINATED BY ';' ENCLOSED BY '\"' \n" + "LINES TERMINATED BY '\r\n';");

        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }
    } catch (ClassNotFoundException | SQLException e) {
        System.out.println(e.getMessage());
    }
  }

This is the Exception:

(Conn=127002095) You have an error in your SQL syntax; check the manual that Corresponds to your Mysql server version for the right syntax to use near 'OUTFILE 'C: Users Wesley.costa Desktop Wesley.costa backup-mails base_respost' at line 2 SELECT codCorrect, codPI, answerMsg, dataResp FROM baserespmanifest INTO OUTFILE 'C: Users Wesley.costa Desktop Wesley.costa backup-correios base_resposta_pi base_resposta_pi.csv' FIELDS TERMINATED BY ';' ENCLOSED BY '"' LINES TERMINATED BY ' ';

  • asks the string exactly how it will be executed

  • Add the Exception being displayed.

  • It’ll come out like this: SELECT codCorreio, codPI, respostaMsg, dataResp FROM baserespmanifest INTO OUTFILE 'C: Users Wesley.costa Desktop Wesley.costa backup-mails base_resposta_pi base_resposta_pi.csv' FIELDS TERMINATED BY ';' ENCLOSED BY '"' LINES TERMINATED BY ' ;';

  • Essa é a Exception: (Conn=127002095) You have an error in your SQL syntax; check the manual that Corresponds to your Mysql server version for the right syntax to use near 'OUTFILE 'C: Users Wesley.costa Desktop Wesley.costa backup-correios base_respost' at line 2

  • @Wesleyferreiracosta You can [Dit] the question to add information. It is much better and more readable than in the comments.

  • Sure you can leave

  • Best thing is to break into other methods, query, bring what you need to a list, and then generate a method that mounts the CSV

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.