[yunqa.de] Re: TDISQLite3UniDirQuery record count [this does work]

  • From: Todd Cary <todd@xxxxxxxxxxxxxxxxxx>
  • To: yunqa@xxxxxxxxxxxxx
  • Date: Fri, 19 Mar 2010 16:21:18 -0700

Ralf -

On the following line, I get the error SQLite Error 21 - library routine called out of sequence.

  sqlite3_check(
    sqlite3_prepare_v2(DB, Pointer(SQL), Length(SQL), @Stmt, nil), DB);


function sqlite3_exists_table_column() works great!  Thanks!

Todd

On 3/19/2010 3:17 AM, Delphi Inspiration wrote:
At 23:06 18.03.2010, Todd Cary wrote:

I want to determine if a table exists, so I have this code:

...
      sCmd := 'SELECT name FROM sqlite_master ' +
              'WHERE type=''table'' AND name = ''' + sTableName + '''';
      SQLGenQry.SelectSQL := sCmd;
      SQLGenQry.Open;
      Result := (SQLGenQry.RecordCount = 1);
...

SQLGenQry is a TDISQLite3UniDirQuery and the RecordCount is always -1.  Am I 
making an error?
TDISQLite3UniDirQuery is a uni-directional TDataSet. It does not buffer 
multiple records in memory. Hence the RecordCount property is not available -- 
it always returns -1.

The following function implements a more efficient way to test if a table 
exists. It has virtually no overhead and uses the DISQLite3Api unit only:

function sqlite3_exists_table(
   const DB: sqlite3_ptr;
   const TableName: Utf8String;
   const DatabaseName: Utf8String = ''): Boolean;
var
   SQL: Utf8String;
   Stmt: sqlite3_stmt_ptr;
begin
   SQL := 'SELECT 1 FROM ' + sqlite3_database_master(DatabaseName) +
     ' WHERE type=''table'' AND name=' + DISQLite3Api.QuotedStr(TableName);
   sqlite3_check(
     sqlite3_prepare_v2(DB, Pointer(SQL), Length(SQL), @Stmt, nil), DB);
   Result := sqlite3_check(
     sqlite3_step(Stmt), DB) = SQLITE_ROW;
   sqlite3_check(
     sqlite3_finalize(Stmt), DB);
end;

Applications pass the database handle and the table name. The name of the 
(attached) database is optional and defaults to the main database. 
sqlite3_exists_table() returns True if the table exists in the database and 
False if it does not exist.

Ralf

_______________________________________________
Delphi Inspiration mailing list
yunqa@xxxxxxxxxxxxx
//www.freelists.org/list/yunqa





--
Ariste Software
Petaluma, CA 94952

http://www.toddcary.com/aristephotography/

_______________________________________________
Delphi Inspiration mailing list
yunqa@xxxxxxxxxxxxx
//www.freelists.org/list/yunqa



Other related posts: