Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QPSQL unable to COPY large files
Forum Update on Monday, May 27th 2025

QPSQL unable to COPY large files

Scheduled Pinned Locked Moved Solved General and Desktop
postgresqlqpsql
2 Posts 1 Posters 377 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    david_qt
    wrote on 23 Jan 2021, 21:52 last edited by
    #1

    Hello.
    I'm trying to make a function for create a large table from file using COPY.
    My function create the *.csv file properly.
    After, my function generate the query, who calls to a postgres function that uses COPY command.

    This is the postgres function:

    CREATE OR REPLACE FUNCTION importar_copy(
        _nombretabla character varying,
        _ruta character varying)
      RETURNS boolean AS
    $BODY$
    DECLARE
    texto text;
    BEGIN
    	texto = FORMAT ('COPY %I FROM %s DELIMITER %s NULL AS ''NULL''', _nombretabla, quote_literal(_ruta), quote_literal(chr(9)));
    	execute (texto);
    	raise notice '%',texto;
    	return true;
    END;
    $BODY$
      LANGUAGE plpgsql VOLATILE
      COST 100;
    

    Well, it's very curious (and very annoying) to see that looks as if the query executed from my application isn't not able to read so a large file, and looks as if it stop to read in any point of the file.
    I think that the file and the query is OK because if I copy the query generate in Qt and I run it in pgAdmin, there are no problem.

    This is the snippet of Qt where I buid the file and after I try to execute the query:

    QTextStream tConceptos;
    QFile file;
     if (tipo == SIMPLIFICADO)
     {
         file.setFileName("conceptos.csv");
         file.open(QIODevice::WriteOnly | QIODevice::Text);
         tConceptos.setDevice(&file);
         tConceptos.setCodec("UTF-8");
     }
    ---------------
    (feeding the QTextStream)
    tConceptos<<"field"<<"\t"<<"field"<<"\t"<<......."\n";
    ----------------
     QString cadenaimportar = "SELECT importar_copy('" + tabla + "','/path/conceptos.csv',)";
     QSqlQuery consulta(db);
     if(consulta.prepare(cadenaimportar))
     {
         consulta.exec();
     }
    qDebug()<<consulta.lastError()<<"--"<<consulta.lastQuery();
    file.close();
    

    I would like to know if I am doing anything wrong, or it is a limitation of Qt (QPSQL driver?) because I think that itsn't a problem of the csv files or queries, because I can execute the same queries without problems in pgAdmin.

    1 Reply Last reply
    0
    • D Offline
      D Offline
      david_qt
      wrote on 23 Jan 2021, 22:51 last edited by
      #2

      Well, I have discovered my problem.
      I was trying to read the file before closing it:

      QString cadenaimportar = "SELECT importar_copy('" + tabla + "','/path/conceptos.csv',)";
       ......
      file.close();
      

      And it would be:

      file.close();
       QString cadenaimportar = "SELECT importar_copy('" + tabla + "','/path/conceptos.csv',)";
       QSqlQuery consulta(db);
       if(consulta.prepare(cadenaimportar))
       {
           consulta.exec();
       }
      qDebug()<<consulta.lastError()<<"--"<<consulta.lastQuery();
      
      1 Reply Last reply
      0

      2/2

      23 Jan 2021, 22:51

      • Login

      • Login or register to search.
      2 out of 2
      • First post
        2/2
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved