* * *

Author Topic: [Solved] SQLite | GetTableNames desnt work  (Read 886 times)

cpalx

  • Hero Member
  • *****
  • Posts: 560
[Solved] SQLite | GetTableNames desnt work
« on: February 14, 2018, 07:31:28 pm »
hello, i use
TSQLite3Connection
and i need The tebles names, so i use

TSQLite3Connection.GetTableNames(a Tstrings)

and i get a memory error

any idea?
« Last Edit: February 15, 2018, 03:34:53 am by cpalx »

molly

  • Hero Member
  • *****
  • Posts: 2345
Re: SQLite | GetTableNames desnt work
« Reply #1 on: February 14, 2018, 07:47:00 pm »
any idea?
without any code and exact error message ?, no idea.

In that case your guess is as good as mine....

Shot in the dark: you did not create your stringlist before passing it along to method GetTableNames...

cpalx

  • Hero Member
  • *****
  • Posts: 560
Re: SQLite | GetTableNames desnt work
« Reply #2 on: February 15, 2018, 03:06:43 am »
================
Code: Pascal  [Select]
  1.  SQLiteConn:= TSQLite3Connection.Create( nil );
  2.   Query:= TSQLQuery.Create( nil );
  3.   SQLiteTran:= TSQLTransaction.Create( nil );
  4.   SQLiteConn.Transaction:= SQLiteTran;
  5.   Query.Transaction:= SQLiteTran;
  6.   SQLiteTran.Action:= caCommitRetaining;
  7.   Query.DataBase:= SQLiteConn;
  8.   Error:= EmptyStr;
  9.  
  10.   if DataBaseName = DBName then
  11.      DataBaseName:= ExtractFilePath( ParamStr( 0 ) ) + DBName ;
  12.  
  13.   SQLiteConn.DatabaseName:= DataBaseName ;
  14.  
  15.   try
  16.      SQLiteConn.Connected:= True;
  17.      Self.Connected:= SQLiteConn.Connected;
  18.  
  19.   except on E: Exception do begin
  20.      Error:= E.ClassName + LineEnding + E.Message;
  21.      Self.Connected:= False;
  22.   end;
  23.   end;                          
  24.  
================


================
Code: Pascal  [Select]
  1. function TSQLiteCache.GetTableNames( var List: Tstrings; ShowSystemTables: Boolean = False ): Boolean;
  2. begin
  3.  
  4.   try
  5.    SQLiteConn.GetTableNames( List, ShowSystemTables );
  6.    result:= true
  7.   except on E: Exception do begin
  8.      Error:= E.ClassName + LineEnding + E.Message;
  9.      Result:= False;
  10.   end;
  11.   end;
  12. end;  
  13.  
================

and i call:
==============
Code: Pascal  [Select]
  1. var ListTableName: Tstrings;
  2. begin
  3.  
  4.   ListTableName:= TStrings.Create;
  5.    if not  Cache.GetTableNames( ListTableName ) then
  6.       ShowMessage( Cache.Error );  
  7.  
==============


And display error:
==============
EAbstractError
Abstract method called
==============

molly

  • Hero Member
  • *****
  • Posts: 2345
Re: SQLite | GetTableNames desnt work
« Reply #3 on: February 15, 2018, 03:17:09 am »
Code: Pascal  [Select]
  1. var ListTableName: Tstrings;
  2. begin
  3.  
  4.   ListTableName:= TStrings.Create;
  5.    if not  Cache.GetTableNames( ListTableName ) then
  6.       ShowMessage( Cache.Error );  
  7.  
==============


And display error:
==============
EAbstractError
Abstract method called
==============

Don't create directly from TStrings It is an abstract class. The warnings during compilation should have given you that hint.

Quote
... Warning: Constructing a class "TStrings" with abstract method "Get"
... Warning: Constructing a class "TStrings" with abstract method "GetCount"
... Warning: Constructing a class "TStrings" with abstract method "Clear"
... Warning: Constructing a class "TStrings" with abstract method "Delete"
... Warning: Constructing a class "TStrings" with abstract method "Insert"

use:
Code: Pascal  [Select]
  1. var ListTableName: TstringList;
  2. begin
  3.  
  4.   ListTableName:= TStringList.Create;
  5.    if not  Cache.GetTableNames( ListTableName ) then
  6.       ShowMessage( Cache.Error );  
  7.  

more information. TStrings:
Quote
TStrings implements an abstract class to manage an array of strings. It introduces methods to set and retrieve strings in the array, searching for a particular string, concatenating the strings and so on. It also allows an arbitrary object to be associated with each string.

It also introduces methods to manage a series of name=value settings, as found in many configuration files.

An instance of TStrings is never created directly, instead a descendant class such as TStringList should be created. This is because TStrings is an abstract class which does not implement all methods; TStrings also doesn't store any strings, this is the functionality introduced in descendants such as TStringList.
« Last Edit: February 15, 2018, 03:20:44 am by molly »

cpalx

  • Hero Member
  • *****
  • Posts: 560
Re: SQLite | GetTableNames desnt work
« Reply #4 on: February 15, 2018, 03:22:05 am »
Thanks Molly, problem solved

molly

  • Hero Member
  • *****
  • Posts: 2345
Re: SQLite | GetTableNames desnt work
« Reply #5 on: February 15, 2018, 03:37:32 am »
You're welcome cpalx.

Another approach is also possible:
Code: [Select]
var ListTableName: Tstrings;
begin
  ListTableName:= TStringList.Create;

  if not  Cache.GetTableNames( ListTableName ) then

  ShowMessage( Cache.Error );
end;

In which case you could keep the TStrings var parameter for TSQLiteCache.GetTableNames. I would personally not opt for it in your case though as using TStringlist as var parameter makes things more explicit. btw: classes are passed by reference by default, so no need for using var modifier.

cpalx

  • Hero Member
  • *****
  • Posts: 560
Re: [Solved] SQLite | GetTableNames desnt work
« Reply #6 on: February 15, 2018, 03:46:46 am »
Thanks for your advice, i changed the params by TStringList

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus