Creating a SQL Server Linked Server to SQLite to Import Data

By:   |   Comments (22)   |   Related: > Linked Servers


Problem

You have data in SQLite databases on mobile devices you would like to import into SQL Server.  In this tip we walk through the steps on how to import this data into SQL Server.

Solution

This describes a simple method of importing data from a SQLite database into SQL Server using a linked server. You can find other methods of course, so see the links below in "Next Steps" for additional options.

Overview

These are the steps we will follow:

  1. Download an ODBC driver for SQLite
  2.  Install the driver
  3.  Create a System DSN for the database
  4.  Create a linked server in SQL Server
  5.  Select the data from the source and insert it into a SQL Server database table

1. Download an ODBC driver for SQLite

Go to this SQLite ODBC driver source page.

Configuring the correct driver is sometimes the most difficult part because of that I suggest downloading both the 32 and 64 bit drivers.

2. Install the driver

Run either the 32 bit or 64 bit driver exe file. Choose the one that suits the operating system you are using.

Download an ODBC driver for SQLite

Configuring the correct driver is sometimes the most difficult part

Install the driver

Run either the 32 bit or 64 bit driver exe file.

Choose the one that suits the operating system you are using.

3. Create a System DSN for the database

Click Start Run and type odbcad32 and press return for the 64 bit administrator.

Create a System DSN for the database

Click Start Run and type C:\Windows\SysWOW64\odbcad32.exe and press return for the 32 bit administrator.

Click Start Run and type C:\Windows\SysWOW64\odbcad32.exe and press return for the 32 bit administrator.

Click on the System DSN tab.

Click on the System DSN tab.

Click add.

Select the appropriate driver.

Select the appropriate driver. If you do not know which one to use then try them in turn.

f you do not know which one to use then try them in turn.

Enter your SQLite database path. Note that some of the options are documented at the driver site. I suggest leaving them as they are initially.

Enter your SQLite database path.

Notice the 32 bit driver is only editable from a 32 bit administrator and the 64 bit driver is only editable from the 64 bit administrator.

Notice the 32 bit driver is only editable from a 32 bit administrator and the 64 bit driver is only editable from the 64 bit administrator.

Notice the remove and configure buttons greyed out.

Notice the remove and configure buttons greyed out.

4. Create a linked server in SQL Server

If you are new to linked servers then you may wish to check these Linked Server tips at MSSQLTips first.

I suggest you simply use this T-SQL to create the linked server to your SQLite database.

There are no login accounts or any security context with this linked server.

USE [master]
GO
EXEC sp_addlinkedserver 
   @server = 'Mobile_Phone_DB_64', -- the name you give the server in SSMS 
   @srvproduct = '', -- Can be blank but not NULL
   @provider = 'MSDASQL', 
   @datasrc = 'Mobile_Phone_DB_64' -- the name of the system dsn connection you created
GO

5. Select the data from the source and insert it into a SQL Server database table.

Now click on the linked server stem and expand it all the way to the tables.

Then simply query the tables.

I suggest this SQLite browser if you want to view the tables or export the data as SQL.

Select *
from openquery(Mobile_Phone_DB_64 , 'select * from db_notes')
GO

You can create a table in your destination SQL Server with this sort of TSQL:

Select * into SQLite_Data -- This creates a table
from openquery(Mobile_Phone_DB_64 , 'select * from db_notes')
GO

Then modify the data types in your destination SQL Server database table using alter commands.

Check these suggestions by Michelle Gutzait or you may decide to not even import the data.

Data Types

Data types in SQLite are found at SQLite.org.

Next Steps


sql server categories

sql server webinars

subscribe to mssqltips

sql server tutorials

sql server white papers

next tip



About the author
MSSQLTips author Graham Okely Graham Okely is a SQL Server DBA and has been working with database systems since 1984 and has been specializing in SQL Server since 2007.

This author pledges the content of this article is based on professional experience and not AI generated.

View all my tips



Comments For This Article




Wednesday, March 2, 2022 - 12:57:36 AM - pcross Back To Top (89843)
Thanks very much Graham.

I tried loading my triggers one at a time without success, but it turns out that other simpler triggers work just fine.

The problem appears to be that SQLite v3.33.0 supports UPDATE-FROM, but, when used in a trigger, the linked server fails.

That is, when I rewrote the UPDATE-FROM queries in the triggers into the "WHERE EXISTS" format ("correlated query"), the linked server worked.

https://sqlite.org/lang_update.html#upfrom
"The UPDATE-FROM idea is an extension to SQL that allows an UPDATE statement to be driven by other tables in the database. ... UPDATE-FROM is supported beginning in SQLite version 3.33.0 (2020-08-14)."

https://sqlite.org/forum/forumpost/fbad1e84e1?raw
For interest, shows the difference between UPDATE-FROM and WHERE EXISTS:

```
update t1
set zipcode = t2.zipcode
from t2
where t2.code_stat = t1.code_stat
;
```

or as a correlated query:

```
update t1
set zipcode = (
select zipcode
from t2
where t2.code_stat = t1.code_stat
)
where exists (
select *
from t2
where t2.code_stat = t1.code_stat
)
;
```
The fix may come with an updated driver one day.

Cheers,

pcross

Thursday, February 24, 2022 - 7:29:30 AM - Graham Okely Back To Top (89829)
Hello pcross
Sorry I am not familiar with SQLite triggers.
If you have to use triggers why not add only one trigger at a time and test it.
All the best.
Regards
Graham Okey

Tuesday, February 22, 2022 - 7:43:47 PM - pcross Back To Top (89820)
Thanks, this is still a very helpful guide and the comments help too.

In SQL Sever my Test connection: succeeded; but "An error occurred while preparing the query"
I found the only way to return data was to drop all triggers from the SQLite database.

Are triggers generally not allowed when used with a linked server or should I go looking for problems with these particular update triggers?

Friday, October 29, 2021 - 10:55:03 AM - Greg Robidoux Back To Top (89384)
Hi Zbynek,

it looks like the issue is with a BLOB column. Not sure if the other messages below will help you.

-Greg

Friday, October 29, 2021 - 9:01:38 AM - Zbynek Back To Top (89383)
Hi, does anyone managed to load text data FROM SQL server TO SQLite database? I don't have any problem with loading floats/integers into SQLite database but when it comes to text data I always ran into this error "Das abfragebasierte Einfügen oder Aktualisieren von BLOB-Werten wird nicht unterstützt.". I've tried converting the input data to ntext, text, nvarchar, varchar etc. but without any success.



Wednesday, May 2, 2018 - 2:25:34 PM - Sriram Back To Top (75841)

I was able to fix the BLOB issue in SQlite. 

I changed the BLOB column to blob(2147483647) in SQLite DB.

I created SSIS packages using the ODBC driver and this allowed for no truncation of the data while transferring between SQLite and SQL server.

P.S.  I dropped the idea of Linked Server and went with SSIS

Thanks.


Wednesday, April 25, 2018 - 8:57:47 AM - Sriram Back To Top (75780)

 

 Hello Graham,

Thanks for your reply.

i just did a 

Select *

from openquery(mydsn, 'select * from db_blobtable')
GO

i have a blob column in sqlite and trying to view data in ssms.

any help is appreciated.Thanks


Tuesday, April 24, 2018 - 11:47:08 PM - Graham Okely Back To Top (75777)

Hello Srira

If I am reading your error correctly the error indicates the design of your destination column is too small.

You are trying to put 1930 characters into 255 characters of space. Try redesigning your destination table.

Regards

Graham


Saturday, April 21, 2018 - 12:09:57 AM - Sriram Back To Top (75745)

 

 Thanks for sharing this. It was useful. I used SSIS to transfer data between ODBC and SQL Server. Now i am having issues with transferring BLOB data from SQLite to SQL server. Do you have any thoughts on this?

 

I get error:

'SQLite3 Datasource' returned data that does not match expected data length for column '[MSDASQL].BLOBCOLUMN'. The (maximum) expected data length is 255, while the returned data length is 1930.

 

Any help is appreciated!

 


Tuesday, March 6, 2018 - 12:18:46 AM - Graham Okely Back To Top (75353)

Hello Anggun Perpatih

Please go through the steps once more and keep an eye out for ANY error before that error you have posted here.

I think you are failing at step 3.

Regards

Graham


Wednesday, February 21, 2018 - 10:45:26 PM - Anggun Perpatih Back To Top (75271)

 

 Hello sir,

i follow the step to create link server (Creating a SQL Server Linked Server to SQLite to Import Data) but i got this error

OLE DB provider "MSDASQL" for linked server "chinok" returned message "[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified".

Msg 7303, Level 16, State 1, Line 1

Cannot initialize the data source object of OLE DB provider "MSDASQL" for linked server "chinok".

 

plz help me


Thursday, December 8, 2016 - 7:34:41 AM - PHALGUNI RATHOD Back To Top (44923)

 Hello Sir,

I'm new to android and sqlite both and have some basic knowledge about mySQL. 

I'm facing a problem and couldn't find the appropriate answer anywhere.

I'm building a patient feedback system app, to save all the data I'm using sqlite3 but I read that it stores locally on our phone and not in a cloud or some server. 

My requirement is that, it must store the data/information in the NGO's (for whom I'm making this) IT System.

How can I do that?

I have searched a lot but didn't get anything! Please Help!!

Thanks in advance

Phalguni Rathod. ([email protected])


Friday, July 8, 2016 - 7:32:16 AM - dason Back To Top (41833)

 Good idea! 

The sqlite3 db file associated with this System DSN can not be deleted at once after you execute the sql "Select * from openquery(Mobile_Phone_DB_64 , 'select * from db_notes')". I must wait a few minutes to delete the file. Why? Can you help me? Thank you very much!

 

 


Thursday, April 7, 2016 - 11:01:47 PM - Graham Okely Back To Top (41160)

Hello Joe

Try simplifying.

Can you simplify your query? Can you use one select statement over one table and still show the error?

Regards

Graham


Thursday, March 24, 2016 - 2:55:57 PM - Joe Back To Top (41051)

When i query my sqllite data, specficlly an integer I get the following, 2147483647 instead of the correct answer.

I have googled the answer and am aware of the 32bit / 64bit conflict but can not find the answer.

Joe

Select

dv.id, dv.Name AS WSID, na.[ip_address] AS IP, dv.operating_system AS OS, dv.version As OSVer, dv.bios_version AS BIOS ,dv.bios_date AS BiosDate,

dv.manufacturer As Mfg, dv.model, dv.serial_number AS SN, Substring(dv.[current_user],9,8) As EmpID,

dv.processor_type,substring(pd.size,1,3)+' GB' As size

From 

Openquery(Spiceworkslink, 'Select * From Devices') As dv

Inner

Join Openquery(Spiceworkslink, 'Select * From [physical_disks]') As pd

On

dv.id = pd.computer_id Join Openquery(Spiceworkslink, 'Select * From [Network_Adapters]') As na

On

dv.id = na.computer_id

Where

Substring(dv.name,1,2) = 'WS' And pd.name = '0' -- and dv.name = 'ws3141'

Order

By dv.name


Sunday, November 15, 2015 - 10:41:25 PM - Graham Okely Back To Top (39075)

I have only extracted data from a SQLite database. I have not found any working example of inserting data into a SQLite database via SQL Server Management Studio.

I have researched this and here are some things I found:

A SQL Server SQLite sync tool: http://sqlite-sync.com/

A conversion tool that has some good comments: http://www.codeproject.com/Articles/26932/Convert-SQL-Server-DB-to-SQLite-DB

A thread on the topic: http://www.sqlservercentral.com/Forums/Topic866972-149-1.aspx

Note the performance difference when pushing and pulling data: http://sqlblog.com/blogs/linchi_shea/archive/2010/12/01/linked-server-and-performance-impact-direction-matters.aspx So pulling data from a SQLite database may be better for performance but the question remains how that can be done using SSMS.

Regards

Graham Okely


Friday, September 11, 2015 - 10:17:14 AM - Greg Back To Top (38651)

 

I'd be grateful if anyone could post some tips about inserting data (from SQL Server) back into SQLite. Especially into TEXT columns.


Saturday, November 1, 2014 - 12:49:05 AM - Graham Okely Back To Top (35144)

Hello Vishal Nayan

Connect your mobile phone to your windows computer. You may need to enable the connection from the phone. Each phone has its own process for doing that.

Note that an iPhone will not let you get to the SQLite databases without using some extra software of some kind. I do not recommend hacking your iPhone if you have one.

Once connected properly your phone should then appear as a drive letter on your windows computer. If it does then browse that drive with windows explorere to find the path to the SQLite database. Then enter that path and database name into step three.

All the  best

Regards Graham Okely


Tuesday, October 28, 2014 - 11:24:45 AM - Vishal Nayan Back To Top (35098)

Hi ,

 

While entering path of SQLite database, in example above it shows that database is located in C drive.So how to give the path if the database is in mobile.

 

Thanks in Anticipation.


Friday, May 16, 2014 - 1:59:50 PM - DaveBoltman Back To Top (30808)

Excellent - thanks for taking the time to make this. Helped a great deal...


Friday, May 9, 2014 - 7:30:12 PM - Adel Back To Top (30711)

Excellent guide! Thank you!


Wednesday, December 4, 2013 - 10:00:21 AM - Srinivas Back To Top (27685)

Awesome, Thanks

 

 

 















get free sql tips
agree to terms