Results 1 to 4 of 4

Thread: how to save QVector<float> into sqlite as BLOB?

  1. #1
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default how to save QVector<float> into sqlite as BLOB?

    Hi,

    I am trying to save QVector<float> into database as a BLOB field, but I am not getting the data back correctly. Here is the code.

    Can anyone please tell me what is the problem?

    Many thanks!


    Qt Code:
    1. QByteArray toByarray( const QVector<float>& data )
    2. {
    3. QByteArray result;
    4. QDataStream bWrite( &result, QIODevice::WriteOnly );
    5. bWrite << data;
    6.  
    7. return result;
    8.  
    9. }
    10.  
    11. QVector<float> toVectorF( const QByteArray& buffer )
    12. {
    13. QVector<float> result;
    14. QDataStream bRead( buffer );
    15. bRead >> result;
    16.  
    17. return result;
    18.  
    19. }
    20.  
    21. int saveToDB( const QVector<float>& vec )
    22. {
    23. QSqlQuery query( db );
    24.  
    25. query.prepare( "insert into MyTable ( array ) values ( ? )" );
    26. query.bindValue( 0, toByarray( vec ) );
    27.  
    28. int id( -1 );
    29. if ( query.exec() ) {
    30. id = query.lastInsertId().toInt();
    31. }
    32.  
    33. return id;
    34. }
    35.  
    36. QVector<float> readFromDB( int id )
    37. {
    38. QSqlQuery query( db );
    39.  
    40. query.prepare( "select array from MyTable where id=?" );
    41. query.bindValue( 0, id );
    42.  
    43. QVector<float> result;
    44. if ( query.exec() && query.next() ) {
    45. result = toVectorF( query.value( 0 ).toByteArray() );
    46. }
    47.  
    48. return result;
    49. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: how to save QVector<float> into sqlite as BLOB?

    What is not correct about what you get back?
    Do you have a valid id to retrieve? Does your insert succeed? Does your insert return a valid id? Does the id overflow an int?

  3. #3
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: how to save QVector<float> into sqlite as BLOB?

    It creates a record in the table as I can see using sqlitebrowser and returns a valid id.

    I use that id to read back the BLOB data, and convert back to QVector<float>, the vector length is correct, but the content is wrong.

    The QVector I save is
    QVector(0, 41.41, 69.29, 98.1, 126.92, 155.77, 184.64, 213.48, 242.15, 271.06)

    But when I read back and convert back to QVector, I get:
    QVector(0, 0.59, 0.75, 1.09, 1.5, 1.7, 1.83, 2.13, 2.15, 1.89)
    Last edited by lni; 3rd April 2016 at 06:34.

  4. #4
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: how to save QVector<float> into sqlite as BLOB?

    Problem solved.

    It is stupid that the sql statement missing a commas.

Similar Threads

  1. Multi defined QVector<float>::QVector<float>
    By lni in forum Qt Programming
    Replies: 1
    Last Post: 6th May 2013, 07:58
  2. Replies: 5
    Last Post: 2nd September 2011, 23:11
  3. How to read and write a float* to mySQL as a BLOB?
    By agerlach in forum Qt Programming
    Replies: 1
    Last Post: 20th May 2011, 08:54
  4. QVector<float> to QByteArray without programming
    By Naami in forum Qt Programming
    Replies: 2
    Last Post: 23rd May 2010, 13:40
  5. Save PDF to BLOB
    By wirasto in forum Qt Programming
    Replies: 1
    Last Post: 24th May 2009, 06:47

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.