#1 2017-09-28 14:37:59

JD
Member
Registered: 2015-08-20
Posts: 101

Using TSQLHttpServer with an in memory database

Hello,

I'm trying to make an in-memory SQLite3 database accessible via HTTP but it doesn't seem to be working.

I use the code below to create the in-memory database when I undefine SQLITE_DB_ON_DISK

  {$IFDEF SQLITE_DB_ON_DISK}
  DBServer    := TSQLRestServerDB.Create(Model, DBNAME, {$IFDEF AUTHENTICATION}True{$ELSE}False{$ENDIF});
  {$ELSE}
  DBServer    := TSQLRestServerDB.Create(Model, ':memory:', {$IFDEF AUTHENTICATION}True{$ELSE}False{$ENDIF});
  {$ENDIF}
  DBServer.CreateMissingTables;

  //
  // Create the HTTP Server
  // Register URI first otherwise error is thrown
  {$IFDEF SQLITE_DB_ON_DISK}
  Server  := TSQLHttpServer.Create(DEFAULT_HTTP_PORT, [DBServer], '+', HTTP_DEFAULT_MODE);
  {$ELSE}
  Server  := TSQLHttpServer.Create(DEFAULT_HTTP_PORT, [], '+', HTTP_DEFAULT_MODE);
  {$ENDIF}
  Server.AccessControlAllowOrigin := '*'; // allow cross-site AJAX queries

if I use [':memory:'] with the HTTP server like this,

Server  := TSQLHttpServer.Create(DEFAULT_HTTP_PORT, [':memory:'], '+', HTTP_DEFAULT_MODE)

the compiler says there is no overloaded version of 'Create' that can be called with these arguments.

What do I need to correct?

Thanks a lot.

Last edited by JD (2017-09-28 14:39:12)

Offline

#2 2017-09-28 17:54:34

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,266
Website

Re: Using TSQLHttpServer with an in memory database

Once the DBServer is defined properly, use it.

So you should just run 

Server  := TSQLHttpServer.Create(DEFAULT_HTTP_PORT, [DBServer], '+', HTTP_DEFAULT_MODE);

Offline

#3 2017-09-29 15:34:18

JD
Member
Registered: 2015-08-20
Posts: 101

Re: Using TSQLHttpServer with an in memory database

Thanks a lot ab. It worked!!!

However, I assumed that the data created when the model was created will be stored in memory. I had created a table Genre with the following initial data in the model

    // Add default record for the genre table
    if DBClient.TableRowCount(TSQLGenre) = 0 then
    begin
      Genre := TSQLGenre.Create;
      //
      try
        Genre.Description := UTF8ToString('Feminin');
        Genre.CreationBy  := TSQLUtilisateur(1);
        DBClient.Add(TSQLRecord(Genre), True);
        Genre.Description := UTF8ToString('Masculin');
        Genre.CreationBy  := TSQLUtilisateur(1);
        DBClient.Add(TSQLRecord(Genre), True);
      finally
        Genre.Free;
      end;
    end;    // if DBClient.TableRowCount(TSQLGenre) = 0

When I save the DB on disk and then access the table from the browser, I get the JSON result [{"ID":1},{"ID":2}], which is correct BUT when the DB is in memory, browser access returns the JSON result {"fieldCount":1,"values":["ID"],"rowCount":0} which is not correct! What am I doing wrong while accessing my in-memory DB?

Thanks a lot,

JD

Offline

Board footer

Powered by FluxBB