0

I tried to create a SQLite database with Qt and and I did!! I called my db "prova_db" and it contains the following table:

marker_db

id     site (columns)
0      www.google.it
1      www.youtube.it
2      www.facebook.it

Then, I tried to query my prova_db. Here is code:

int main () {
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("prova_db");
    if (!db.open()) { printf("DB doesn't exist\n");}
    else  {
        QSqlQuery query1;
            query1.exec( "SELECT site FROM marker_db WHERE id = 1");

        int i = query1.numRowsAffected();
        printf("result row: %d\n", i);
        while(query1.next()){
            QString str = query1.value(0).toString();
            printf("result: %s\n", str);
        }
    }
    db.close();
return 0;
}

The result is:

result row : 0
result : (strange char)

instead the result should be:

result row: 1
result : www.youtube.it

where am I doing wrong??

Thank you!

Cristina1986
  • 505
  • 1
  • 8
  • 21

1 Answers1

1

I think you have the wrong expectation: the result of numRowsAffected() tells you how many rows have been altered by the query. Your query does not alter anything, so the result should be either 0 or undefined. Thus, you shouldn't expect the first output to be risultato riga: 1. Use the size() method instead to find out how many rows have matched your SELECT query.

The second problem is that you are passing a QString object to printf(), which expects a pointer to a null-terminated array of char values instead (when you use the %s format specifier). You need to convert the QString object into a pointer to a C array of characters.

Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
  • sorry. I changed italian words. I changed `query1.numRowsAffected();` with `query1.size();` and result row is `-1`. Also, how to convert QString object into a pointer to a array of characters? Thank you for your answer! – Cristina1986 Jan 14 '13 at 00:23
  • :) ok, that was just an advice. i did understand, but some people on this forum probably skipped over this question because they did not want to bother with a foreign language. anyway did my answer help you? – Andy Prowl Jan 14 '13 at 00:28
  • Yes Thank you! I was able to convert QString objcet into an array o characters thanks to : [link](http://stackoverflow.com/questions/5505221/converting-qstring-to-char). Now `result` is `www-youtube.it`, but I don't understand why `query1.size()` is `-1` – Cristina1986 Jan 14 '13 at 00:38
  • @Cristina1986: This link may help: http://harmattan-dev.nokia.com/docs/library/html/qt4/qsqlquery.html#size: *"Returns the size of the result (number of rows returned), or -1 if the size cannot be determined or if the database does not support reporting information about query sizes"*. I must assume your database does not support returning information about query sizes – Andy Prowl Jan 14 '13 at 00:41
  • @Cristina1986: anyway you can still create a `counter` variable, initialize it to `0`, and do `counter++` inside the `while` loop to get the size of the result. – Andy Prowl Jan 14 '13 at 00:43