What is the best way to update sqlite schema and data on a software update?












0















I'm including an SQLite database in a recent application I'm designing. This will sit in the application folder and will contain a mixture of stock and user data. It's being developed via .net and entity framework.



I'm wanting to know how to approach updating this database between application updates (say adding tables and fields) whilst also preserving user data client-side when the user updates the application. I'm assuming that if I just change the database schema and data development side and then just overwrite to client side .db file on update, then you'll loose all of the user data.



Any suggestions?










share|improve this question























  • You mentioned that you are using Entity Framework, EF has "migrations" feature which allow you to keep target database in sync and have a history of changes.

    – Fabio
    Nov 19 '18 at 23:20
















0















I'm including an SQLite database in a recent application I'm designing. This will sit in the application folder and will contain a mixture of stock and user data. It's being developed via .net and entity framework.



I'm wanting to know how to approach updating this database between application updates (say adding tables and fields) whilst also preserving user data client-side when the user updates the application. I'm assuming that if I just change the database schema and data development side and then just overwrite to client side .db file on update, then you'll loose all of the user data.



Any suggestions?










share|improve this question























  • You mentioned that you are using Entity Framework, EF has "migrations" feature which allow you to keep target database in sync and have a history of changes.

    – Fabio
    Nov 19 '18 at 23:20














0












0








0








I'm including an SQLite database in a recent application I'm designing. This will sit in the application folder and will contain a mixture of stock and user data. It's being developed via .net and entity framework.



I'm wanting to know how to approach updating this database between application updates (say adding tables and fields) whilst also preserving user data client-side when the user updates the application. I'm assuming that if I just change the database schema and data development side and then just overwrite to client side .db file on update, then you'll loose all of the user data.



Any suggestions?










share|improve this question














I'm including an SQLite database in a recent application I'm designing. This will sit in the application folder and will contain a mixture of stock and user data. It's being developed via .net and entity framework.



I'm wanting to know how to approach updating this database between application updates (say adding tables and fields) whilst also preserving user data client-side when the user updates the application. I'm assuming that if I just change the database schema and data development side and then just overwrite to client side .db file on update, then you'll loose all of the user data.



Any suggestions?







.net entity-framework sqlite






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 '18 at 22:44









stigzlerstigzler

64217




64217













  • You mentioned that you are using Entity Framework, EF has "migrations" feature which allow you to keep target database in sync and have a history of changes.

    – Fabio
    Nov 19 '18 at 23:20



















  • You mentioned that you are using Entity Framework, EF has "migrations" feature which allow you to keep target database in sync and have a history of changes.

    – Fabio
    Nov 19 '18 at 23:20

















You mentioned that you are using Entity Framework, EF has "migrations" feature which allow you to keep target database in sync and have a history of changes.

– Fabio
Nov 19 '18 at 23:20





You mentioned that you are using Entity Framework, EF has "migrations" feature which allow you to keep target database in sync and have a history of changes.

– Fabio
Nov 19 '18 at 23:20












1 Answer
1






active

oldest

votes


















0














You basically need something to compare against in the live database against the new.



e.g. if adding a table then you could use SELECT * FROM sqlite_master WHERE name = your_new_table_name to see if the table already exists. However you could use the simpler CREATE TABLE IF NOT EXISTS ........



New columns, with limitations, you could use ALTER TABLE your_existing_tablename ADD COLUMN you_new_column_definition (existing data is kept, if provided a default value will be applied).



Limitations are :-





  1. The column may not have a PRIMARY KEY or UNIQUE constraint.

  2. The column may not have a default value of CURRENT_TIME, CURRENT_DATE, CURRENT_TIMESTAMP, or an expression in parentheses.

  3. If a NOT NULL constraint is specified, then the column must have a default value other than NULL.


  4. If foreign key constraints are enabled and a column with a REFERENCES clause is added, the column must have a default value of
    NULL.




    • Note also that when adding a CHECK constraint, the CHECK constraint is not tested against preexisting rows of the table. This can result in a table that contains data that is in violation of the CHECK constraint. Future versions of SQLite might change to validate CHECK constraints as they are added.






SQL As Understood By SQLite - ALTER TABLE



If the column already exists then the result would include a message saying that the column is a duplicate e.g. :-



ALTER TABLE ex01 ADD COLUMN col5 TEXT
> duplicate column name: col5
> Time: 0s


You could always extract the existing columns using PRAGMA table_info(your_existing_table_name); and only proceed if the column to be added is not one of the values in the name column of the result set.




  • Note that the link above suggests other ways to handle more complex situations.


With Android the stock SQLiteDatabase methods utilise the user_version (bytes 60 63in the database header).) to hold the actual version of the database which is compared against a provided value. If the provided value is greater then the onUpgrade method is invoked.



PRAGMA Statements
Database File Format



P.S. Asking Best can be lead to opinion based responses, sometimes casuing heated arguments and thus asking for the Best should be avodied. Rather ask for ways.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53383720%2fwhat-is-the-best-way-to-update-sqlite-schema-and-data-on-a-software-update%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You basically need something to compare against in the live database against the new.



    e.g. if adding a table then you could use SELECT * FROM sqlite_master WHERE name = your_new_table_name to see if the table already exists. However you could use the simpler CREATE TABLE IF NOT EXISTS ........



    New columns, with limitations, you could use ALTER TABLE your_existing_tablename ADD COLUMN you_new_column_definition (existing data is kept, if provided a default value will be applied).



    Limitations are :-





    1. The column may not have a PRIMARY KEY or UNIQUE constraint.

    2. The column may not have a default value of CURRENT_TIME, CURRENT_DATE, CURRENT_TIMESTAMP, or an expression in parentheses.

    3. If a NOT NULL constraint is specified, then the column must have a default value other than NULL.


    4. If foreign key constraints are enabled and a column with a REFERENCES clause is added, the column must have a default value of
      NULL.




      • Note also that when adding a CHECK constraint, the CHECK constraint is not tested against preexisting rows of the table. This can result in a table that contains data that is in violation of the CHECK constraint. Future versions of SQLite might change to validate CHECK constraints as they are added.






    SQL As Understood By SQLite - ALTER TABLE



    If the column already exists then the result would include a message saying that the column is a duplicate e.g. :-



    ALTER TABLE ex01 ADD COLUMN col5 TEXT
    > duplicate column name: col5
    > Time: 0s


    You could always extract the existing columns using PRAGMA table_info(your_existing_table_name); and only proceed if the column to be added is not one of the values in the name column of the result set.




    • Note that the link above suggests other ways to handle more complex situations.


    With Android the stock SQLiteDatabase methods utilise the user_version (bytes 60 63in the database header).) to hold the actual version of the database which is compared against a provided value. If the provided value is greater then the onUpgrade method is invoked.



    PRAGMA Statements
    Database File Format



    P.S. Asking Best can be lead to opinion based responses, sometimes casuing heated arguments and thus asking for the Best should be avodied. Rather ask for ways.






    share|improve this answer




























      0














      You basically need something to compare against in the live database against the new.



      e.g. if adding a table then you could use SELECT * FROM sqlite_master WHERE name = your_new_table_name to see if the table already exists. However you could use the simpler CREATE TABLE IF NOT EXISTS ........



      New columns, with limitations, you could use ALTER TABLE your_existing_tablename ADD COLUMN you_new_column_definition (existing data is kept, if provided a default value will be applied).



      Limitations are :-





      1. The column may not have a PRIMARY KEY or UNIQUE constraint.

      2. The column may not have a default value of CURRENT_TIME, CURRENT_DATE, CURRENT_TIMESTAMP, or an expression in parentheses.

      3. If a NOT NULL constraint is specified, then the column must have a default value other than NULL.


      4. If foreign key constraints are enabled and a column with a REFERENCES clause is added, the column must have a default value of
        NULL.




        • Note also that when adding a CHECK constraint, the CHECK constraint is not tested against preexisting rows of the table. This can result in a table that contains data that is in violation of the CHECK constraint. Future versions of SQLite might change to validate CHECK constraints as they are added.






      SQL As Understood By SQLite - ALTER TABLE



      If the column already exists then the result would include a message saying that the column is a duplicate e.g. :-



      ALTER TABLE ex01 ADD COLUMN col5 TEXT
      > duplicate column name: col5
      > Time: 0s


      You could always extract the existing columns using PRAGMA table_info(your_existing_table_name); and only proceed if the column to be added is not one of the values in the name column of the result set.




      • Note that the link above suggests other ways to handle more complex situations.


      With Android the stock SQLiteDatabase methods utilise the user_version (bytes 60 63in the database header).) to hold the actual version of the database which is compared against a provided value. If the provided value is greater then the onUpgrade method is invoked.



      PRAGMA Statements
      Database File Format



      P.S. Asking Best can be lead to opinion based responses, sometimes casuing heated arguments and thus asking for the Best should be avodied. Rather ask for ways.






      share|improve this answer


























        0












        0








        0







        You basically need something to compare against in the live database against the new.



        e.g. if adding a table then you could use SELECT * FROM sqlite_master WHERE name = your_new_table_name to see if the table already exists. However you could use the simpler CREATE TABLE IF NOT EXISTS ........



        New columns, with limitations, you could use ALTER TABLE your_existing_tablename ADD COLUMN you_new_column_definition (existing data is kept, if provided a default value will be applied).



        Limitations are :-





        1. The column may not have a PRIMARY KEY or UNIQUE constraint.

        2. The column may not have a default value of CURRENT_TIME, CURRENT_DATE, CURRENT_TIMESTAMP, or an expression in parentheses.

        3. If a NOT NULL constraint is specified, then the column must have a default value other than NULL.


        4. If foreign key constraints are enabled and a column with a REFERENCES clause is added, the column must have a default value of
          NULL.




          • Note also that when adding a CHECK constraint, the CHECK constraint is not tested against preexisting rows of the table. This can result in a table that contains data that is in violation of the CHECK constraint. Future versions of SQLite might change to validate CHECK constraints as they are added.






        SQL As Understood By SQLite - ALTER TABLE



        If the column already exists then the result would include a message saying that the column is a duplicate e.g. :-



        ALTER TABLE ex01 ADD COLUMN col5 TEXT
        > duplicate column name: col5
        > Time: 0s


        You could always extract the existing columns using PRAGMA table_info(your_existing_table_name); and only proceed if the column to be added is not one of the values in the name column of the result set.




        • Note that the link above suggests other ways to handle more complex situations.


        With Android the stock SQLiteDatabase methods utilise the user_version (bytes 60 63in the database header).) to hold the actual version of the database which is compared against a provided value. If the provided value is greater then the onUpgrade method is invoked.



        PRAGMA Statements
        Database File Format



        P.S. Asking Best can be lead to opinion based responses, sometimes casuing heated arguments and thus asking for the Best should be avodied. Rather ask for ways.






        share|improve this answer













        You basically need something to compare against in the live database against the new.



        e.g. if adding a table then you could use SELECT * FROM sqlite_master WHERE name = your_new_table_name to see if the table already exists. However you could use the simpler CREATE TABLE IF NOT EXISTS ........



        New columns, with limitations, you could use ALTER TABLE your_existing_tablename ADD COLUMN you_new_column_definition (existing data is kept, if provided a default value will be applied).



        Limitations are :-





        1. The column may not have a PRIMARY KEY or UNIQUE constraint.

        2. The column may not have a default value of CURRENT_TIME, CURRENT_DATE, CURRENT_TIMESTAMP, or an expression in parentheses.

        3. If a NOT NULL constraint is specified, then the column must have a default value other than NULL.


        4. If foreign key constraints are enabled and a column with a REFERENCES clause is added, the column must have a default value of
          NULL.




          • Note also that when adding a CHECK constraint, the CHECK constraint is not tested against preexisting rows of the table. This can result in a table that contains data that is in violation of the CHECK constraint. Future versions of SQLite might change to validate CHECK constraints as they are added.






        SQL As Understood By SQLite - ALTER TABLE



        If the column already exists then the result would include a message saying that the column is a duplicate e.g. :-



        ALTER TABLE ex01 ADD COLUMN col5 TEXT
        > duplicate column name: col5
        > Time: 0s


        You could always extract the existing columns using PRAGMA table_info(your_existing_table_name); and only proceed if the column to be added is not one of the values in the name column of the result set.




        • Note that the link above suggests other ways to handle more complex situations.


        With Android the stock SQLiteDatabase methods utilise the user_version (bytes 60 63in the database header).) to hold the actual version of the database which is compared against a provided value. If the provided value is greater then the onUpgrade method is invoked.



        PRAGMA Statements
        Database File Format



        P.S. Asking Best can be lead to opinion based responses, sometimes casuing heated arguments and thus asking for the Best should be avodied. Rather ask for ways.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 '18 at 23:14









        MikeTMikeT

        16.5k112642




        16.5k112642
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53383720%2fwhat-is-the-best-way-to-update-sqlite-schema-and-data-on-a-software-update%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Academy of Television Arts & Sciences

            L'Équipe

            1995 France bombings