PDA

View Full Version : write Records from DB to a binary file based on DB fields Size



beemaneni
26th February 2017, 01:16
Hi,
i have a query regarding where it has involvement of database and Qt. So here i go
I have created a table with some columns which stores different data types.
I need to read them and write to a binary file directly from database without having any intermediate structures.I need to write in hex format in to binary file. Say if i read a value from db as -80 which i stored as an int in database. i need to write it as 4 byte hex value to a file.Here is a part of code



QFile output_file("xyz.dat");
output_file.open(QIODevice::ReadWrite);
QDataStream stream(&output_file);

while(query.next())
{

for(int i=1;i<20;i++){
byteArray.append(query.value(i));
qDebug() << "Record Read :" <<byteArray.at(i);
}
}
output_file << byteArray.toHex();


Please let me know ur thots.Thaks in Advance
Regards
Bala B

jefftee
26th February 2017, 04:35
Not sure I understand your problem... Why mess around with a QByteArray? Just read the data from the db and store in the correct data type (int, long, double, etc). The QDataStream << operator has overloads for all common data types.

beemaneni
26th February 2017, 07:18
i can do that way..no issues with that..but y do i need to have when i have datatypes already in database..Cant i use with out any intermediate structures?
i want to write data from database(which is decimal values and strings in database) to binary file.i want to write hex data to a binary file.if one of my database field is 4 bytes, i wanna store respective 4 bytes value in hexadecimal values and so on.Hope i am clear enough

Regards
Bala B

jefftee
26th February 2017, 19:04
Sorry, I don't understand what you're trying to say. I don't see why you can't use QDataStream to write out all of the data you get from the DB. Is there an example where that can't/doesn't work?

beemaneni
27th February 2017, 01:08
Let me try to explain again.

i have a table of records in my database say few integers , floats and some text. I need to put them in a binary file which i have defined it for reading.Reg binary file , first four bytes of file is integer, next byte is uchar, char, integer etc in the order respectively. Now i need to read the data from db and convert the read data in to hex and write it to binary file.
Here is the method i tried.
Note: I dont want to have any intermediate structures to read values from db and store in them and convert them to hex and write to binary file.Is this possible to do that ?

query.exec(QString("SELECT * FROM CycleSetpointSensitive where Model ='"+modelNumber+"'"));
while(query.next())
{
byteArray.append(query.value(1).toInt()); // just for all fields i have put to int which takes 4 bytes..
byteArray.append(query.value(2).toInt()); // result of this needs to be 1 byte which is tiny int.
byteArray.append(query.value(3).toInt());
byteArray.append(query.value(4).toInt());
byteArray.append(query.value(5).toString());
}
stream << byteArray.toHex(); // stream is QDataStream

So when i put my first value(-80) in to file it shud be stored with 4 bytes as FFFFFFFB.
I have attached the order of my column types in pic

jefftee
27th February 2017, 01:59
So when i put my first value(-80) in to file it shud be stored with 4 bytes as FFFFFFFB.
The int value -80 should be FFFFFFB0 in hex (I assume FFFFFFFB was a typo). I ran the small program below to show what is actually written when you use QDataStream to output the data to a file. I output the -80 integer value and a string, encoded to UTF8 as seen below:


QFile testf("test.dat");
testf.open(QFile::WriteOnly);
int test_int = -80;
QString test_string = "This has been a test.";
QDataStream stream(&testf);
stream << test_int << test_string.toUtf8();
testf.close();


This results in the following contents in test.dat, which will show you that by simply using QDataStream to output the data, no magic is needed. The string value is preceeded by the length of the string, i.e. hex 00000015 is 21, the length of the string I wrote out. If you use QDataStream to output your data, you can also use QDataStream to read the values back from the file, as long as you use the same data type and order, etc.

Here's a screen shot of what the contents of test.dat looks like:

12358

So, just read the data from your database into local variables and then output using QDataStream, no magic required for converting to hex.

Edit: If you don't want to use local variables as in your example, then just do the following:



stream << query.value(1).toInt(); // just for all fields i have put to int which takes 4 bytes..
stream << query.value(2).toInt(); // result of this needs to be 1 byte which is tiny int.
stream << query.value(3).toInt();
stream << query.value(4).toInt();
stream << query.value(5).toString();


For query.value(2), QVariant doesn't seem to have a single byte integer type conversion available, but it will work fine to simply output a 4 byte integer for query.value(2) as well.

beemaneni
27th February 2017, 04:31
Thank you jeffTee.That works good..