Click here to Skip to main content
15,895,746 members
Articles / Desktop Programming / Windows Forms

SQLite Compare Utility

Rate me:
Please Sign up or sign in to vote.
4.89/5 (68 votes)
21 Feb 2015LGPL35 min read 285.3K   37.2K   131  
Utility for comparing two SQLite database files for both structure and data
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SQLite;
using System.IO;


namespace CreateTestDatabases
{
    public class CreateTriggerChecks_1 : IDatabasePairCreator
    {
        #region IDatabasePairCreator Members

        public void CreatePair(string db1, string db2)
        {
            if (File.Exists(db1))
                File.Delete(db1);
            if (File.Exists(db2))
                File.Delete(db2);
            SQLiteConnection.CreateFile(db1);

            using (SQLiteConnection c1 = Utils.CreateConnection(db1))
            {
                c1.Open();

                Utils.RunCommand(
                    "CREATE TABLE exam (ekey      INTEGER PRIMARY KEY," +
                        "fn        VARCHAR(15),                             " +
                        "ln        VARCHAR(30),                             " +
                        "exam      INTEGER,                                 " +
                        "score     DOUBLE,                                  " +
                        "timeEnter DATE)", c1);

                Utils.RunCommand(
                    "CREATE TRIGGER insert_exam_timeEnter AFTER  INSERT ON exam            " +
                        "BEGIN                                                                 " +
                        " UPDATE exam SET timeEnter = DATETIME('NOW')                           " +
                        " WHERE rowid = new.rowid;                                     " +
                        "END", c1);

                Utils.RunCommand(
                    "CREATE TABLE examlog (" +
                    "  lkey INTEGER PRIMARY KEY, " +
                    "  ekey INTEGER, " +
                    "  ekeyOLD INTEGER, " +
                    "  fnNEW   VARCHAR(15)," +
                    "  fnOLD   VARCHAR(15), " +
                    "  lnNEW   VARCHAR(30), " +
                    "  lnOLD   VARCHAR(30), " +
                    "  examNEW INTEGER, " +
                    "  examOLD INTEGER, " +
                    "  scoreNEW DOUBLE, " +
                    "  scoreOLD DOUBLE, " +
                    "  sqlAction VARCHAR(15), " +
                    "  examtimeEnter    DATE, " +
                    "  [sort $it] CONSTRAINT sortit CHECK([sort $it] > 5),"+
                    "  examtimeUpdate   DATE, " +
                    "  timeEnter        DATE)", c1);

                Utils.RunCommand(
                    "CREATE TRIGGER update_examlog AFTER UPDATE  ON exam                   " +
                    " BEGIN                                                                 " +
                    "   INSERT INTO examlog  (ekey,ekeyOLD,fnOLD,fnNEW,lnOLD,               " +
                             "lnNEW,examOLD,examNEW,scoreOLD,               " +
                             "scoreNEW,sqlAction,examtimeEnter,             " +
                             "[sort $it],"+
                             "examtimeUpdate,timeEnter)                     " +
                        "values (new.ekey,old.ekey,old.fn,new.fn,old.ln,             " +
                        " new.ln,old.exam, new.exam,old.score,                " +
                       " new.score, 'UPDATE',old.timeEnter,                  " +
                       "NULL,"+
                        "DATETIME('NOW'),DATETIME('NOW') ); " +
                    "END", c1);

                Utils.RunCommand(
                    "CREATE TRIGGER insert_examlog AFTER INSERT ON exam                    " +
                    " BEGIN                                                                 " +
                    " INSERT INTO examlog  (ekey,fnNEW,lnNEW,examNEW,scoreNEW,              " +
                           "sqlAction,examtimeEnter,timeEnter)              " +
                    " values (new.ekey,new.fn,new.ln,new.exam,new.score,          " +
                       "'INSERT',new.timeEnter,DATETIME('NOW') );           " +
                    " END                 ", c1);

                Utils.RunCommand(
                    "CREATE TRIGGER delete_examlog DELETE ON exam                          " +
                    " BEGIN                                                                 " +
                    "  INSERT INTO examlog  (ekey,fnOLD,lnNEW,examOLD,scoreOLD,              " +
                           "sqlAction,timeEnter)                            " +
                    "     values (old.ekey,old.fn,old.ln,old.exam,old.score,          " +
                    "   'DELETE',DATETIME('NOW') ); " +
                    " END", c1);

                Utils.RunCommand(
                    "CREATE TABLE foo ("+
                    " id INTEGER NOT NULL PRIMARY KEY"+
                    ")", c1);

                Utils.RunCommand(
                    "CREATE TABLE bar (" +
                    " id INTEGER NOT NULL PRIMARY KEY," +
                    " fooId INTEGER CONSTRAINT fk_foo_id REFERENCES foo(id)," +
                    " fooId2 INTEGER NOT NULL CONSTRAINT fk_foo_id2 REFERENCES foo(id) ON DELETE CASCADE" +
                    ")", c1);

                File.Copy(db1, db2);
            } // using
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Software Developer
Israel Israel
My name is Liron Levi and I'm developing software for fun & profit for 15 years already.

Comments and Discussions