[SOLVED] SQLite check if record exists lazarus / 16-Databases ---------------------------------------------------------------------- Pe3s · 2023-05-31 17:07 Hello, how can I check if a record exists, I want the program to display information that the record exists. e.g. The given e-mail address exists! ---------------------------------------------------------------------- paweld · 2023-05-31 20:38 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 SQLQuery1.SQL.Text := 'select name from users where email=:mail';   SQLQuery1.ParamByName('mail').AsString : 'kontakt@example.com';    SQLQuery1.Open;    if not SQLQuery1.IsEmpty then   begin     ShowMessage('E-mail is exist for user: ' + SQLQuery1.FieldByName('name').AsString);     SQLQuery1.Close;   end   else   begin     SQLQuery1.Close;     //Add email to db   end; ---------------------------------------------------------------------- Чебурашка · 2023-06-01 06:42 Quote from: Pe3s on May 31, 2023, 07:07:08 pmHello, how can I check if a record exists, I want the program to display information that the record exists. e.g. The given e-mail address exists! I normally use sql count function. 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   q.SQL.Text := 'select count(id) as count_id from table where string_expr=:string_expr';    q.ParamByName('string_expr').AsString : 'what?';      q.Open();  Exists := (q.FieldByName('count_id').AsInteger > 0);      q.Close(); if Exists then  ShowMessage(Format('Requested string_expr "%s" exist', ['what?'])); ---------------------------------------------------------------------- Pe3s · 2023-06-01 06:59 And what would the procedure of adding data look like with checking if the record exists ---------------------------------------------------------------------- Чебурашка · 2023-06-01 07:15 Quote from: Pe3s on June 01, 2023, 08:59:14 amAnd what would the procedure of adding data look like with checking if the record exists 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  if Exists then (*say that exists*)else  // add    q.SQL.Text = 'insert into table (...) values (:...)'  q.ParamByName('...').As := v...;  // .. repeat for each param  q.ExecSQL(); (* surround with try except to make it rite *)(* maybe say that you did add *) ---------------------------------------------------------------------- Zvoni · 2023-06-01 07:23 Quote from: Pe3s on June 01, 2023, 08:59:14 amAnd what would the procedure of adding data look like with checking if the record exists Whatever for? Declare the EMail-Field as UNIQUE and then just add the record. If the Value is already there you'll get an exception. btw: An EMail-Address is also a candidate for a Primary Key ---------------------------------------------------------------------- Чебурашка · 2023-06-01 08:11 Quote from: Zvoni on June 01, 2023, 09:23:23 amDeclare the EMail-Field as UNIQUE and then just add the record. If the Value is already there you'll get an exception. Maybe the OP prefers the schema check existence + insert, instead of insert + manage duplicate exception. I also do, but is just my personal preference. I am from the "exception is when your code does the right thing but something goes wrong, unpredictably, maybe sometimes yes and sometimes not" school. ---------------------------------------------------------------------- Zvoni · 2023-06-01 08:29 Huh? Why is OP asking the same question: https://forum.lazarus.freepascal.org/index.php/topic,61425.0.html ---------------------------------------------------------------------- Чебурашка · 2023-06-01 08:45 Quote from: Zvoni on June 01, 2023, 10:29:35 amHuh? Why is OP asking the same question: https://forum.lazarus.freepascal.org/index.php/topic,61425.0.html Maybe he wants to increase the post count because his goal to become a Hero Member asap. We should think about attributing a significance to all the posts we do (for example if I post "Thanks for the kind answer", I'd say that my post should not be counted in my posts count) ---------------------------------------------------------------------- Pe3s · 2023-06-01 14:50 Hello, I wanted to dust off the previous question, but the forum did not allow because it exceeded 120 days, so I asked again because I wanted to have a better grade at school at the end of the year about being a hero - pajamas with the Superman logo are enough  Thank you very much for all the replies, Regards and have a nice day