Results 1 to 2 of 2

Thread: sqlite seek() bug?

  1. #1
    Join Date
    Feb 2008
    Location
    South Korea
    Posts
    14
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question sqlite seek() bug?

    I'm having a wierd problem with seek() on a sqlite database (Windows XP).
    I have prepare'd a SQL statement, and then use seek() to get a random record in the result set.
    The seek() function always returns true, but sometimes the record is not initialized. I see no logic behind this behaviour, since isValid(), isActive() and isSelect() all return true after a seek() (see attached output).

    Anybody have any clue to why this is happening?

    Regards,
    Ingemar

    Qt Code:
    1. bool VAce::randomSynonym(QSqlQuery *currentSql, int synCount)
    2. {
    3. int engIndex = currentSql->record().indexOf("wrdeng");
    4. int typeIndex = currentSql->record().indexOf("typid");
    5.  
    6. QSqlQuery randomSql(QSqlDatabase::database("vaceDB"));
    7.  
    8. //get number of records
    9. randomSql.prepare("select count(*) from word where wrdeng != :engWord and typid = :typeID");
    10. randomSql.bindValue(":engWord", currentSql->record().value(engIndex).toString());
    11. randomSql.bindValue(":typeID", currentSql->record().value(typeIndex).toString());
    12. randomSql.exec();
    13. randomSql.next();
    14. int numRecs = randomSql.record().value(0).toInt();
    15.  
    16.  
    17. randomSql.prepare("select * from word where wrdeng != :engWord and typid = :typeID");
    18. randomSql.bindValue(":engWord", currentSql->record().value(engIndex).toString());
    19. randomSql.bindValue(":typeID", currentSql->record().value(typeIndex).toString());
    20. randomSql.exec();
    21.  
    22. int idIndex = randomSql.record().indexOf("wrdid");
    23.  
    24. for (int i=0; i<synCount; i++)
    25. {
    26. int newRec = rand() % numRecs;
    27. bool b1 = randomSql.seek(newRec);
    28.  
    29. qDebug() << newRec;
    30. qDebug() << "seek OK " << b1;
    31. qDebug() << "select " << randomSql.isSelect();
    32. qDebug() << "active " << randomSql.isActive();
    33. qDebug() << "valid " << randomSql.isValid();
    34. qDebug() << "fields in record" << randomSql.record().count();
    35. qDebug() << "field 0 =" << randomSql.record().field(0).name();
    36. qDebug() << "field 0 value =" << randomSql.record().field(0).value();
    37.  
    38. //remove after debug
    39. /*if (randomSql.record().value(idIndex).toInt() == 0)
    40.   {
    41.   QMessageBox::information(this, "Error", "randomSql record not initialized.\nPrinting will abort.");
    42.   return false;
    43.   }*/
    44. //----------------
    45.  
    46. bool continueSearch = true;
    47. int tmpSynonym = 0;
    48. QString tmpStr = "";
    49.  
    50. while ((tmpStr.length() == 0) || continueSearch)
    51. {
    52. tmpSynonym = (rand() % 6 + 1) + 3;
    53.  
    54. if (synonymList.isEmpty())
    55. tmpStr = currentSql->record().value(tmpSynonym).toString().trimmed();
    56. else
    57. tmpStr = randomSql.record().value(tmpSynonym).toString().trimmed();
    58.  
    59. // make sure we don't select a synonym twice
    60. if (synonymList.contains(tmpStr))
    61. continueSearch = true;
    62. else
    63. continueSearch = false;
    64. }
    65.  
    66. synonymList.append(tmpStr);
    67.  
    68. if (synonymList.size() == 1)
    69. pickedSynonym.append(tmpSynonym);
    70. }
    71.  
    72. return true;
    73. }
    To copy to clipboard, switch view to plain text mode 

    Output from the qDebug statements. As you can see in the last entry the field is not initialized even though the record seems fine..
    warning: 661

    warning: seek OK true

    warning: select true

    warning: active true

    warning: valid true

    warning: fields in record 12

    warning: field 0 = "wrdid"

    warning: field 0 value = QVariant(qlonglong, 4240)

    warning: 336

    warning: seek OK true

    warning: select true

    warning: active true

    warning: valid true

    warning: fields in record 12

    warning: field 0 = "wrdid"

    warning: field 0 value = QVariant(qlonglong, 3387)

    warning: 404

    warning: seek OK true

    warning: select true

    warning: active true

    warning: valid true

    warning: fields in record 12

    warning: field 0 = "wrdid"

    warning: field 0 value = QVariant(qlonglong, 3561)

    warning: 146

    warning: seek OK true

    warning: select true

    warning: active true

    warning: valid true

    warning: fields in record 12

    warning: field 0 = "wrdid"

    warning: field 0 value = QVariant(qlonglong, 2901)

    warning: 693

    warning: seek OK true

    warning: select true

    warning: active true

    warning: valid true

    warning: fields in record 12

    warning: field 0 = "wrdid"

    warning: field 0 value = QVariant(qlonglong, 4312)

    warning: 28

    warning: seek OK true

    warning: select true

    warning: active true

    warning: valid true

    warning: fields in record 12

    warning: field 0 = "wrdid"

    warning: field 0 value = QVariant(qlonglong, 2605)

    warning: 130

    warning: seek OK true

    warning: select true

    warning: active true

    warning: valid true

    warning: fields in record 12

    warning: field 0 = "wrdid"

    warning: field 0 value = QVariant(qlonglong, 2878)

    warning: 98

    warning: seek OK true

    warning: select true

    warning: active true

    warning: valid true

    warning: fields in record 12

    warning: field 0 = "wrdid"

    warning: field 0 value = QVariant(qlonglong, 2804)

    warning: 44

    warning: seek OK true

    warning: select true

    warning: active true

    warning: valid true

    warning: fields in record 12

    warning: field 0 = "wrdid"

    warning: field 0 value = QVariant(qlonglong, 2714)

    warning: 461

    warning: seek OK true

    warning: select true

    warning: active true

    warning: valid true

    warning: fields in record 12

    warning: field 0 = "wrdid"

    warning: field 0 value = QVariant(, )
    Last edited by wysota; 11th March 2008 at 12:06. Reason: Changed [code] to [quote]

  2. #2
    Join Date
    Feb 2008
    Location
    South Korea
    Posts
    14
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: sqlite seek() bug?

    I found a solution to my problem for getting random records.

    Instead of using seek(), SQLite supports the random() function, so I just include this in the SQL statement.
    For example, to get 4 random rows from a table, do this;

    select col1,col2,...,random() r from table order by r limit 4

    Works pefectly

    / Ingemar

Similar Threads

  1. The Sqlite Error In Run!
    By alphaboy in forum Installation and Deployment
    Replies: 1
    Last Post: 19th November 2007, 14:45
  2. Bulk insert into SQLite
    By munna in forum Qt Programming
    Replies: 6
    Last Post: 19th November 2007, 03:56
  3. SQLite make problems
    By raphaelf in forum Newbie
    Replies: 21
    Last Post: 3rd July 2007, 14:40
  4. SQLITE database problems
    By phoenix in forum Newbie
    Replies: 3
    Last Post: 30th April 2007, 21:38
  5. Sqlite & QPixmap Help please
    By munna in forum Qt Programming
    Replies: 2
    Last Post: 4th November 2006, 05:35

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.