Recent

Author Topic: SQLite, code only, no TSQLite3Connection  (Read 3161 times)

jbmckim

  • Full Member
  • ***
  • Posts: 144
SQLite, code only, no TSQLite3Connection
« on: February 03, 2018, 01:55:28 am »
I like the DB connectors and I'll use the SQLite connector if need be but I was wondering if there was a "code only" solution.  I'm starting what will likely be a rather large project and I'd like to have my DB access all run through one unit.  This unit will be a worker bee and won't have a UI.  I've looked and maybe my Google skills aren't that sharp but everything I find uses the connectors dropped on a UI.

Thanks.

valdir.marcos

  • Hero Member
  • *****
  • Posts: 1106
Re: SQLite, code only, no TSQLite3Connection
« Reply #1 on: February 03, 2018, 03:14:44 am »
Take a look here:
[Solved]How can I create TSQLite3 components in a unit at run time.
http://forum.lazarus.freepascal.org/index.php/topic,30398.msg193409.html#msg193409

GAN

  • Sr. Member
  • ****
  • Posts: 370
Re: SQLite, code only, no TSQLite3Connection
« Reply #2 on: February 03, 2018, 03:27:18 am »
I use this unit in my projects. I use ZeosLib but it will serve as a guide.

Code: Pascal  [Select][+][-]
  1. unit dbfunctions;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.     Classes, SysUtils, db, ZConnection, ZDataset, FileUtil
  9.     ;
  10.  
  11. function DB_Exists (sDBase, sTable, sField, sValue: String) : Integer;
  12.  
  13. implementation
  14.  
  15.  
  16. function DB_Exists (sDBase, sTable, sField, sValue: String) : Integer;    //-1 Database does not exist
  17. var                                                                       // 0 Database not Found
  18.   ZConn:TZConnection;                                                     // >0 Number of records found
  19.   ZQry:TZQuery;
  20.   DSource:TDataSource;
  21.   iRet:Integer=0;
  22. begin
  23.   if not(FileExists(sDBase)) then Exit(-1);
  24.   ZConn:=TZConnection.Create(nil);
  25.   ZQry:=TZQuery.Create(nil);
  26.   DSource:=TDataSource.Create(nil);
  27.   ZConn.Protocol:='sqlite-3';
  28.   ZConn.Database:=sDBase;
  29.   ZConn.Connect;
  30.   ZQry.Connection:=ZConn;
  31.   DSource.DataSet:=ZQry;
  32.   DSource.Enabled:=True;
  33.   ZQry.SQL.Text:='SELECT '+sField+' FROM '+sTable+' WHERE '+sField+'='+QuotedStr(sValue)+' ;';
  34.   ZQry.Open;
  35.   iRet:=ZQry.RecordCount;
  36.   ZQry.Close;
  37.   DSource.Enabled:=False;
  38.   ZConn.Disconnect;
  39.   FreeAndNil(ZConn);
  40.   FreeAndNil(ZQry);
  41.   FreeAndNil(DSource);
  42.   DB_Exists:=iRet;
  43. end;
  44.  
  45. end.
Lazarus 2.0.8 FPC 3.0.4 Linux Mint Mate 19.3
Zeos 7̶.̶2̶.̶6̶ 7.1.3a-stable - Sqlite 3.32.3 - LazReport

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: SQLite, code only, no TSQLite3Connection
« Reply #3 on: February 03, 2018, 11:09:34 am »
Take a look here:
[Solved]How can I create TSQLite3 components in a unit at run time.
http://forum.lazarus.freepascal.org/index.php/topic,30398.msg193409.html#msg193409

Indeed. Actually I never use the visual component, I always do it like that.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

jbmckim

  • Full Member
  • ***
  • Posts: 144
Re: SQLite, code only, no TSQLite3Connection [SOLVED]
« Reply #4 on: February 03, 2018, 10:36:46 pm »
Thanks all,

Just to put things (mostly) all in one place:

You do need some things in "uses"

Code: Pascal  [Select][+][-]
  1. uses
  2.   ..., db, sqldb, sqlite3conn,...;

Then this - I put this in the global vars for the unit:

Code: Pascal  [Select][+][-]
  1. var
  2.   DBconnection: TSQLite3Connection;  

Then this:

Code: Pascal  [Select][+][-]
  1. DBconnection := TSQLite3Connection.Create(nil);  

Mostly putting this here so I can google it later to remind myself :o

jbmckim

  • Full Member
  • ***
  • Posts: 144
Re: SQLite, code only, no TSQLite3Connection [SOLVED]
« Reply #5 on: February 03, 2018, 10:43:04 pm »
And here's a good example of how to use after things are defined:

http://wiki.freepascal.org/TSQLite3Connection

 

TinyPortal © 2005-2018