Forum > Database
SQLite3 + FPC. native!
TRon:
--- Quote from: Seenkao on June 27, 2024, 04:06:12 pm ---I write about examples! :)
--- End quote ---
Oh, yes... you are right and I missed that. Apologies.
--- Quote ---Has no one ever done this?
--- End quote ---
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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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:
What about internet search and C API using? Like
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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:
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.html#msg496461
Seenkao:
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:
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
Navigation
[0] Message Index
[#] Next page
[*] Previous page