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!
QByteArray toByarray
( const QVector<float>
& data
) {
bWrite << data;
return result;
}
QVector<float> toVectorF( const QByteArray& buffer )
{
QVector<float> result;
bRead >> result;
return result;
}
int saveToDB( const QVector<float>& vec )
{
query.prepare( "insert into MyTable ( array ) values ( ? )" );
query.bindValue( 0, toByarray( vec ) );
int id( -1 );
if ( query.exec() ) {
id = query.lastInsertId().toInt();
}
return id;
}
QVector<float> readFromDB( int id )
{
query.prepare( "select array from MyTable where id=?" );
query.bindValue( 0, id );
QVector<float> result;
if ( query.exec() && query.next() ) {
result = toVectorF( query.value( 0 ).toByteArray() );
}
return result;
}
QByteArray toByarray( const QVector<float>& data )
{
QByteArray result;
QDataStream bWrite( &result, QIODevice::WriteOnly );
bWrite << data;
return result;
}
QVector<float> toVectorF( const QByteArray& buffer )
{
QVector<float> result;
QDataStream bRead( buffer );
bRead >> result;
return result;
}
int saveToDB( const QVector<float>& vec )
{
QSqlQuery query( db );
query.prepare( "insert into MyTable ( array ) values ( ? )" );
query.bindValue( 0, toByarray( vec ) );
int id( -1 );
if ( query.exec() ) {
id = query.lastInsertId().toInt();
}
return id;
}
QVector<float> readFromDB( int id )
{
QSqlQuery query( db );
query.prepare( "select array from MyTable where id=?" );
query.bindValue( 0, id );
QVector<float> result;
if ( query.exec() && query.next() ) {
result = toVectorF( query.value( 0 ).toByteArray() );
}
return result;
}
To copy to clipboard, switch view to plain text mode
Bookmarks