Results 1 to 13 of 13

Thread: QSqlDatabase Size Limit Question

  1. #1
    Join Date
    Jul 2010
    Posts
    23
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default QSqlDatabase Size Limit Question

    Is there a way to limit a fix number of records for all tables under QSqlDatabase? For example, I just need to store 100 records for all tables, if newer record comes in, I need to replace the oldest with the newest after 100 entries are filled. Is it possible? if so, how? Thanks.
    HYIP Daily Blog - A must read for all HYIP investors. All HYIP news on the net.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QSqlDatabase Size Limit Question

    You can create a trigger that does the job for you.

  3. #3
    Join Date
    Jul 2010
    Posts
    23
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QSqlDatabase Size Limit Question

    Can you give me some specific examples? I am still novice when it comes to DB. Thanks.
    HYIP Daily Blog - A must read for all HYIP investors. All HYIP news on the net.

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QSqlDatabase Size Limit Question

    see http://en.wikipedia.org/wiki/Database_trigger. In basic a trigger is a sql command which is executed when a specific condition is true. In your case: If after an insert the row number is 100, delete the first ones.

  5. #5
    Join Date
    Jul 2010
    Posts
    23
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QSqlDatabase Size Limit Question

    I know I am asking a bit much, but are there any example on how to use any kind of trigger along with QSqlDatabase? Thanks.
    HYIP Daily Blog - A must read for all HYIP investors. All HYIP news on the net.

  6. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QSqlDatabase Size Limit Question

    It has nothing to do with QSqlDatabase or Qt in general. It is a pure SQL statement which is handled by the DBMS. Therefore you have to tell us, what DBMS you use. MySQL, SQLite... and for them, all the examples are on the URL I gave. So try it yourself first, and then post what you get and we will move on from that.

  7. The following user says thank you to Lykurg for this useful post:

    ken123 (2nd December 2011)

  8. #7
    Join Date
    Jul 2010
    Posts
    23
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QSqlDatabase Size Limit Question

    I am using SQLite. Assuming my table is called Professor with columns of: id, last, first.
    id is auto-incremented. I can't really depend on id because it will increment every time I insert the data.
    What I want do is to delete row #1 whenever number of row is reached 100.
    I tried:
    "delete from professor where rowid = 1;"
    But it only delete it one time, it won't delete the 2nd time. Somehow it still remembers the row #1 I deleted.

    What would be the SQL statement to do what I wanted? Thanks.
    Last edited by ken123; 2nd December 2011 at 19:26.
    HYIP Daily Blog - A must read for all HYIP investors. All HYIP news on the net.

  9. #8
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QSqlDatabase Size Limit Question

    You can do something like
    sql Code:
    1. DELETE FROM professor ORDER BY rowid ASC LIMIT 1
    To copy to clipboard, switch view to plain text mode 

  10. #9
    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: QSqlDatabase Size Limit Question

    The notion of "row #1" doesn't make sense unless you have a definite ordering of rows, e.g, by timestamp, insertion order or something else (you said you didn't want to rely on id (wisely)). Lykurg's example is using the internal rowid for this purpose. Anyway here is an example for Sqlite where each row is time stamped and only the most recent 5 are kept:
    Qt Code:
    1. create table test ( stamp datetime, data varchar(10));
    2. -- Create stuff in random time order
    3. insert into test values (datetime('now', '-09 hours'), 'B');
    4. insert into test values (datetime('now', '-04 hours'), 'G');
    5. insert into test values (datetime('now', '-10 hours'), 'A');
    6. insert into test values (datetime('now', '-03 hours'), 'H');
    7. insert into test values (datetime('now', '-08 hours'), 'C');
    8. insert into test values (datetime('now', '-01 hours'), 'J');
    9. insert into test values (datetime('now', '-02 hours'), 'I');
    10. insert into test values (datetime('now', '-06 hours'), 'E');
    11. insert into test values (datetime('now', '-05 hours'), 'F');
    12. insert into test values (datetime('now', '-07 hours'), 'D');
    13. -- Display it in default and time order
    14. select 'Default order';
    15. select * from test;
    16. select 'Time order';
    17. select * from test order by stamp;
    18. --
    19. -- Keep only five most recent rows
    20. delete from test
    21. where rowid not in (
    22. select rowid
    23. from test
    24. order by stamp desc
    25. limit 5)
    26. ;
    27.  
    28. select 'Default order after delete';
    29. select * from test;
    30. select 'Time order after delete';
    31. select * from test order by stamp;
    To copy to clipboard, switch view to plain text mode 
    There are many ways to do it. There are also quite few ways to get unexpected results. The example may not do the expected thing if two recent timestamps are equal.

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

    ken123 (2nd December 2011)

  12. #10
    Join Date
    Jul 2010
    Posts
    23
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QSqlDatabase Size Limit Question

    Thanks all for your helps, I'll give it a try.
    HYIP Daily Blog - A must read for all HYIP investors. All HYIP news on the net.

  13. #11
    Join Date
    Jul 2010
    Posts
    23
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QSqlDatabase Size Limit Question

    It works. If I have over 10K worth of rows for the table, is there a way to improve the performance for this scheme? Or this is the best strategy to go with?
    Currently if I used the following query statements as ChrisW67 suggested:
    Qt Code:
    1. delete from test
    2. where rowid not in (
    3. select rowid
    4. from test
    5. order by stamp desc
    6. limit 10000);
    To copy to clipboard, switch view to plain text mode 
    Thanks.
    HYIP Daily Blog - A must read for all HYIP investors. All HYIP news on the net.

  14. #12
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QSqlDatabase Size Limit Question

    What do you do not like with that solution? Is it too slow? Improvements cannot be advised without knowledge of your real use case.
    If e.g. you use no transactions and only add one row then querying 10000 ids and use them in a NOT IN is slow. In that particular situation my solution for deleting only the oldest is more efficient.

    Maybe you can think of calculating the count of row which should be deleted and then delete just them.

  15. #13
    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: QSqlDatabase Size Limit Question

    Quote Originally Posted by ken123 View Post
    It works. If I have over 10K worth of rows for the table, is there a way to improve the performance for this scheme? Or this is the best strategy to go with?
    I agree with Lykurg: we can only give general answers without specific knowledge of your actual usage patterns.

    An index on stamp is probably good place to look for performance improvement of my query.
    Different database systems will optimise the query different ways. You might also like to try alternate formulations of the query like :
    Qt Code:
    1. DELETE FROM test
    2. WHERE stamp <= (
    3. SELECT stamp
    4. FROM test
    5. ORDER BY stamp DESC
    6. LIMIT 1 OFFSET 5 -- keep this many records
    7. )
    8. ;
    To copy to clipboard, switch view to plain text mode 

    You could also check the existing row count (a relatively quick query) before running an expensive query that might ultimately delete nothing.

Similar Threads

  1. Replies: 1
    Last Post: 22nd July 2011, 12:22
  2. file size limit in vsftpd
    By sups in forum Qt Programming
    Replies: 1
    Last Post: 17th March 2011, 09:21
  3. How to limit the size of QLineEdit?
    By MIH1406 in forum Qt Programming
    Replies: 6
    Last Post: 30th September 2009, 12:10
  4. Limit window size to certain ranges
    By markwestcott in forum Qt Programming
    Replies: 1
    Last Post: 26th June 2009, 09:50
  5. QVBoxLayout width size limit
    By QPlace in forum Qt Programming
    Replies: 7
    Last Post: 18th June 2009, 16:41

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.