Lazarus

Programming => Databases => Topic started by: JanRoza on December 10, 2007, 03:12:01 am

Title: Switching indexes in SQLite
Post by: JanRoza on December 10, 2007, 03:12:01 am
One more SQLite question.
In my application I have a dbgrid based on a SQLite table and a button which should change sorting order when clicked.
First click should sort grid on Name ascending and when clicked again it should change sorting order on the same field but now descending.
I tried following code but it looks as if SQLite doesn't use the ASC and DESC part of the query and always sorts on Naam ascending.

Code: [Select]

  if SortNaam = 'Ascending'
  then begin
       SortNaam := 'Descending';
       Sqlite3dsAdressen.ExecSQL('SELECT * FROM Adressen ORDER BY Naam DESC');
       Sqlite3dsAdressen.RefetchData;
       dbgrid1.Refresh;
  end
  else begin
       SortNaam := 'Ascending';
       Sqlite3dsAdressen.ExecSQL('SELECT * FROM Adressen ORDER BY Naam ASC');
       Sqlite3dsAdressen.RefetchData;
       dbgrid1.Refresh;
  end;


If that is true then how can I change the sorting order of the grid?

I also created two indexes in the SQLite database (Name-Asc and Name_Desc) to try a different route but I still haven't found a way to select a SQLite index in code.

Any suggestions are more than welcome!
Title: RE: Switching indexes in SQLite
Post by: LuizAmérico on December 15, 2007, 11:41:02 am
Modify to this:

if SortNaam = 'Ascending'
  then begin
       SortNaam := 'Descending';
       Sqlite3dsAdressen.SQL := 'SELECT * FROM Adressen ORDER BY Naam DESC';
       Sqlite3dsAdressen.RefetchData;
  end
  else begin
       SortNaam := 'Ascending';
       Sqlite3dsAdressen.SQL := 'SELECT * FROM Adressen ORDER BY Naam ASC';
       Sqlite3dsAdressen.RefetchData;
  end;
Title: Switching indexes in SQLite
Post by: JanRoza on December 15, 2007, 10:34:09 pm
Thank you Luiz,

That did the trick.
It's easy once you know it, but before you know it takes some sweating.

 :D
Jan
TinyPortal © 2005-2018