SQLite3 + FPC. native! lazarus / 63-Database ---------------------------------------------------------------------- Seenkao · 2024-06-27 13:40 Всем привет! Есть ли примеры, где используется SQLite3 без лишних обёрток? Мне не нужен ни какой графический интерфейс, мне нужно работать с базой данных в консоли. ------------------------------- Google translate: Hi all! Are there any examples where SQLite3 is used without unnecessary wrappers? I don't need any GUI, I need to work with the database in the console. ---------------------------------------------------------------------- marcov · 2024-06-27 13:42 Read the source of the wrappers? ---------------------------------------------------------------------- TRon · 2024-06-27 13:59 And that is besides the fact that you can access SQL databases using their wrappers without an actual gui and from the console. ---------------------------------------------------------------------- Seenkao · 2024-06-27 14:06 Я пишу про примеры!  Я могу использовать сам SQLite3 в консоли. Я вижу пример на другом ЯП. Я хочу состыковать и понять как это сделано на Паскале. Я просто не хочу изобретать очередной велосипед: брать код из C/C++ и перекидывать его на Паскаль. Но с подобными ответами, видимо это и придётся делать.  Неужели ни кто этого не делал? ------------------------------- Google translate: I write about examples! I can use SQLite3 itself in the console. I see an example in another language. I want to connect and understand how this is done in Pascal. I just don’t want to reinvent another wheel: take code from C/C++ and transfer it to Pascal. But with such answers, apparently this is what will have to be done. Has no one ever done this? ---------------------------------------------------------------------- Thaddy · 2024-06-27 14:08 I don't know if he translated sqlite3 too, but a full translation of sqlite2 has been made by https://www.yunqa.de/delphi/ I mean, full translation, not headers. pure pascal code. but it is not free. ---------------------------------------------------------------------- TRon · 2024-06-27 14:16 Quote from: Seenkao on June 27, 2024, 04:06:12 pmI write about examples! Oh, yes... you are right and I missed that. Apologies. QuoteHas no one ever done this? I think I have an idea what those c examples look like (edit see Awkward's post below this/mine) and I wonder if you would want to punish yourself that way. Database components are non visual components so can be used as such. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program dbtest; {$MODE OBJFPC}{$H+} uses  classes, sysutils, dateutils, db, sqldb, sqlite3conn; var  DBConnection  : TSQLite3Connection;  DBTransaction : TSQLTransaction;  DBQ           : TSQLQuery;   {$IFDEF LCL}  // For Lazarus Change MainForm.YourLogMem to point to a Memo on your Form  LogMemo   : TMemo absolute MainForm.YourLogMemo;  {$ENDIF}   procedure WriteLog(S:String);inline;begin{$IFDEF LCL}LogMemo.Append{$ELSE}WriteLn{$ENDIF}(S);end;  procedure WriteLog;begin WriteLog('')end;  procedure WriteLog(S:String;const a:array of const);begin WriteLog(Format(S,a));end;  procedure CreateDB(ADataBaseName: string);begin  DBConnection := TSQLite3Connection.Create(nil);   DBConnection.DatabaseName := ADataBaseName;  DBTransaction := TSQLTransaction.Create(DBConnection);  DBTransAction.Database := DBConnection;   DBQ             := TSQLQuery.Create(DBConnection);  DBQ.Database    := DBConnection;  DBQ.Transaction := DBTransaction;end; procedure DestroyDB;begin  DBConnection.Free;end;  procedure test;var  TableName: string;  TableNames: TStringList;  n : integer;begin  WriteLog('DBConnection.DatabaseName = "%s"', [DBConnection.DataBaseName]);  TableNames := TStringList.Create;  DBConnection.GetTableNames(TableNames);  n := 1;  for TableName in TableNames do  begin    WriteLog('Tablename[%d] = %s', [n, TableName]);    inc(n);  end;   TableNames.Free;end; procedure FIllDB;var  n: integer;begin  DBConnection.ExecuteDirect('CREATE TABLE "data" (num INTEGER);');  DBQ.SQL.text := 'INSERT INTO "data" (num) values (:number)';  for n := 1 to 100 do  begin    DBQ.Params.ParamByName('number').AsInteger := n;    DBQ.ExecSQL;  end;  DBTransaction.Commit;end; procedure test2;var  numfield: TField;  x:integer;begin  writeln('select');  DBQ.SQL.Text := 'select * from data';  writeln('open');  DBQ.Open;  writeln('last');  DBQ.Last;  writeln('first');  DBQ.First;  numfield := DBQ.FieldByName('num');  while not DBQ.EOF do  begin    x := numfield.AsInteger;    writeln('x = ', x);    DBQ.Next;  end;end; procedure RunningMan;begin   WriteLog('begin');   try     CreateDB('test.s3db');     if not fileexists('test.s3db')       then FillDB;     writeln('test1');     test;     writeln('test2');     test2;   finally     DestroyDB;   end;   WriteLog('end');end; begin  RunningMan;end. ---------------------------------------------------------------------- Awkward · 2024-06-27 14:19 What about internet search and C API using? Like Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function GetTextValue(db:PSQLite3; const atable, afield, acond:string):string;var  lSQL:string;  vm:pointer;begin  result:='';  if db<>nil then  begin    lSQL:='SELECT '+afield+' FROM '+atable+' WHERE '+acond;    if sqlite3_prepare_v2(db, PAnsiChar(lSQL),-1, @vm, nil)=SQLITE_OK then    begin      if sqlite3_step(vm)=SQLITE_ROW then      begin        result:=sqlite3_column_text(vm,0);      end;      sqlite3_finalize(vm);    end;  end;end; ---------------------------------------------------------------------- Handoko · 2024-06-27 14:38 I have a SQLite demo written using Lazarus, without using TDBNavigator, TDBEdit, TDBMemo, etc. I am not sure but maybe it can be useful: https://forum.lazarus.freepascal.org/index.php/topic,65185.msg496461.h tml#msg496461 ---------------------------------------------------------------------- Seenkao · 2024-06-27 14:45 TRon, Благодарю! Это достаточно подробный пример! Awkward, мне приходится смотреть разные ЯП. Не критично как выглядит в них код, зачастую критично понимание кода. И смотря код с использованием LCL я не достаточно ясно понимаю как всё работает и мне приходится по крупинкам брать всё из разных примеров. Handoko, Благодарю! Гляну. Google translate: TRon, Thank you! This is quite a detailed example! Awkward, I have to watch different languages. It is not critical how the code looks in them; understanding the code is often critical. And looking at the code using LCL, I don’t clearly understand how everything works and I have to take everything bit by bit from different examples. Handoko, Thank you! I'll take a look. ---------------------------------------------------------------------- cdbc · 2024-06-27 21:38 Hi I dunno... But maybe this unit can be of help to you... It's been my SQLite3 workhorse for years and it needs to be refactored, ah well, that's for a rainy day  Regards Benny ---------------------------------------------------------------------- TRon · 2024-06-28 00:48 Quote from: Seenkao on June 27, 2024, 04:45:37 pmTRon, Thank you! You are most welcome. The part that is important to understand from that is is that you can use about any non visual component that way (provided the components does not have any GUI related dependencies) QuoteThis is quite a detailed example! I used it as an example for someone else on this forum so I am sorry for any possible additional noise in the code that doesn't really belong there (just try to ignore that). You can use almost any example from the wiki to accomplish the same task without using a GUI (with the exception of visual components like f.e. a dbedit or dbgrid etc). I prefer to setup my databases manually (even those with a GUI) to make it clear to the reader of the code what is done to initialize the database components. Using the object inspector to set the database properties and attaching events is usually not the most brightest idea and doing it this way allows to initialize the database components at any given time in your code (e.g. only when necessary though you can achieve similar results with using datamodules for/with GUI applications). @cdbc: Nice ! I use something similar when developing (smaller) projects that require database access. ---------------------------------------------------------------------- Thaddy · 2024-06-28 14:16 I still hope the newspaper boy will release his translation as open source.