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 " << 
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Alchazar
  • 41
  • 2
  • 8
  • 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(); } ` – CJCombrink Nov 02 '15 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. – Michael Vincent Nov 02 '15 at 14:22
  • @TheBadger tryed your code, result the same - getting nothing. – Alchazar Nov 02 '15 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. – Alchazar Nov 02 '15 at 14:34
  • @Alchazar the 0 returned with a count(*) suggests the table is present, but empty. Assuming no other errors are reported. – Michael Vincent Nov 02 '15 at 14:38
  • @MichaelVincent insert query works without any errors: query.exec("insert into transportas (transportas) values ('asdasd22')"); and new record is added to table. But select doesn't show any data. – Alchazar Nov 02 '15 at 14:43
  • Are you sure the insert query and the select query are pointing to the same database file? Do you need to end a transaction after the INSERT? – Michael Vincent Nov 02 '15 at 14:46
  • @MichaelVincent I renamed table just in case, and run two queryes: query.exec("insert into t_transportas (transportas) values ('asdasd22')"); query.exec("SELECT id, transportas FROM t_transportas"); Insert works, select not... records are inserted in table, checked with DB explorer tool – Alchazar Nov 02 '15 at 14:53
  • Are you using the same database connection for both? Is it necessary to close a DB connection or query after and insert? Can you copy and paste your code for the insert and the select? – Michael Vincent Nov 02 '15 at 14:59
  • Edited my firts post with new code with INSERT – Alchazar Nov 02 '15 at 15:08

1 Answers1

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?

Community
  • 1
  • 1
Alchazar
  • 41
  • 2
  • 8