Results 1 to 12 of 12

Thread: Restrict specified number of charecters in each row of Sqlite Db

  1. #1
    Join Date
    Nov 2010
    Posts
    100
    Thanks
    38
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4

    Default Restrict specified number of charecters in each row of Sqlite Db

    Hello , every one i am stuck with a problem which i am not able to clear at all.. I have some continuous data which i am receiving on RS232 terminal. I wanted to save exactly 2944 chars in each row of the db.. This is what i tried first but this does not give the right result..

    Qt Code:
    1. QString sq("INSERT INTO Bulkdb values(%1, %2)");
    2. sq = sq.arg(rcount)
    3. .arg("'" + (stfram.length()>2944)?stfram.left(2944):stfram + "'");
    4. //.arg("'" + stfram + "'");
    To copy to clipboard, switch view to plain text mode 
    i even tried it this way also but only the first row of data is saved which is exactly 2944 char's but it is empty from the second row.. What is the right way of pushing only so many chars of data in each row.. Pls help me out i have been stuck with this problem past 3 days don't know how to go ahead..
    Qt Code:
    1. if(stfram.length()>2944)
    2. newstr.append(stfram.left(2944)
    3. QString sq("INSERT INTO Bulkdb values(%1, %2)");
    4. sq = sq.arg(rcount)
    5. .arg("'" + newstr + "'");
    To copy to clipboard, switch view to plain text mode 

    thank you

  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: Restrict specified number of charecters in each row of Sqlite Db

    You only get the first block of data because you don't iterate to get the remainder of the data.

    You should also get out of the habit of using strings to build SQL statements because the approach is fragile (What happens if the data contains a single quote?) and open to deliberate abuse (look up SQL Injection).

    Something like this (all error checking omitted):
    Qt Code:
    1. QByteArray stfram; // I assume this
    2. const int blockLength = 2944;
    3.  
    4. qry.prepare("insert into bulkdb (col1, col2) values (?, ?)");
    5.  
    6. for (int pos = 0; pos < stfram.length(); pos += blockLength) {
    7. qry.bindValue(0, stfram.mid(pos, blockLength));
    8. qry.bindValue(1, /* some value for col2 */);
    9. qry.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to ChrisW67 for this useful post:

    nagabathula (1st January 2011)

  4. #3
    Join Date
    Nov 2010
    Posts
    100
    Thanks
    38
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4

    Default Re: Restrict specified number of charecters in each row of Sqlite Db

    Hello sir thank you so much for the help.. i tried the code sir this is what i did,
    Here also i have exactly 2944 chars in the first frame, in case i don't clear stfram after a few rows the data is disturbed sir , its not exactly 2944 chars after a few rows so i thought i will remove 2944 chars every iteration and intialize pos =0; Is it right sir..
    Qt Code:
    1. // Data is appending continuously to stfram
    2. qry.prepare("insert into Bulkdb (Time_Interval, ChannelData) values (?, ?)");
    3. for (pos = 0; pos < stfram.length(); pos += blockLength){
    4. qry.bindValue(0, rcount);
    5. qry.bindValue(1, stfram.mid(pos, blockLength));
    6. qry.exec();
    7. rcount++;
    8. stfram.remove(0, blockLength); // trying to clear 2944 length from stfram
    9. pos=0;
    10. }
    To copy to clipboard, switch view to plain text mode 
    i don't exactly get 2944 chars in every frame sir which is the right place to clear this container .. I am calling this function every 5 milli secs.
    thank you

  5. #4
    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: Restrict specified number of charecters in each row of Sqlite Db

    How is data being continuously appended to strfram? This changes the situation quite a lot.

  6. #5
    Join Date
    Nov 2010
    Posts
    100
    Thanks
    38
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4

    Default Re: Restrict specified number of charecters in each row of Sqlite Db

    Hello sir this is what i am doing calling this function every 5 milli secs.
    Qt Code:
    1. void datareceiver::savetodb()
    2. if(port->bytesAvailable())
    3. {
    4. numBytes = port->bytesAvailable();
    5. if(numBytes > sizeof(buff))
    6. numBytes = sizeof(buff);
    7. i = port->read(buff,numBytes);
    8. QByteArray data = (QByteArray::fromRawData(buff,numBytes));
    9. quint16 r;
    10. //****************** Bit Wise Shift Left and ORing of two Bytes **/
    11. for (int i = 0; i < data.size(); i+=2)
    12. {
    13. r = (((quint16) data.at(i) << 5) |(data.at(i+1)));
    14. char *ptrR = (char*) &r;
    15. result.append(ptrR[1]);
    16. result.append(ptrR[0]);
    17. }
    18. oldat.append(result.toHex());
    19.  
    20. //******************search for First frame ID and save to db from there*********************/
    21. if(datrow ==0)
    22.  
    23. {
    24. QByteArray pattern("03b702200000");
    25. QByteArrayMatcher matcher(pattern);matcher(pattern);
    26. pos = matcher.indexIn(oldat, pos);
    27. datrow++;
    28. }
    29. for(np = pos; np<oldat.size(); np++)
    30. {
    31. stfram.append(oldat.at(np));
    32. pos++;
    33. }
    34.  
    35. /********** Saving to db every 2944 i.e one Master Frame**************/
    36. if((stfram.length()>2944) && (stfram.startsWith("03b702200000")))
    37. {
    38. QSqlQuery qry;
    39. qry.prepare("insert into Bulkdb (Time_Interval, ChannelData) values (?, ?)");
    40. for (npos = 0; npos < stfram.length(); npos += blockLength){
    41. qry.bindValue(0, rcount);
    42. qry.bindValue(1, stfram.mid(npos, blockLength));
    43. qry.exec();
    44. rcount++;
    45. }
    46. }
    47. }
    48. stfram.clear();
    49. npos=0;
    50. }
    To copy to clipboard, switch view to plain text mode 


    Thank you sir
    Last edited by nagabathula; 3rd January 2011 at 02:57.

  7. #6
    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: Restrict specified number of charecters in each row of Sqlite Db

    OK, so extra data is coming in frequently and possibly asynchronously (another thread).

    Qt Code:
    1. // Data is appending continuously to stfram
    2. qry.prepare("insert into Bulkdb (Time_Interval, ChannelData) values (?, ?)");
    3. for (pos = 0; pos < stfram.length(); pos += blockLength){
    4. qry.bindValue(0, rcount);
    5. qry.bindValue(1, stfram.mid(pos, blockLength));
    6. qry.exec();
    7. rcount++;
    8. stfram.remove(0, blockLength); // trying to clear 2944 length from stfram
    9. pos=0;
    10. }
    To copy to clipboard, switch view to plain text mode 

    Resetting pos results in an endless for loop if data keeps arriving (you'd be better off using a while loop). If the data provider is in the same thread then it will not run until the event loop is reached. The for loop will terminate when the buffer is empty (and potentially a partial block is written to the database) and return to the event loop.

    If the data provider is in another thread then you have other issues with synchronisation of access and the possibility below.

    If there is any chance that more data can arrive in the buffer between line 6 and line 9 then this approach will fail. To see why:
    • Imagine that the stfram contains 1000 bytes at the start of the for loop
    • At line 6 and 7 up to blockLength bytes are copied into the database. In this case 1000 bytes.
    • Before line 9 is reached more data arrives so the buffer contain 1200 bytes.
    • At line 9 up to blockLength bytes are removed. The buffer is now empty but the last 200 bytes have been lost.

  8. #7
    Join Date
    Nov 2010
    Posts
    100
    Thanks
    38
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4

    Default Re: Restrict specified number of charecters in each row of Sqlite Db

    Sir i was thinking if i stop the timer before i start saving the data to db and then start the timer1 again after saving the data and clearing all the containers..
    Can you pls tell me what is the best approach sir so that i don't lose any data and i am able to save every frame in the db.? I have been trying many methods past week to get the data right but i am going wrong some wer so i am missing some data in btw.i will use a while loop instead and try sir,

    Qt Code:
    1. if((stfram.length()>2944) && (stfram.startsWith("03b702200000")))
    2. {
    3. timer1->stop();
    4. QSqlQuery qry;
    5. qry.prepare("insert into Bulkdb (Time_Interval, ChannelData) values (?, ?)");
    6. for (npos = 0; npos < stfram.length(); npos += blockLength){
    7. qry.bindValue(0, rcount);
    8. qry.bindValue(1, stfram.mid(npos, blockLength));
    9. qry.exec();
    10. rcount++;
    11. }
    12. timer1->start(20);
    13. }
    14. }
    15. stfram.clear();
    16. npos=0;
    17. }
    To copy to clipboard, switch view to plain text mode 

    sir and instead of using stfram.remove(0, blockLength); i am using stfram.clear(); now , but that wud empty the whole container no sir. and sud't i be making pos =0; when i clear the stfram cause the container will be empty then no sir and the pos should also be in initial condition zero.?

    If the data provider is in the same thread then it will not run until the event loop is reached
    yes sir the data provider is in the same thread but what is event loop sir i din't understand that line.

  9. #8
    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: Restrict specified number of charecters in each row of Sqlite Db

    The event loop is the mechanism that orchestrates the event-driven GUI. It is where the "idle" program spends its time. QTimer timeout() signals are not sent until the program reaches the "idle" state. If the timer is in the same thread no data can be added to the buffer while your program is in a loop writing to the database.

    If you only want to save whole blocks in each record of the database, and leave any partial block in the buffer, then it really is as simple as this:
    Qt Code:
    1. qry.prepare("insert into Bulkdb (Time_Interval, ChannelData) values (?, ?)");
    2.  
    3. while (stfram.length() >= blockLength) {
    4. qry.bindValue(0, rcount++);
    5. qry.bindValue(1, stfram.left(blockLength));
    6. qry.exec();
    7. stfram = stfram.mid(blockLength);
    8. }
    To copy to clipboard, switch view to plain text mode 

  10. The following user says thank you to ChrisW67 for this useful post:

    nagabathula (3rd January 2011)

  11. #9
    Join Date
    Nov 2010
    Posts
    100
    Thanks
    38
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4

    Default Re: Restrict specified number of charecters in each row of Sqlite Db

    Hello sir thank you so much for your time and help..
    i don't have to clear the stfram buffer here no sir. ?
    stfram = stfram.mid(blockLength);
    here stfram data is replaced with data after 2944 chars right sir so i don't have to clear this buffer.. But i have a small problem sir after 3 rows the data is empty and another time when i tested it showed the right data 2944 chars for all the rows that time there were many rows more then 100 rows of right data..

    sir what is the basics of clearing a container which we use in our program. I have to clear the containers right after every iteration. ? cause i have used a few more QByteArrays before this where i am retrieving and aligning the data before i plan to push it into the database.

    Thank you
    Last edited by nagabathula; 3rd January 2011 at 07:22.

  12. #10
    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: Restrict specified number of charecters in each row of Sqlite Db

    I cannot tell you what the logic of your program is or should be. The buffer will be emptied of whole records by the while loop which will exit if there is not a whole record in the buffer. Presumably, when more data is added to the buffer you will call this code again. If no more data is appended to the buffer then more records will never be written to the database.

    If you want to throw away the content of the buffer the the clear() method is good for that.

  13. The following user says thank you to ChrisW67 for this useful post:

    nagabathula (3rd January 2011)

  14. #11
    Join Date
    Nov 2010
    Posts
    100
    Thanks
    38
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4

    Default Re: Restrict specified number of charecters in each row of Sqlite Db

    Hello sir i have one more doubt .. I am actually doing this to save the data into the rsdata from rs232 port
    Qt Code:
    1. QByteArray rsdata = (QByteArray::fromRawData(buff,numBytes));
    To copy to clipboard, switch view to plain text mode 
    but i read the detail description of
    QByteArray QByteArray::fromRawData ( const char * data, int size )
    Constructs a QByteArray that uses the first size characters in the array data. The bytes in data are not copied. The caller must be able to guarantee that data will not be deleted or modified as long as the QByteArray (or an unmodified copy of it) exists.
    Any attempts to modify the QByteArray or copies of it will cause it to create a deep copy of the data, ensuring that the raw data isn't modified.
    but i am doing some bitwise shifting and aligining of the data so can it be the reason i am losing data sir, ?

    I tried it this way i though i will append the data into an QByteArray rsdata; is this right sir it sud append the data in buff but i am not seeing the right data.
    Qt Code:
    1. rsdata.append((QByteArray::fromRawData(buff,numBytes)).toHex());
    To copy to clipboard, switch view to plain text mode 


    thank you

  15. #12
    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: Restrict specified number of charecters in each row of Sqlite Db

    I cannot tell you why you are losing data.

    It is easy enough to do a simple experiment to find out if the sort of manipulations you do will cause a deep copy:
    Qt Code:
    1. #include <QtCore>
    2. #include <QDebug>
    3.  
    4. int main(int argc, char **argv)
    5. {
    6. QCoreApplication app(argc, argv);
    7.  
    8. char buf[] = "....";
    9. QByteArray a = QByteArray::fromRawData(buf, 4);
    10. QByteArray b = a; // shallow copy of a
    11. qDebug() << buf << a << b; // all point to buf and show same data
    12.  
    13. buf[0] = 'X'; // modify original buffer directly
    14. qDebug() << buf << a << b; // all changed because the share the buffer
    15.  
    16. b[0] = 'Y'; // change using the byte array interface causes deep copy
    17. qDebug() << buf << a << b; // only b changed after deep-copy
    18. }
    To copy to clipboard, switch view to plain text mode 
    Output:
    Qt Code:
    1. .... "...." "...."
    2. X... "X..." "X..."
    3. X... "X..." "Y..."
    To copy to clipboard, switch view to plain text mode 

  16. The following user says thank you to ChrisW67 for this useful post:

    nagabathula (5th January 2011)

Similar Threads

  1. Replies: 4
    Last Post: 16th October 2015, 15:13
  2. QTableView restrict ScrollArea
    By hoshy in forum Qt Programming
    Replies: 2
    Last Post: 6th April 2009, 08:54
  3. Can we restrict the movement of a Qt Window?
    By Frank J. Lhota in forum Qt Programming
    Replies: 4
    Last Post: 21st October 2008, 20:34
  4. How to restrict autodelete in Qt?
    By vql in forum Qt Programming
    Replies: 1
    Last Post: 23rd March 2008, 11:28
  5. Restrict size of QLineEdit
    By bruccutler in forum Newbie
    Replies: 2
    Last Post: 19th March 2007, 16:31

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
  •  
Qt is a trademark of The Qt Company.