Is it possible to embedded a Sqlite database into an excel 2007 file (zip archive)

帅比萌擦擦* 提交于 2019-12-03 18:10:28

问题


I'm working on an excel application that requires a database back end. My preference is to use SQLite 3 and to make this as seamless and portable as possible for the end user.

Recently I have learned that an Excel 2007 file is simply a zip archive with a xlsm extension. My question is this, can I store my back-end SQLite 3 database in the Zip archive and use ODBC to interact with the database. If so, can anyone point me to some background information, articles, guidance on achieving this objective. Are there any downsides to this approach or a better alternative I should know about.

Thanks for your input.


回答1:


Some notes. So far, no one has complained that the file does not open. Note that the Excel file is saved before the ADO code is run.

Very hidden:

ThisWorkbook.Worksheets("Courses").Visible = xlVeryHidden
ThisWorkbook.Worksheets("System").Visible = xlVeryHidden

A snippet of code:

    Const gCN = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="

<...>

Set rs = CreateObject("ADODB.Recordset")
Set cn = CreateObject("ADODB.Connection")
Set fs = CreateObject("Scripting.FileSystemObject")

scn = gCN & ThisWorkbook.FullName _
& ";Extended Properties=""Excel 8.0;HDR=Yes;"";"

cn.Open scn

''If they do not have an ID, they do not exist.

sSQL = "SELECT ID,FirstName,LastName, " _
& "CourseName,AdditionalText,Format(ExpiryDate,'dd/mm/yyyy') As ExpiryDate " _
& "FROM [Applicants$] WHERE DateCancelled Is Null AND ID Is Not Null " _
& "AND (FirstName Is Null OR LastName Is Null Or CourseName Is Null " _
& "Or ExpiryDate Is Null) " & sWhere

rs.Open sSQL, cn

References:

Excel ADO
Connection strings

Most of the methods available to Jet can be used with Excel

Fundamental Microsoft Jet SQL for Access 2000
Intermediate Microsoft Jet SQL for Access 2000
Advanced Microsoft Jet SQL for Access 2000

Edit re Comments

I did not find the leak particularly bad, but I did not run many iterations, and this is quite a good machine.

The code below uses DAO, which does not cause a memory leak.

'Reference: Microsoft Office 12.0 Access Database Engine Object Library
Dim ws As DAO.Workspace
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim sDb As String
Dim sSQL As String

sDb = ActiveWorkbook.FullName

Set ws = DBEngine.Workspaces(0)
Set db = ws.OpenDatabase(sDb, False, True, "Excel 8.0;HDR=Yes;")

sSQL = "SELECT * FROM [Sheet1$];"
Set rs = db.OpenRecordset(sSQL)

Do While Not rs.EOF
    For i = 0 To rs.Fields.Count - 1
        Debug.Print rs.Fields(i)
    Next

    rs.MoveNext
Loop

rs.Close
db.Close
ws.Close

 'Release objects from memory.
Set rs = Nothing
Set db = Nothing
Set ws = Nothing

Acknowledgement: http://www.ozgrid.com/forum/showthread.php?t=37398




回答2:


Here is an alternative.

1) At Open (EVENTs in VBA) unzip from Excel .xlsm, sqlite and dbFile.

2) Process what you .....

3) At save (EVENTs in VBA) the book an then attach the Excel .xlsm ,sqlite , dbFile in Excel .xlsm.




回答3:


Excel rewrites the file every time it is saved, so your own added file would be deleted. Furthermore, there is no SQLite driver that can access database files inside of zip archives.

You would have either to ship the database file alongside with the Excel file, or to recreate the database with a list of SQL commands when your application detects that the DB file is missing.
This still requires that some SQLite (ODBC) driver is installed on the user's machine.

The most seamless and portable way to store data in an Excel file is to store it in an Excel sheet, as mentioned by Remou. However, it's possible that the ADO driver will refuse to open the file when it's already open in Excel, so that you have to use Excel functions to access the data.




回答4:


Try using http://code.google.com/p/pyinex/ this embed the Python interpreter in Excel



来源:https://stackoverflow.com/questions/13068022/is-it-possible-to-embedded-a-sqlite-database-into-an-excel-2007-file-zip-archiv

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!