QSqlQuery with postgresql array's



  • 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.



  • 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 ?



  • 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.



  • 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)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.