Recursive queries with sqlite

Discussion of open issues, suggestions and bugs regarding UniDAC (Universal Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
sandy771
Posts: 194
Joined: Tue 22 May 2007 13:57

Recursive queries with sqlite

Post by sandy771 » Wed 15 Oct 2014 14:41

Has anyone else had a problem using recusive queries using sqlite?

I emailed Devart a couple of days ago and got told that this is supported in the later version of sqlite - but when I replied (twice) witha demo project and explained that I was using the latest version 3.8.6 I have had no reply back from them.

I can only assume from their reply that these queries are supported but for some reason I get an error.

with recursive path as (select id, from_visit from visits where visits.id=27585 union all select visits.id, visits.from_visit from path join visits on (path.from_visit = visits.id) ) select * from path;

I get an error - "near "all": syntax error

The query works fine at the sqlite command line

Cheers

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Re: Recursive queries with sqlite

Post by AlexP » Thu 16 Oct 2014 05:09

Hello,

We cannot reproduce the problem. You query is executed with no errors. Please try running the following code and let us know the results.

Code: Select all

program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils, Uni, SQLiteUniProvider;

var
  UniConnection: TUniConnection;
  UniQuery: TUniQuery;
begin
  UniConnection := TUniConnection.Create(nil);
  try
    UniConnection.ConnectString := 'Provider Name=SQLite;Data Source=:memory:';
    UniConnection.Connect;
    WriteLn('SQLite version: ' +  UniConnection.ClientVersion); //3.8.5
    WriteLn('UniDAC Version: ' + UniDACVersion); //5.5.12
    UniConnection.ExecSQL('create table visits(id integer, from_visit integer)');
    UniConnection.ExecSQL('create table path(id integer, from_visit integer)');
    UniConnection.ExecSQL('insert into visits values(27585, 1)');
    UniConnection.ExecSQL('insert into path values(1, 1)');
    UniQuery := TUniQuery.Create(nil);
    try
      UniQuery.Connection := UniConnection;
      UniQuery.SQL.Text := 'with recursive path as (select id, from_visit from visits where visits.id=27585 union all select visits.id, visits.from_visit from path join visits on (path.from_visit = visits.id) ) select * from path';
      UniQuery.Open;
      WriteLn('Record Count: ' + IntToStr(UniQuery.RecordCount)); //1
    finally
      UniQuery.Free;
    end;
  finally
    UniConnection.Free;
    readln;
  end;
end.

sandy771
Posts: 194
Joined: Tue 22 May 2007 13:57

Re: Recursive queries with sqlite

Post by sandy771 » Thu 16 Oct 2014 12:16

Thanks Alex

I am away from my programming machine at the moment. Your code has reminded me that I downgraded to UniDac 5.2.7 (becuase of what seemed to be a conflict with a third party application) - would using this version affect a recursive query?

Thanks

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Re: Recursive queries with sqlite

Post by AlexP » Fri 17 Oct 2014 08:02

If you are working with the SQLite3.dll library, then this functionality doesn't depend on the UniDAC version (it is built in the library). This functionality is supported in the Direct mode since UniDAC 5.2.6.

luksmolders
Posts: 1
Joined: Fri 17 Oct 2014 13:27

Re: Recursive queries with sqlite

Post by luksmolders » Fri 17 Oct 2014 13:31

Hi,

I tried the example above and got following error:
EUniError error raised, with message : near "WITH" : syntax error.

SQLite version : 3.7.2
UniDAC Version : 5.5.12

I think the version of sqlite is the problem ?

(I'm using Delphi Starter Edition XE6).

Luk

sandy771
Posts: 194
Joined: Tue 22 May 2007 13:57

Re: Recursive queries with sqlite

Post by sandy771 » Fri 17 Oct 2014 20:52

Sorry about the delay replying - that worked with your example - but still fails in my test program :(

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Re: Recursive queries with sqlite

Post by AlexP » Mon 20 Oct 2014 08:52

>luksmolders
This functionality appeared in SQLite 3.8.3, You should update your library version.

>sandy771
Please send me your project (including the database file) to alexp*devart*com - and we will check this query on your sample.

sandy771
Posts: 194
Joined: Tue 22 May 2007 13:57

Re: Recursive queries with sqlite

Post by sandy771 » Mon 20 Oct 2014 08:54

Thanks Alex - I sent my program twice and as I said I am using 3.8.6

The immediate need for this has now gone though

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Re: Recursive queries with sqlite

Post by AlexP » Tue 21 Oct 2014 04:26

Unfortunately, we received no your letter, please send it once more to support*devart*com .

Post Reply