Click here to Skip to main content
Click here to Skip to main content

Convert SQL Server DB to SQLite DB

By , 13 Jan 2013
 
convsqlservertosqlite/converter.png

Introduction

I needed to convert the existing SQL server databases to SQLite databases as part of a DB migration program and did not find any decent free converter to do the job.

This is my attempt to solve the problem. I should warn you though that I did not have much time to test it on too many databases. In any case - the source code is very well documented and easy to understand, so if you do have a problem it should be relatively easy to fix. (Please send me the fixed source code. If you do so, I can update the software so that everybody can enjoy it.)

Using the Code

The code is split between a dataaccess project (class library) that contains the conversion code itself and a converter project (WinForms) that drives the conversion code and provides a simple UI for user interaction.

The main class that performs the conversion is the sqlservertosqlite class. It does the conversion by doing the following steps:

  1. Reading the designated SQL server schema and preparing a list of tableschema objects that contain the schema for each and every SQL server table (and associated structures like indexes).
  2. Preparing an empty sqlite database file with the schema that was read from SQL server. In this step, the code may alter few SQL-server types that are not supported directly in sqlite.
  3. Copying rows for each table from the SQL server database to the sqlite database.

Basically, that's it!

Points of Interest

In order to read the SQL server DB schema, I was mainly using the pseudo information_schema.table table. You can find more information about it on the Internet if you want.

History

  • 13th June, 2008: Initial version
  • 08th July, 2008: v 1.2
    • Fixed a bug that caused unique indexes to be generated as non-unique in some cases
  • 08th July, 2008: v 1.3
    • Fixed a bug that caused the utility to crash sometimes when processing index information
  • 17th July, 2008: v 1.4
    • Fixed a bug that caused wrong columns to become primary keys on rare occasions and improved conversion performance
  • 20th July, 2008: v 1.5
    • Added support for case-insensitive columns (COLLATE NOCASE)
  • 22nd July, 2008: v 1.6
    • Added support for encrypting the resulting DB file (using the built-in encryption support that exists in the SQLite .NET provider)
  • 05th October, 2008: v 1.7
    • Fixed information_schema references to use UPPER-CASE in order to resolve international character set issues (Turkish)
  • 14th December 2008: v1.8
    • Integrated support for foreign keys from the revised version made by Yogesh Jagota
    • Merged support for selective table import
  • 21st February 2009: v1.9
    • Added contribution from johnny dickson cano that allows to select using SQL server integrated security or using user name /password instead
    • Added support for converting IDENTITY columns to AUTOINCREMENT in SQLite (suggestion by Paul Shaffer)
  • 04th March 2009: v1.10
    • Fixed a bug that caused the converter to crash when encountering a datetime field in the original SQL server schema. Thanks to bmcclint for sending me the bug with the correct bugfix.
  • 23rd May 2009: v1.11
    • Added support for simulating foreign keys using triggers (contributed by Martijn Muurman)
    • Added a small bugfix so that now an 'int' type is always converted to 'integer' type in sqlite. This was needed because sqlite will autoincrement only on 'integer' column types.
  • 04th June 2009: v1.12
    • Fixed a bug in trigger generation code that caused schema generation to fail when more than a single column is referencing the same column in a foreign table
  • 20th September 2009: v1.13
    • Fixed AUTOINCREMENT bug suggested by MAEP
    • Fixed 64 bit support problem (thanks to Murry Gammash)
    • Added support for converting SQL Server views (suggested by Richard Thurgood)
  • 22nd September 2009: v1.14
    • Fixed a critical bug that caused the conversion process to fail on some SQL Server databases that used the [dbo] notation.
  • 25th September 2009: v1.15
    • Fixed a critical bug that caused the primary keys to be discarded
    • Fixed trigger generation bug
  • 4th December 2009: v1.16
    • Fixed generation code to create GUID types for SQL-Server's uniqueidentifier type (instead of nvarchar as it was until now)
    • Updated the solution to use the latest SQLite .NET provider library
  • 24th March, 2011
    • Attached compiled version of the project for anyone that doesn't have Visual Studio and still needs to use the utility
  • 1st July, 2011
    • Updated binary zip as the earlier one was missing the DLL file needed by the application
  • 15th Nov 2011: v1.17
    • Fixed a bug that caused the software to crash when encountering NULL values in some of SQL Server meta data tables
  • 19th June 2012: v1.19
    • Added support for ignoring views when creating the DB schema, Added support for blank characters inside column names.
  •  14th January 2013: v1.20
    • Fixed problem with column names
    • Added more width to the database names combo box. 

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication

About the Author

liron.levi
Software Developer Arineta Cardio Imaging
Israel Israel
Member
My name is Liron Levi and I'm developing software for fun & profit for 15 years already. I'm now working for Arineta Cardio Imaging as a software developer (the company develops a CT machine).
 
I can be contacted directly at liron.levi@outlook.com

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
AnswerRe: superb program :)memberliron.levi10 Mar '13 - 21:09 
Hi James
 
All the parameters that you put in the UI can be programmatically entered by using the SqlServerToSQLite class and calling ConvertSqlServerToSQLiteDatabase.
Unfortunately I'm too busy at the moment to make changes in this program.
 
HTH
Liron
AnswergoodmemberMember 15605935 Feb '13 - 21:32 
english: Thanks.. good.
chinese: 很好用 很牛逼
QuestionGreatmemberWin32nipuh23 Jan '13 - 4:22 
Thank you.
 
I have an exception during the conversion: System.DateTime cannot be converted to System.Byte[]
AnswerRe: Greatmemberliron.levi26 Jan '13 - 19:53 
Hi
 
The utility issues a log file (under the folder that contains the executable).
 
Can you please send the zipped log folder to me (liron.levi@outlook.com) ?
 
Thanks in advance
Liron
GeneralMy vote of 5memberEdo Tzumer14 Jan '13 - 1:42 
איזה יופי,
 
חוסך יומיים פתוח!
 
Splendid piece Liron!
GeneralRe: My vote of 5memberliron.levi14 Jan '13 - 4:12 
אני שמח שזה עזר לך
נחמד לראות שגם דוברי עברית משתמשים בכלי Smile | :)
 
לירון
QuestionSelect DB Dropdown List WidthmemberMember 975255913 Jan '13 - 6:58 
Any chance of making the dropdown list where you select the database wider? The list window is so narrow that I can't tell which database I'm selecting (many of our databases start with the same 20 characters... I'm hoping not to have to rename them). Other that this one minor issue, the program has been great!
 
I'd consider looking into this myself except that I don't have a Windows development environment.
 
Thanks,
Greg
AnswerRe: Select DB Dropdown List Widthmemberliron.levi13 Jan '13 - 20:07 
No problem - I'll post the change in the next version.
 
Liron
GeneralRe: Select DB Dropdown List WidthmemberMember 975255914 Jan '13 - 16:02 
Thank you! This is wonderful!
QuestionGreat!!memberMember 358483425 Dec '12 - 20:06 
Thanks, you help me a lot
thanks again ^^
QuestionThanks.. Great.memberjutatip16 Dec '12 - 17:38 
Thanks.. Great.
QuestionForeign Key mismatch Problemsmembersekerg16 Dec '12 - 6:21 
Any foreign key problems reported ?
 
It seems when a primary/foreign key is used in multiple tables, the converter does not know who the ultimate parent it. Thus, many bogus triggers are created causing foreign key mismatch errors.
QuestionGreat!memberMember 770135722 Nov '12 - 22:02 
This is a great tool that actually works as specified.
 
I would love to see support for the Geom type. I used Barend(Member 2500293)temporarily fix and just scripted the rest in spatialite_gui-1.5.0.
 
Thanks very much Big Grin | :-D
AnswerRe: Great!memberliron.levi23 Nov '12 - 3:49 
Hi
 
The problem is that SQLITE does not have anything that matches the SQL Server geometry type. How did you do the conversion ?
 
Liron
Questionunsuppoerted file error while connecting ?memberoguzkaygun26 Oct '12 - 8:20 
Hello
I convert sql2008 database to sqlite.
Connection string is below.
"Data Source=c:\\Basic\\Basic.db;Version=3;";
I used Finisar.SQLite AdoNet. Csharp.
I got error that unsupported file then i used Version=2;
While version=2, i got error that SQL logic error. Engrypted database or missing database.
What can be problem ?
Thank you.
AnswerRe: unsuppoerted file error while connecting ?memberliron.levi26 Oct '12 - 21:52 
I suspect that your problem is that you are using the wrong SQLite library. Try to use the one provided by Robert Simpson - http://sqlite.phxsoftware.com/[^]
 
Liron
QuestionThank you!!memberRablinz1 Oct '12 - 0:46 
Very nice work. This saved me countless hours, thank you.
I modified it to work in my WPF application.
 
I've now got to do SQLite to SQLServer... if any has already done it, please let me know!
BugSmall DDL error in Column Names?memberTomtommit29 Sep '12 - 5:35 
Hi there, first of all: thanks for a great tool! It works really well and saved me a ton of work converting a fairly complex database.
 
Some applications are rather particular in how they want the DDL formatted in SQLite, so I got errors when trying to import converted databases.
 
I resolved the issue by changing
SqlServerToSQLite.cs
Line 689            sb.Append("\t\"" + col.ColumnName + "\"\t\t");
to
Line 689            sb.Append("\t[" + col.ColumnName + "]\t");
and now everything gets imported fine.
 
I also formatted the syntax of Data Types to all caps (e.g. 'INTEGER' instead of 'integer'). Although both are valid SQL syntax, most tools use caps for non-values.
 
Maybe this is of some use.
Again: many thanks for this excellent tool! Big Grin | :-D
 
Kind regards,
T
GeneralRe: Small DDL error in Column Names?memberliron.levi29 Sep '12 - 7:14 
Thanks for the bug tip. I'll incorporate this fix in the next version (whenever that may be .. Smile | :) )
 
I'm happy it helped you.
Liron
QuestionWhat to put in "SQL server address" ?memberAlaeddine Ghribi27 Sep '12 - 12:10 
I installed wamp server and had on it an sqlserver database, i want to know what to put on "SQL server address" field to detect the available DB and convert them?
Thank you.
AnswerRe: What to put in "SQL server address" ?memberliron.levi28 Sep '12 - 4:13 
Hi
 
You should put the path to your SQL Server instance.
This is composed from the machine name (or simply '.' or (local) for local machine) and the instance name.
 
For example:
 
1. Suppose you have an sql express instance on your local machine: (local)/sqlexpress
2. Suppose you have a SQL server instance named 'DStore' on a machine named 'norway': norway/DStore
 
If you are not sure - you can simply check SQL Server connection string and look at the 'Data Source' field.
 
HTH
Liron
GeneralRe: What to put in "SQL server address" ?memberAlaa Gh28 Sep '12 - 5:18 
I made my_machine_name/databse_name without success. I really did not understand your examples. If i have a "users" database in my wamp server/phpmyadmin, i don't know what to put...
GeneralRe: What to put in "SQL server address" ?memberliron.levi28 Sep '12 - 6:09 
Maybe I mislead you - you need to use '\' instead of '/'
For example - use (local)\sqlexpress instead of (local)/sqlexpress ...
 
Liron
GeneralRe: What to put in "SQL server address" ?memberAlaa Gh28 Sep '12 - 6:12 
If i put (local)\users (with users the name of the database), is that true ? Sigh | :sigh:
GeneralRe: What to put in "SQL server address" ?memberliron.levi28 Sep '12 - 8:42 
No, You have to put the SQL server instance name. User names will not work here..
BTW - are you sure you are trying to connect to a SQL Server DB instance ? Maybe you are confusing the whole thing with MySQL (for which the converter will not work) ?
 
Liron

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 14 Jan 2013
Article Copyright 2008 by liron.levi
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid