简体   繁体   中英

How do I access SQLite from VBA?

I have an Excel workbook that has some adodb code that queries a local access database. I want to do the same for SQLite as I believe that will provide better performance. How do I do that? Can I connect to an SQLite file using adodb or odbc?

I need something simple that I can deploy so that if i can minimize unnecessary configuration and installation maybe peoople that will be using the excel won't have admin rights on the pc so they can't install software.

Facing the same question, I made a lightweight library to give direct access to SQLite3 from Excel VBA. The end result is a much simpler solution, with no intervening ODBC or OleDb/ADO layer, and the performance reflects the SQLite database performance and not that of the opaque wrapper. It's also nice because you need no registration of a COM component in the registry, you just copy two .dlls with your workbook and add a .bas module into your project.

A disadvantage of this approach is that the API is not the standard DAO or ADO interface, so you might need to make some wrappers, or convert some of your code to make it work. That also means you need some familiarity with the SQLite API to use it, but the SQLite documentation is very clear.

I have put an early version of the project on CodePlex: SQLite for Excel provides a high-performance path to the SQLite3 API functions, preserving the semantics of the SQLite3 library calls and allowing access to the distributed SQLite3.dll without recompilation.

Any feedback would be much appreciated.

Update: The SQLite for Excel project now lives on GitHub .

How to access sqlite from vba macro from here (original article in chinese) :

Software environment:

1) Win7 (32 bit) 2) Excel2007 (with VBA function)

Steps:

1) First, http://www.zsplat.pwp.blueyonder.co.uk/programming/sqlite-3.5.7-odbc-0.65.zip download and install the SQLite ODBC driver.

Note: If you use Win7, then you need to Adminitrator permissions, otherwise the installation fails. The simplest step is to use the Administrator login, and then install the SQLite ODBC driver.

2) Open the Excel VBA code editor window, the menu bar [tool] -> [reference], adding Microsoft ActiveX Data Objects 2.7, the purpose is to use VBA, and database connectivity.

3) Use the following code to connect SQLite database:

Dim conn As New ADODB.Connection 
Dim dbName As String 

'Define the connection string 

dbName = "Driver = {SQLite3 ODBC Driver}; Database = D: \ yourdbname.db" 

'Open the database 

ff.Open dbName 

ff.Execute "create table a (a, b, c);" 

'Other operations on and VBA to connect to other DB like Access the same. 


'Close connection 
ff.Close 

There're some DLLs that you can use to access a SQLITE Database from Visual Basic using adodb. I think here is the info that you're looking for. I never tried it, but the link can be usefull for you.

Good Luck.

我使用过 Datenhaus 的dhSQLite ,效果很好。

google sqliteforexcel and download it from github

govert responded to this already (the creator), the easy solution to me, unless you can't.. don't use Access. Transfer the data to sqlite db files. You can even use ADO to a server or DAO and read the rs directly in to a sqlite table

It requires no driver or install. You can have your workbook download the 2 .dll's from a shared location on file open(check if they don't already exist on their file system, on say c:\\temp), then user is running sqlite. I suspect, if you're so inclined to ask this, you'd want to write a function to loop calls to the appropriate sqlite functions(explained in example docs) and read returned data in to a vba array. Then you can do whatever you want with it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM