Columns in a table



  • Hallo,
    I am new at using QML and I am trying to count the columns of a table from a database but I don't know how to do it, I have tried to import a js with the row:

    @
    cols = document.getElementById('myTableName').rows[0].cells.length;
    @

    but QtCreator gives me an error : "ReferenceError: Can't find variable: document".
    So, how can I find the number of columns of a table from an opened database?
    Thank you,
    Giammarco

    [EDIT: code formatting, Volker]



  • You are not showing enough code. Foremost, you don't show us where that document variable is supposed to come from.



  • Giammarco,
    I agree with Andre, you should post some more code, otherwise we don't understand the error. How is "document" defined?

    I have already worked on a project using Qt and Sql, here you have a synthetic example from my program (maybe it helps):

    @// OPEN DATABASE CONNECTION
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName(fileName);
    if(!db.open())
    ErrorMessage();

    // MAKES QUERY (hope you know sql)
    QSqlQueryModel *model_query = new QSqlQueryModel(this);
    model_query->setQuery("SELECT * FROM myTableName", db);

    // GENERIC LOOP TO GO THROUGHT ALL THE ELEMENTS ON THE TABLE
    for(int i=0; i<model_query->rowCount(); i++) {
    for(int j=0; j<model_query->columnCount(); j++) {
    (...)
    }
    }@

    You can see on the for loop that I used the functions:
    @int QSqlQueryModel::rowCount()
    int QSqlQueryModel::columnCount()@

    Cheers!


Log in to reply
 

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