1

Well the thing is that when the program execute the query to copy a table to a file .CSV. Qt show me the next error.

"ERROR: syntax error at end of input LINE 1: EXECUTE

Here are the code of the export action:

QSqlQuery qry;

qry.prepare("copy inventory to './inventory.csv'");
if(qry.exec()){
    qDebug()<<"Succes";
}else{
    qDebug()<<qry.lastError().text();
}

The version of qt is 5.4, used postgresql 9.3 and driver PQSQL working fine just can execute another's query very well like select.

Thanks.

Patrick
  • 23,688
  • 4
  • 49
  • 75
MRlinux
  • 59
  • 5

2 Answers2

1

You mentioned that you're using Qt's SQL interface and its PostgreSQL driver.

While Qt's PostgreSQL driver is built on top of PostgreSQL's standard client library libpq, as far as I can tell it does not offer support for lots of the functionality of libpq. In particular, there appears to be no way to access support for the COPY protocol, nor for LISTENing for asynchronous notifications.

You will have to:

  • libpq directly to COPY ... FROM STDIN
  • or use regular INSERT statements via Qt; or
  • Transfer the CSV input to the server, then use COPY ... FROM '/path/on/server' to read the input from a file on the server that the PostgreSQL database is running on, readable by the user the PostgreSQL database runs as.

(You could also submit a patch to Qt to add support for the COPY protocol, which shouldn't be too hard to implement, but is perhaps not the best choice if you're asking this.)

Craig Ringer
  • 259,831
  • 56
  • 584
  • 684
  • @MRlinux Qt is just a C++ framework. `libpq` is a C library, and can be used directly in C++ code. You just use `libpq`'s API's directly, per the PostgreSQL manual. – Craig Ringer Jul 14 '15 at 06:01
0
  1. Using COPY needs superuser rights . Do not confuse with \COPY of PostgreSQL
  2. COPY TO requires absolute path to the output file. If 1st point is considered, try removing the ./ in your output file name
  3. You can refer to related posts: post1 and post2
Community
  • 1
  • 1
techneaz
  • 978
  • 6
  • 9