To use the dll you need to use the LoadLibrary windows function and define
pointers to each of the sqlite functions. Here is a short example to get you
started. For more on using DLLs try this MSDN link:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/dynamic_link_library_functions.asp


#include <cstdlib>
#include <iostream>
#include <windows.h>

using namespace std;

#define SQLITE_OK           0   /* Successful result */
#define SQLITE_ERROR        1   /* SQL error or missing database */
#define SQLITE_INTERNAL     2   /* NOT USED. Internal logic error in SQLite
*/
#define SQLITE_PERM         3   /* Access permission denied */
#define SQLITE_ABORT        4   /* Callback routine requested an abort */
#define SQLITE_BUSY         5   /* The database file is locked */
#define SQLITE_LOCKED       6   /* A table in the database is locked */
#define SQLITE_NOMEM        7   /* A malloc() failed */
#define SQLITE_READONLY     8   /* Attempt to write a readonly database */
#define SQLITE_INTERRUPT    9   /* Operation terminated by
sqlite3_interrupt()*/
#define SQLITE_IOERR       10   /* Some kind of disk I/O error occurred */
#define SQLITE_CORRUPT     11   /* The database disk image is malformed */
#define SQLITE_NOTFOUND    12   /* NOT USED. Table or record not found */
#define SQLITE_FULL        13   /* Insertion failed because database is full
*/
#define SQLITE_CANTOPEN    14   /* Unable to open the database file */
#define SQLITE_PROTOCOL    15   /* Database lock protocol error */
#define SQLITE_EMPTY       16   /* Database is empty */
#define SQLITE_SCHEMA      17   /* The database schema changed */
#define SQLITE_TOOBIG      18   /* NOT USED. Too much data for one row */
#define SQLITE_CONSTRAINT  19   /* Abort due to contraint violation */
#define SQLITE_MISMATCH    20   /* Data type mismatch */
#define SQLITE_MISUSE      21   /* Library used incorrectly */
#define SQLITE_NOLFS       22   /* Uses OS features not supported on host */
#define SQLITE_AUTH        23   /* Authorization denied */
#define SQLITE_FORMAT      24   /* Auxiliary database format error */
#define SQLITE_RANGE       25   /* 2nd parameter to sqlite3_bind out of
range */
#define SQLITE_NOTADB      26   /* File opened that is not a database file
*/
#define SQLITE_ROW         100  /* sqlite3_step() has another row ready */
#define SQLITE_DONE        101  /* sqlite3_step() has finished executing */


typedef struct sqlite3 sqlite3;
typedef int (*sqlite_open)(const char*, sqlite3**);
typedef int (*sqlite_close)(sqlite3*);
typedef int (*sqlite_exec)(sqlite3*,char*, int
(*callback)(void*,int,char**,char**),void *, char**);

string filename = "sqlite3.dll";

int main(int argc, char *argv[])
{

    HMODULE myDll;
    myDll = LoadLibrary(filename.c_str());
    if (myDll != NULL)
    {
              char *zErrMsg = 0;
              sqlite3 *db;
              cout << "Loaded " << filename << endl;
              sqlite_open sqlite3_open;
              sqlite_close sqlite3_close;
              sqlite_exec sqlite3_exec;
              sqlite3_open =
(sqlite_open)GetProcAddress(myDll,"sqlite3_open");
              sqlite3_close =
(sqlite_close)GetProcAddress(myDll,"sqlite3_close");
              sqlite3_exec =
(sqlite_exec)GetProcAddress(myDll,"sqlite3_exec");
              int i = sqlite3_open("test.db",&db);
              cout << "Open Status : " << i << endl;
              i = sqlite3_exec(db, "Create Table test(a int, b int)", NULL,
0, NULL);
              i = sqlite3_close(db);
              cout << "Close Status : " << i << endl;
              FreeLibrary(myDll);
    }
    else
    {
              cout << "Failed to Load " << filename << endl;
    }
    system("PAUSE");
    return EXIT_SUCCESS;
}




On Sun, Feb 24, 2008 at 8:48 PM, Fin Springs <[EMAIL PROTECTED]>
wrote:

>
> On Feb 23, 2008, at 9:15 PM, Sam Carleton scarleton-at-
> miltonstreet.com |sqlite| wrote:
>
> > How do I compile a C program to use the shared DLL rather then
> > statically link in SQLite?
> Good question; the DLL download only has the DEF file and the DLL, but
> you normally need an export LIB file to link against. You do an
> explicit LoadLibrary I suppose, and use GetProcAddress calls to get
> the function pointers.
>
> Alternatively, you could just use the amalgamation version: it's
> one .h and one .c, and you'll need the .h even if you do use the DLL
> (unless you want to extern everything). It will also make your
> debugging easier, since you'll be able to step into the sqlite code.
> _______________________________________________
> sqlite-users mailing list
> sqlite-users@sqlite.org
> http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
>
_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to