SQLite

From HashVB
Jump to: navigation, search
float
 This article is based on Visual Basic 6. Find other Visual Basic 6 articles.
 This article is currently work in progress. Please come back later.

SQLite is a SQL database engine similar to Microsoft MDB format and Jet. It allows you to have a very light weight Database engine embedded in an application without the overhead of ADO, ODBC, etc...

Unfortunately, the SQLite dll uses CDECL callign convention so can't be used directly by VB.

There is however a couple of tricks you can do to use an SQLite in VB without relying on 3rd party libraries.

This procedure is taken and modified from the "Compiling SQLite with MS VC++ 5 for C newbies" section of SQLite HowToCompile page and requires Visual C++:

  1. Create a new empty Win32 DLL project. When asked for a name, choose carefully as it will be the one used to generate the DLL
  2. Unzip the SQLite source code into this directory (Caution: There are two versions of the source code. One is the most generic, ie. not meant for a particular OS, while one source package has been massaged, ready to be compiled by MS VC++. You need to use the latter)
  3. Add all the newly unzipped .c files to the project except tclsqlite.c
  4. Set the project to build a release version under the Build, Set Active Configuration menu
  5. To add the VB wrapper, download Steve O'Hara's free VB wrapper from the Files section of the SQLite Yahoo group and add its source file pssql.c and its export file pssql.def to your project. Be sure to edit the DEF file so that the LIBRARY line matches the name of your DLL (or you'll get a warning)
  6. If you wish to add version information so you get a Version tab when right-clicking on the DLL in Windows Explorer, add a new Resource Script. A clear-text res.rc is added to the project that you can edit to include version information. Note that this version info embedded by VC++ is independent from the version info returned by sqlite_libversion(), ie. don't worry about the fact that the former is a four-digit number, while the latter uses three.
  7. Rebuild the full project. You'll see plenty of warnings, but if all goes well, you should now have a DLL in a Release/ subdirectory in your project directory.

Note that pssql.c assumes that the SQLite source code is located in a sqlite/ subdirectory, so either create a subdirectory to host the SQLite source files, or edit the #include entry to use the correct path.

To check that this DLL works fine, create a new project in VB, add the following code in a form, and hit F5:

Private Declare Function PSVBUTLS_VersionDB Lib "mysqlite.dll" () As String

Private Sub Form_Load()
  Me.Caption = "SQLite version " & PSVBUTLS_VersionDB$
End Sub