Skip to content

Why does the DB get sorted when saved from of version 3.9.0 #830

Closed

Description

@rvalk

Details for the issue

Since version 3.9.0 it came to my attention that tables are sorted during save of the database. In version 3.9.0 it was only sorted when you did a refresh of the table, but since version 3.9.1 it always sort the table prior it is saved.

This is very annoying, because I have a config table in the database, which contains configuration parameters for my application which should be saved in the order it was written in the table. Now the table is sorted when saved, I can not read the table anymore, because al configuration items are out of order and my application behaves bad.

Please tell me that this sort option can be switched off somewhere, because I use tables which should not be sorted, but stay in the order the were written.

Useful extra information

I'm opening this issue because:

  • DB4S is crashing
  • DB4S has a bug
  • DB4S needs a feature
  • DB4S has another problem

I'm using DB4S on:

  • Windows: ( version: 10 )
  • Linux: ( distro: ___ )
  • Mac OS: ( version: ___ )
  • Other: ___

I'm using DB4S version:

  • 3.9.1
  • 3.9.0
  • Other: ___

I have also:

Activity

  1. chrisjlocke commented on Oct 16, 2016

    @chrisjlocke
    Member

    Are you sure the records are getting sorted, and not just the view of them? I think in the newer version, the rowid is sorted descending, so newer records are at the top of the list.

    Records are not 'sorted' per se, but have a rowid column. Unless this is an auto increment field, rowids will get reused.

  2. justinclift commented on Oct 16, 2016

    @justinclift
    Member

    @rvalk Ahhh... it sounds like there's a small misunderstanding with how databases work.

    It should be pretty easy to make this work for you though. 😄

    With SQL databases, you control the order of rows using an "ORDER BY" statement in your SQL query:

        http://www.w3schools.com/sql/sql_orderby.asp

    With your application, what is the SQL query you are using at the moment? We can probably suggest how to adjust it, so the order is how you want. 😄

  3. chrisjlocke commented on Oct 16, 2016

    @chrisjlocke
    Member

    There was a question recently on the sqlite mailing list that mentioned sorting, and someone wanted to specifically insert a record between two rowids, so 'shuffle down' the other rowids. Thats not what the rowid column is for. So this was a good explanation:

    You see, in the relational database model there is no such things as "records". What you have above (a table) consists of a "bag of tuples". There is no order to them. They are merely a "bag" (known mathematically as a set). When you issue a query (SELECT) you project sets and conditions and obtain a set of data as a result (another bag of data).

    If you wish an order, you tell it what order to put it in (using an ORDER BY clause). Due to the inherently sequential nature of modern computing technology most programming languages are unable to deal with the data in its native form (a set) and therefore you retrieve and work on them one after each, in forward order only, in the order you requested presentation using the ORDER BY clause.

    The "integer primary key" (whether you use autoincrement of not) is simply a unique key in each record that is derived sequentially (unless you provide it) based on the previous (or biggest ever previously used, if you use autoincrement) number in that field, to uniquely identify the contents of that tuple. It does not contain any useful information which you should be manipulating in any way other than using it as a linkage to perhaps other related tuples in other tables.

  4. vtronko commented on Oct 16, 2016

    @vtronko
    Member

    @justinclift i think he means db sctructure tab.

  5. justinclift commented on Oct 16, 2016

    @justinclift
    Member

    Hmmm, that's possible too. 😉

  6. rvalk commented on Oct 16, 2016

    @rvalk
    Author

    Hello,
    What I use is:
    Select * from adapter;
    If rs <> nil then
    Do until rs.eof
      cgf=cfg+1
      Config(cfg)=rs.field("ConfData").stringvalue
      rs.MoveNext
    Loop
    End if
    The config array will be used to configure the application.
    These entries in the db should stay unchanged as I wrote them in the db, but the last 2 versions of sqlitebrowser sort them ascending before writing the changes.
    That is my problem.
    With kind regards,
    Rob

  7. justinclift commented on Oct 16, 2016

    @justinclift
    Member

    @rvalk Cool. 😄

    You will probably need to update the SQL query to be:

    Select * from adapter ORDER BY something_goes_here;
    

    The something_goes_here is a new column, with a row number in it, so that the order is always given in the row number. Kind of like this:

    something_goes_here field1 field2 field3
    1 abc def ghi
    2 jkl mno pqr
    3 stu vwx yz1

    Does that make sense?

    By the way, pick a better column name than "something_goes_here". 😄

  8. rvalk commented on Oct 16, 2016

    @rvalk
    Author

    chrisjlocke, I am sure it is not only the view. If I restart the application after I saved the db changes, the application goes nuts. When I pause th eapplication and check its config, all the values are in the wrong places. This was not the case with the version prior 3.9.0. With 3.9.0 I really also thought that it was only the view, because I saw that refreshing the records sorted the records in the table, but as far as I know it did not save that sorted order. Now with 3.9.1 I am faced with a changed table. Before upgrading to 3.9.1 I never saw my application going nuts.

    I dont want an order in this table, I know how to order the table ascending or descending on a certain field, but I want this table unsorted in the order that I wrote the records into the table. Unchanged.
    That is the way I do read the table when I start the application, in the order I did write th erecords in the table.

  9. rvalk commented on Oct 16, 2016

    @rvalk
    Author

    Justin, I dont want my table sorted. I only want it in the way I did write the records to the table. These are only 14 records and it is read in the way it was saved when setup was used of my application.

  10. justinclift commented on Oct 16, 2016

    @justinclift
    Member

    @rvalk I understand. It's a pain that this has changed from your point of view. 😦

    The problem is that SQL databases never guarantee an order for their results without being told to (eg using ORDER BY). SQL databases aren't really a spreadsheet, even though they can look like it at first.

    With SQL, you really, really, really need to know how to grab the exact bit of data you want. Using a row order like a spreadsheet is kind of um... playing with fire?

  11. justinclift commented on Oct 16, 2016

    @justinclift
    Member

    @rvalk As an alternative approach, instead of using an order like the above example... do each of your configuration lines have a unique name or something?

  12. rvalk commented on Oct 16, 2016

    @rvalk
    Author

    hmmm...ok, then I will use an index from 1 to 14 to keep the table in the order I want it to be.

    No, just one field. I use it this way to have just 1 database containing all the files I need and not all kind of seperate files on disk. With only one field I can make changes in the setup of my application without a need to adjust the database fields. If I change something one day and have a config item more, I delete the contents of the table and write 15 records to that table. When something else goes out, I delete the contents of the table again and write 14 records in the table. You see my point, I do not need to add or delete fields in the table. This is by the way the only table I use this way, all other tables are used as you would expect a database is used and do not give any problem. :-)

  13. justinclift commented on Oct 16, 2016

    @justinclift
    Member

    Ahhh. Yeah. It sounds like adding an index will probably be the best idea then. 😄

  14. rvalk commented on Oct 16, 2016

    @rvalk
    Author

    Ok, that is what I thought already.
    Fyi, Justin, sqlitebrowser is a great application, I use it during development of my application to check if the info is stored in the tables as I want it and also ti flip bits to see how my application responds on the change.
    Great work!

  15. justinclift commented on Oct 16, 2016

    @justinclift
    Member

    Thanks @rvalk. 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions