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. QSqlQuery with postgresql array's
Forum Update on Monday, May 27th 2025

QSqlQuery with postgresql array's

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 3.7k 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.
  • S Offline
    S Offline
    sting
    wrote on 23 Feb 2015, 02:40 last edited by
    #1

    I have been looking at the api and can't find a way to insert arrays from QSqlQuery into a PostgreSQL database.

    Is there a lower level interface that I might be able to use. I have an array of raw int's that I want to store.

    1 Reply Last reply
    0
    • J Offline
      J Offline
      jerome_isAviable
      wrote on 23 Feb 2015, 08:26 last edited by
      #2

      could you give more informations around that ?
      i'm not sure to understand what you want to do.

      I use ppostgresql tables a lot now, and i can insert everything without any problem. Just have to take care with upper and lower case of chars named fields and tables... the driver is not good for that and old.
      The best way is to have all named columns and tables in lower case chars.

      also, QSqlQuery not give back an array...
      but if you have an harray of datas or a QList<QStringList> (who can be imagine to be an array style structure beacause of two dimension data form), who represent for each first list entry, a row of fields (so the QStringList would be the content row for each column of an array...), so you can insert datas like that:
      @
      // consider your database is open and ready and "data" is your
      // QList<QStringList> variable
      // your table name is "my_table"
      // your columns names are: col0, col1, col2
      QSqlQuery query;
      foreach(QStringList row, data) {
      query.prepare("INSERT INTO my_table "
      " ( col0, col1, col2) "
      "VALUES (:col0, col1, col2) ;");
      query.bindvalue(":col0", row[0]);
      query.bindvalue(":col1", row[1]);
      query.bindvalue(":col2", row[2]);
      if(query.exec())
      return false;
      }
      query.finish;
      query.clear();
      @

      hope this help.

      but maybe you talk about array type of data inside a postgresql field ?

      1 Reply Last reply
      0
      • S Offline
        S Offline
        sting
        wrote on 23 Feb 2015, 16:27 last edited by
        #3

        I have a table defined as:

        CREATE TABLE block (
        foo integer[1024]
        );

        int data[1024];

        then I want to

        INSERT INTO block (foo) Values (data);

        I have done this in java and the process is simply to collect and log the data to a database for future processing.

        1 Reply Last reply
        0
        • J Offline
          J Offline
          jerome_isAviable
          wrote on 24 Feb 2015, 02:05 last edited by
          #4

          so this is easy... the exemple cde i write for you should do the job.
          just take care do well define your data type as "int". Yur name table is lower case sensitive, your name of field too... you will not have any problem.
          Just connect the database well, i give you an exemple from my code about "how to connect the database":
          (don't forget to include all needed: QSqlDatabase, QSqlQuery, QSqlError and all you need... ALSO, don't forget to include in your pro file the "sql" at QT line...)
          @
          QSqlDatabase database = QSqlDatabase::addDatabase("QPSQL");
          database.setDatabaseName("my_db_name");
          database.setHostName("localhost");
          if(!database.open(QString("the_user_name"),
          QString("my_password"))) {
          QString err = database.lastError().text();
          qDebug() << QString("Database have problem to connect: \n"
          "%1").arg(err); }
          @
          include this code inside your constructor or inside a specific class to be call before use the database... if you not do that, Qt will not be able to talk with the database. Read the official doc about QSqlDatabase.

          also, in my exemple in the previous post, i not use transaction... but you can do it and open a transaction before the loop (foreach) and commit after the loop.

          having fun.

          (ps: if something seems not clear for you, or if my explication is not good, you can also pastebin your code here and i can look at your code and give you back a pastebin code who will works fine... but don't forget to include information about your database connection: user name, password, host name, database name use by your postgresql base)

          1 Reply Last reply
          0

          1/4

          23 Feb 2015, 02:40

          • Login

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