Results 1 to 7 of 7

Thread: write Records from DB to a binary file based on DB fields Size

  1. #1
    Join Date
    Jul 2013
    Posts
    33
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default write Records from DB to a binary file based on DB fields Size

    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

    Qt Code:
    1. QFile output_file("xyz.dat");
    2. output_file.open(QIODevice::ReadWrite);
    3. QDataStream stream(&output_file);
    4.  
    5. while(query.next())
    6. {
    7.  
    8. for(int i=1;i<20;i++){
    9. byteArray.append(query.value(i));
    10. qDebug() << "Record Read :" <<byteArray.at(i);
    11. }
    12. }
    13. output_file << byteArray.toHex();
    To copy to clipboard, switch view to plain text mode 

    Please let me know ur thots.Thaks in Advance
    Regards
    Bala B
    Last edited by anda_skoa; 26th February 2017 at 10:10. Reason: missing [code] tags

  2. #2
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: write Records from DB to a binary file based on DB fields Size

    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.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  3. #3
    Join Date
    Jul 2013
    Posts
    33
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: write Records from DB to a binary file based on DB fields Size

    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

  4. #4
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: write Records from DB to a binary file based on DB fields Size

    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?
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  5. #5
    Join Date
    Jul 2013
    Posts
    33
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: write Records from DB to a binary file based on DB fields Size

    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
    Attached Images Attached Images

  6. #6
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: write Records from DB to a binary file based on DB fields Size

    Quote Originally Posted by beemaneni View Post
    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:
    Qt Code:
    1. QFile testf("test.dat");
    2. testf.open(QFile::WriteOnly);
    3. int test_int = -80;
    4. QString test_string = "This has been a test.";
    5. QDataStream stream(&testf);
    6. stream << test_int << test_string.toUtf8();
    7. testf.close();
    To copy to clipboard, switch view to plain text mode 

    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:

    hex.jpg

    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:

    Qt Code:
    1. stream << query.value(1).toInt(); // just for all fields i have put to int which takes 4 bytes..
    2. stream << query.value(2).toInt(); // result of this needs to be 1 byte which is tiny int.
    3. stream << query.value(3).toInt();
    4. stream << query.value(4).toInt();
    5. stream << query.value(5).toString();
    To copy to clipboard, switch view to plain text mode 

    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.
    Last edited by jefftee; 27th February 2017 at 03:10.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  7. #7
    Join Date
    Jul 2013
    Posts
    33
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: write Records from DB to a binary file based on DB fields Size

    Thank you jeffTee.That works good..

Similar Threads

  1. Replies: 6
    Last Post: 10th June 2012, 20:43
  2. Replies: 2
    Last Post: 2nd November 2010, 06:15
  3. Replies: 0
    Last Post: 26th October 2010, 18:59
  4. How to write a QList into a binary file?
    By Lawand in forum Qt Programming
    Replies: 23
    Last Post: 15th June 2009, 11:04
  5. Writing Log Records To A Log File
    By vermarajeev in forum Qt Programming
    Replies: 9
    Last Post: 20th March 2007, 14:08

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.