0

My QT SQL select doesn't return any data:

 //connect DB
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("C:/temp/atsakymai_1.db");
if (!db.open())
{
    qDebug() << "Connection error!!!";
}
QSqlQuery query;
query.prepare("SELECT * FROM transportas");
if (!query.exec())
{
     qDebug() << "SQL error: "<< query.lastError().text() << endl;
}

qDebug() << query.executedQuery();
qDebug() << query.result();
qDebug() << query.size();

query.first();
while (query.next())
{
    qDebug() << "found " << endl;
}

Tried to write path like this - C:\temp\atsakymai_1.db, but result the same:

query.result - 0x3c6ed8
query.size - -1

I had google all day to find any solution, but it didn't help, still have no idea what is wrong with my code.

Tried INSERT before SELECT, INSERT works without errors, but SELECT still return nothing:

 QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("C:/temp/atsakymai_1.db");

if (!db.open())
{
    qDebug() << "error!!!";
}

QSqlQuery query(db);

query.exec("insert into t_transportas (transportas) values ('asdasd22')");
query.exec("SELECT id, transportas FROM 't_transportas'");

qDebug() << query.executedQuery();
qDebug() << query.result();
qDebug() << query.size();
if (query.size()> 0)
{
    qDebug() << "found " << 
}
10
  • 1
    QT is QuickTime, Qt is the framework... Do you need to prepare the query, what happens if you just try: QSqlQuery query("SELECT * FROM transportas;"); bool success = query.exec(); while(query.next() == true) { qDebug() << query.value(0).toString(); } Commented Nov 2, 2015 at 14:22
  • @Alchazar can you confirm that db.open() works ok? Does query.exec() return true? Have you a SQLite explorer type application that can connect to that file to check there is data in it? I like to use "select count ( * ) from mytable" as it always returns one row provided the table is present in the database. Commented Nov 2, 2015 at 14:22
  • @TheBadger tryed your code, result the same - getting nothing. Commented Nov 2, 2015 at 14:31
  • @MichaelVincent select count returns 0. I use SQLite database browser to explore database. Data correct, the same query, as used in code, works correctly in explorer. Commented Nov 2, 2015 at 14:34
  • @Alchazar the 0 returned with a count(*) suggests the table is present, but empty. Assuming no other errors are reported. Commented Nov 2, 2015 at 14:38

1 Answer 1

2

Found where problem is, QSqlQuery::size() function not working, it always return -1. So don't use this function to find rows count. In place of this use .first() .next() functions. This code works correctly:

 QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("C:/temp/atsakymai_1.db");
if (!db.open())
{
    qDebug() << "error!!!";
}

QSqlQuery query(db);
query.prepare("SELECT id, transportas FROM 't_transportas'");
if (!query.exec())
{
     qDebug() << "SQL error: "<< query.lastError().text() << endl;
}
    query.first();
    qDebug() << "found: " << endl;
    while (query.next())
    {

        qDebug() << query.value("id").toString() << ". " << query.value("transportas").toString();

    }

Asnwer found here: QtSQL + Sqlite and support for .size() function?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.