Click here to Skip to main content
15,884,917 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 281K   37.1K   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 CreateDataChecks_3 : 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 log (" +
                    " id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," +
                    " user_id INTEGER NULL DEFAULT NULL," +
                    " type_id INTEGER NOT NULL," +
                    " severity_id TINYINT NOT NULL," +
                    " message VARCHAR(255) NOT NULL," +
                    " data LONGBLOB NULL," +
                    " timestamp DATETIME NOT NULL," +
                    " ip INTEGER UNSIGNED NOT NULL" +
                    " )", c1);

                Utils.RunCommand(
                    "CREATE TABLE test1 (" +
                    "     a," +
                    "     b COLLATE BINARY," +
                    "     c COLLATE RTRIM," +
                    "     d COLLATE NOCASE" +
                    ")", c1);

                SQLiteTransaction tx1 = c1.BeginTransaction();
                InsertData(c1, tx1);
                tx1.Commit();

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

        #endregion

        private void InsertData(SQLiteConnection conn, SQLiteTransaction tx)
        {
            Random rand = new Random();
            SQLiteCommand insert1 = new SQLiteCommand("INSERT INTO log " +
                "(user_id,type_id,severity_id,message,data, timestamp,ip) VALUES " +
                "(1,2,3,'message',X'01020304','2004-12-12',4)", conn, tx);

            SQLiteCommand insert2 = new SQLiteCommand("INSERT INTO test1 (a,b,c,d) VALUES " +
                    "(1,2,'c','d')", conn, tx);

            for (int i = 0; i < 100; i++)
            {
                insert1.ExecuteNonQuery();
                insert2.ExecuteNonQuery();
            } // for
        }
    }
}

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