delphi dxExpress MySQL: invalid LAST_INSERT_ID value - mysql

I am developing an application in Delphi and want to insert records to Mysql's table.
An then I want to know inserted record's identity value. So I write bellow code.
On run time , insert is done and record added but returned value for identity is zero!!
what is my mistake ?!!
-- MySql table create
CREATE TABLE Sample_Table (
id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
mobile_number varchar(20) DEFAULT NULL,
message_body text,
PRIMARY KEY (id)
);
--- Delphi code
SQLCon := TSQLConnection.Create(self);
with SQLCon do begin
Close;
DriverName := 'MySQL';
GetDriverFunc := 'getSQLDriverMYSQL';
LibraryName := 'dbxmys.dll';
VendorLib := 'LIBMYSQL.dll';
LoginPrompt := false;
Params.Values['HostName'] := '127.0.0.1';
Params.Values['Database'] := 'sms_test';
Params.Values['User_Name'] := 'root';
Params.Values['Password'] := 'root';
Open;
end;
SQLQry := TSQLQuery.Create(self);
with SQLQry do begin
Close;
SQLConnection := SQLCon;
SQL.Clear;
SQL.Add('INSERT INTO Sample_Table ');
SQL.Add('(mobile_number, message_body) VALUES');
SQL.Add(format('(%s, %s);',[QuotedStr('989121011689'), QuotedStr('Text1')]));
ExecSQL();
Close;
SQL.Clear;
SQL.Add('SELECT LAST_INSERT_ID() EngineRefNo;');
Open;
First;
ListBox1.items.Add(FieldByName('EngineRefNo').AsString);
Close;
end;
SQLCon.Close;
any advise?

You should get the ID in one go
SQLQry := TSQLQuery.Create(self);
with SQLQry do begin
SQLConnection := SQLCon;
SQL.Add('INSERT INTO Sample_Table ');
SQL.Add('(mobile_number, message_body) VALUES');
SQL.Add(format('(%s, %s);',[QuotedStr('989121011689'), QuotedStr('Text1')]));
SQL.Add('SELECT LAST_INSERT_ID() EngineRefNo;');
Open;
ListBox1.items.Add(FieldByName('EngineRefNo').AsString);
Close;
end;
SQLCon.Close;
and you should think about using parameters to prevent sql injection
SQLQry := TSQLQuery.Create(self);
with SQLQry do begin
SQLConnection := SQLCon;
SQL.Add('INSERT INTO Sample_Table ');
SQL.Add('( mobile_number, message_body ) VALUES');
SQL.Add('( :mobile_number, :message_body );');
SQL.Add('SELECT LAST_INSERT_ID() EngineRefNo;');
ParamByName( 'mobile_number' ).Value := '989121011689';
ParamByName( 'message_body' ).Value := 'Text1';
Open;
ListBox1.items.Add(FieldByName('EngineRefNo').AsString);
Close;
end;
SQLCon.Close;

Apparently this is a problem known with delphi .
quoting the above link:
Certain ODBC applications (including Delphi and Access) may have
trouble obtaining the auto-increment value using the previous
examples. In this case, try the following statement as an alternative:
SELECT * FROM tbl WHERE auto IS NULL;
This alternative method requires that sql_auto_is_null variable is not
set to 0. See Server System Variables
So in your case (untested):
SQLCon := TSQLConnection.Create(self);
with SQLCon do begin
Close;
DriverName := 'MySQL';
GetDriverFunc := 'getSQLDriverMYSQL';
LibraryName := 'dbxmys.dll';
VendorLib := 'LIBMYSQL.dll';
LoginPrompt := false;
Params.Values['HostName'] := '127.0.0.1';
Params.Values['Database'] := 'sms_test';
Params.Values['User_Name'] := 'root';
Params.Values['Password'] := 'root';
Open;
end;
SQLQry := TSQLQuery.Create(self);
with SQLQry do begin
Close;
SQLConnection := SQLCon;
SQL.Clear;
SQL.Add('INSERT INTO Sample_Table ');
SQL.Add('(mobile_number, message_body) VALUES');
SQL.Add(format('(%s, %s);',[QuotedStr('989121011689'), QuotedStr('Text1')]));
ExecSQL();
Close;
SQL.Clear;
SQL.Add('SELECT id FROM Sample_Table WHERE id IS NULL');
Open;
First;
ListBox1.items.Add(FieldByName('Id').AsString);
Close;
end;
SQLCon.Close;

Related

Insert the records of a dbf file into a table in MS Access database using Delphi

I am trying to insert the records of a dbf file into a table in a MS Access database that I have already created.
The dbf file's name is tab1.dbf and it has three columns: cl1, cl2, cl3.
The MS Access database name is db1 and it has one table tb2 with three columns: cl1, cl2, cl3.
I have connected Delphi to the MS Access database using ADOConnection1.
To insert the dbf file's records, I have to click in a Button1 with OpenDialog1
The code I use is this :
procedure TForm1.Button1Click(Sender: TObject);
var importdir,ipo : string;
begin
if form1.OpenDialog1.Execute then
begin
importdir:= extractfiledir(form1.OpenDialog1.FileName);
ipo:= form1.OpenDialog1.FileName ;
end;
form1.Edit1.Text:= importdir;
ADOConnection1.Execute('insert into tab2 SELECT * FROM [ database = '+ipo+' ].tab1' );
end;
but when i execut the form1 i have this error message :
name of the file is incorrect
can you help me guys ?
Here after is one simple solution. It simple because it assume the Access database structure is the same as the dBASE structure. You'll get started with this example that you'll adapt to your needs.
procedure TDbfToAccessWithAdoForm.DbfToAccessButtonClick(Sender: TObject);
var
Fld : Integer;
FldValue : Variant;
InsertSQL : String;
begin
ADOConnectionAccess.Connected := TRUE;
ADOConnectionDbf.Connected := TRUE;
ADOQueryDbf.SQL.Text := 'Select * from Clients';
ADOQueryDbf.Open;
// Build the parametrized INSERT statement
InsertSQL := 'insert into Clients(';
for Fld := 0 to ADOQueryDbf.FieldCount - 1 do
InsertSQL := InsertSQL + ADOQueryDbf.Fields[Fld].FieldName + ',';
// Remove extra coma
Delete(InsertSQL, Length(InsertSQL), 1);
InsertSQL := InsertSQL + ') values (';
for Fld := 0 to ADOQueryDbf.FieldCount - 1 do
InsertSQL := InsertSQL + ':' + ADOQueryDbf.Fields[Fld].FieldName + ',';
// Remove extra coma
Delete(InsertSQL, Length(InsertSQL), 1);
InsertSQL := InsertSQL + ')';
while not ADOQueryDbf.Eof do begin
ADOQueryAccess.SQL.Text := InsertSQL;
for Fld := 0 to ADOQueryDbf.FieldCount - 1 do begin
FldValue := ADOQueryDbf.Fields[Fld].Value;
// Here you can do whatever conversion is required
if FldValue = Null then begin
if ADOQueryDbf.FieldDefList[Fld].DataType = ftDateTime then
FldValue := 0 // My Access table doesn't like empty datetime
else
FldValue := ' '; // My Access table doesn't like empty string
end;
ADOQueryAccess.Parameters.ParamByName(ADOQueryDbf.Fields[Fld].FieldName).Value := FldValue;
end;
ADOQueryAccess.ExecSQL;
ADOQueryDbf.Next;
end;
ADOQueryDbf.Close;
ADOQueryAccess.Close;
end;
You should add error checking and try/finally or try/except. I let you do it as you do usually.

Decode base64 image into BLOB with PL/SQL

I'm using the script below in order to fetch JSON file from MongoDB, parse it and then insert it into Oracle table.
The script works fine in a sense that it inserts all values correctly into Oracle table. That includes the value Photo which is an image of base64 formate and it is much larger than 32KB.
The column Photo in the table Appery_Photos is of the type CLOB while column DecodedPhoto is of the type BLOB.
The problem lies in the line blobOriginal := base64decode1(Photo); which I used to decode the CLOB into BLOB. The function base64decode1 has been replaced with several functions (i.e. decode_base64 , base64DecodeClobAsBlob_plsql, base64decode , from_base64 & finally JSON_EXT.DECODE).
The result was the same for all of them. That is, the resultant BLOB object cannot be openned as an image in any of images editors (I'm using Oracle SQL Developer to download it).
I checked CLOB, and I could not find any newlines \n, nor could I find any spaces (only + signs found). Furthermore, I inserted CLOB value into the base64-image-converter and it displays the image correctly. In addition, I tried to encode the resultant BLOB in base64 back in order to further validate (using the opposite functions provided in the links above), the resultant base64 is not the same at all.
BEGIN
l_http_request := UTL_HTTP.begin_request('https://api.appery.io/rest/1/db/collections/Photos?where=%7B%22Oracle_Flag%22%3A%22Y%22%7D' , 'GET' , 'HTTP/1.1');
-- ...set header's attributes
UTL_HTTP.set_header(l_http_request, 'X-Appery-Database-Id', '53f2dac5e4b02cca64021dbe');
l_http_response := UTL_HTTP.get_response(l_http_request);
BEGIN
LOOP
UTL_HTTP.read_text(l_http_response, buf);
l_response_text := l_response_text || buf;
END LOOP;
EXCEPTION
WHEN UTL_HTTP.end_of_body THEN
NULL;
END;
l_list := json_list(l_response_text);
FOR i IN 1..l_list.count
LOOP
A_id := json_ext.get_string(json(l_list.get(i)),'_id');
l_val := json_ext.get_json_value(json(l_list.get(i)),'Photo');
dbms_lob.createtemporary(Photo, true, 2);
json_value.get_string(l_val, Photo);
dbms_output.put_line(dbms_lob.getlength(Photo));
dbms_output.put_line(dbms_lob.substr(Photo, 20, 1));
blobOriginal := base64decode1(Photo);
A_Name := json_ext.get_string(json(l_list.get(i)),'Name');
Remarks := json_ext.get_string(json(l_list.get(i)),'Remarks');
Status := json_ext.get_string(json(l_list.get(i)),'Status');
UserId := json_ext.get_string(json(l_list.get(i)),'UserId');
A_Date := json_ext.get_string(json(l_list.get(i)),'Date');
A_Time := json_ext.get_string(json(l_list.get(i)),'Time');
MSG_status := json_ext.get_string(json(l_list.get(i)),'MSG_status');
Oracle_Flag := json_ext.get_string(json(l_list.get(i)),'Oracle_Flag');
acl := json_ext.get_string(json(l_list.get(i)),'acl');
INSERT
INTO Appery_Photos
(
A_id,
Photo,
DecodedPhoto,
A_Name,
Remarks,
Status,
UserId,
A_Date,
A_Time,
MSG_status ,
Oracle_Flag,
acl
)
VALUES
(
A_id,
Photo,
blobOriginal,
A_Name,
Remarks,
Status,
UserId,
A_Date,
A_Time,
MSG_status ,
Oracle_Flag,
acl
);
dbms_lob.freetemporary(Photo);
END LOOP;
-- finalizing
UTL_HTTP.end_response(l_http_response);
EXCEPTION
WHEN UTL_HTTP.end_of_body THEN
UTL_HTTP.end_response(l_http_response);
END;
Any help is deeply appreciated.
I found that is not in the function I used in the base64 decoding. Instead, the value I have is not base64 encoded strings, but base64 encode dataURi's, something like
data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAA
So I have to use something like: clobbase642blob( substr( Photo, instr( Photo, ',' ) + 1 ) )
The following script is inspired by Oracle Community answer
DECLARE
l_param_list VARCHAR2(512);
l_http_request UTL_HTTP.req;
l_http_response UTL_HTTP.resp;
l_response_text CLOB;
--l_response_text VARCHAR2(32767);
buf VARCHAR2(32767);
l_list json_list;
l_val json_value;
A_id VARCHAR2(100);
Photo CLOB;
A_Name VARCHAR2(100);
Remarks VARCHAR2(100);
Status VARCHAR2(100);
UserId VARCHAR2(100);
A_Date VARCHAR2(100);
A_Time VARCHAR2(100);
MSG_status VARCHAR2(100);
Oracle_Flag VARCHAR2(100);
acl VARCHAR2(100);
obj json_list;
blobOriginal BLOB := empty_blob();
clobInBase64 CLOB;
substring VARCHAR2(2000);
tmp BLOB;
n pls_integer := 0;
substring_length pls_integer := 2000;
------------------------------------------------------
FUNCTION clobbase642blob(
p_clob CLOB )
RETURN BLOB
IS
t_blob BLOB;
t_buffer VARCHAR2(32767);
t_pos NUMBER := 1;
t_size NUMBER := nls_charset_decl_len( 32764, nls_charset_id( 'char_cs' ) );
t_len NUMBER;
t_tmp raw(32767);
BEGIN
dbms_lob.createtemporary( t_blob, true );
t_len := LENGTH( p_clob );
LOOP
EXIT
WHEN t_pos > t_len;
t_buffer := REPLACE( REPLACE( SUBSTR( p_clob, t_pos, t_size ), chr(10) ), chr(13) );
t_pos := t_pos + t_size;
WHILE t_pos 0
LOOP
t_buffer := t_buffer || REPLACE( REPLACE( SUBSTR( p_clob, t_pos, 1 ), chr(10) ), chr(13) );
t_pos := t_pos + 1;
END LOOP;
t_tmp := utl_encode.base64_decode( utl_raw.cast_to_raw( t_buffer ) );
dbms_lob.writeappend( t_blob, utl_raw.length( t_tmp ), t_tmp );
END LOOP;
RETURN t_blob;
END;
------------------------------------------------------
BEGIN
-- service's input parameters
-- preparing Request...
l_http_request := UTL_HTTP.begin_request('https://api.appery.io/rest/1/db/collections/Photos?where=%7B%22Oracle_Flag%22%3A%22Y%22%7D' , 'GET' , 'HTTP/1.1');
-- ...set header's attributes
UTL_HTTP.set_header(l_http_request, 'X-Appery-Database-Id', '53f2dac5e4b02cca64021dbe');
l_http_response := UTL_HTTP.get_response(l_http_request);
BEGIN
LOOP
UTL_HTTP.read_text(l_http_response, buf);
l_response_text := l_response_text || buf;
END LOOP;
EXCEPTION
WHEN UTL_HTTP.end_of_body THEN
NULL;
END;
l_list := json_list(l_response_text);
FOR i IN 1..l_list.count
LOOP
A_id := json_ext.get_string(json(l_list.get(i)),'_id');
--deal with base64 URI photo >32KB
l_val := json_ext.get_json_value(json(l_list.get(i)),'Photo');
dbms_lob.createtemporary(Photo, true, 2);
json_value.get_string(l_val, Photo);
--dbms_output.put_line(dbms_lob.getlength(Photo));
--dbms_output.put_line(dbms_lob.substr(Photo, 20, 1));
blobOriginal := clobbase642blob( SUBSTR( Photo, 24 ) );
A_Name := json_ext.get_string(json(l_list.get(i)),'Name');
Remarks := json_ext.get_string(json(l_list.get(i)),'Remarks');
Status := json_ext.get_string(json(l_list.get(i)),'Status');
UserId := json_ext.get_string(json(l_list.get(i)),'UserId');
A_Date := json_ext.get_string(json(l_list.get(i)),'Date');
A_Time := json_ext.get_string(json(l_list.get(i)),'Time');
MSG_status := json_ext.get_string(json(l_list.get(i)),'MSG_status');
Oracle_Flag := json_ext.get_string(json(l_list.get(i)),'Oracle_Flag');
acl := json_ext.get_string(json(l_list.get(i)),'acl');
INSERT
INTO Appery_Photos
(
A_id,
Photo,
DecodedPhoto,
A_Name,
Remarks,
Status,
UserId,
A_Date,
A_Time,
MSG_status ,
Oracle_Flag,
acl
)
VALUES
(
A_id,
Photo,
blobOriginal,
A_Name,
Remarks,
Status,
UserId,
A_Date,
A_Time,
MSG_status ,
Oracle_Flag,
acl
);
dbms_lob.freetemporary(Photo);
END LOOP;
-- finalizing
UTL_HTTP.end_response(l_http_response);
EXCEPTION
WHEN UTL_HTTP.end_of_body THEN
UTL_HTTP.end_response(l_http_response);
END;
/
It is Base64 or HexBinary...
This works for HexBinary
function DESERIALIZE_HEX_BLOB(P_SERIALIZATION CLOB)
return BLOB
is
V_BLOB BLOB;
V_OFFSET INTEGER := 1;
V_AMOUNT INTEGER := 32000;
V_INPUT_LENGTH NUMBER := DBMS_LOB.GETLENGTH(P_SERIALIZATION);
V_HEXBINARY_DATA VARCHAR2(32000);
begin
if (P_SERIALIZATION is NULL) then return NULL; end if;
DBMS_LOB.CREATETEMPORARY(V_BLOB,TRUE,DBMS_LOB.CALL);
while (V_OFFSET <= V_INPUT_LENGTH) loop
V_AMOUNT := 32000;
DBMS_LOB.READ(P_SERIALIZATION,V_AMOUNT,V_OFFSET,V_HEXBINARY_DATA);
V_OFFSET := V_OFFSET + V_AMOUNT;
DBMS_LOB.APPEND(V_BLOB,TO_BLOB(HEXTORAW(V_HEXBINARY_DATA)));
end loop;
return V_BLOB;
end;
--
And could probably be modified to handle Base64 without too much trouble.

Lazarus Pascal parameterized query causing segfault! Why?

I've only just started learning Pascal so please excuse my ineptitude if I've missed something glaringly obvious.
I have a program that connects to a DB, retrieves a list of accounts and displays them in a StringGrid. I am now trying to extend the program so that selecting a row in the grid will perform a further search using parameters returned from the grid.
I have the grid setup correctly (I think!), I've managed to write the accountID value into a variable and am using it to construct a query.
When the time comes to run the query a segfault is issued and I don't understand why, the error message is
Project SQLConnect raised exception class 'External: SIGSEGV' at address 583166
The Assembler window shows the following gobbledygook:
00583166 8b09 mov (%ecx),%ecx
Here are the procedures I'm using to return data
Initial return before selecting row, this works - on its own:
procedure TForm1.sendQueryClick(Sender: TObject);
begin
CreateConnection;
CreateTransaction;
try
Query := GetQuery;
Query.SQL.Text := 'SELECT * FROM tbl_accounts LEFT JOIN tbl_properties ON tbl_accounts.ClientID = tbl_properties.PropertyID LEFT JOIN tbl_clients ON tbl_accounts.ClientID = tbl_clients.ClientID ORDER BY tbl_accounts.AccountID DESC';
AConnection.Open;
Query.Open;
while not Query.Eof do
begin
accID := Query.FieldByName('AccountID').AsString;
accountNo := Query.FieldByName('AccountNumber').AsString;
mortgagors := Query.FieldByName('Mortgagors').AsString;
address := Query.FieldByName('Address').AsString;
accResults.RowCount := accResults.RowCount + 1;
accResults.Cells[0, accResults.RowCount - 1] := accID;
accResults.Cells[1, accResults.RowCount - 1] := accountNo;
accResults.Cells[2, accResults.RowCount - 1] := mortgagors;
accResults.Cells[3, accResults.RowCount - 1] := address;
Query.Next;
end;
finally
Query.Close;
AConnection.Close;
Query.Free;
ATransaction.Free;
AConnection.Free;
end;
end;
This is the row select query, which causes the segfault:
procedure TForm1.accResultsSelectCell(Sender: TObject; aCol, aRow: Integer; var CanSelect: Boolean);
begin
// Hide Account ID Column
accResults.ColWidths[0] := 0;
CurrCol := 0;
CurrRow := aRow;
// Grab Account ID value from cell
CellValue := accResults.Cells[CurrCol, CurrRow];
selectedRow.Text := CellValue;
// Setup Query
try
TestQuery := GetQuery;
TestQuery.SQL.Text := 'SELECT * FROM tbl_accounts LEFT JOIN tbl_properties ON tbl_accounts.ClientID = tbl_properties.PropertyID LEFT JOIN tbl_clients ON tbl_accounts.ClientID = tbl_clients.ClientID WHERE AccountID = :AccID';
TestQuery.Params.ParamByName('AccID').AsString := CellValue;
// Open Database connection
AConnection.Open;
TestQuery.Open;
begin
end;
finally
TestQuery.Close;
AConnection.Close;
TestQuery.Free;
ATransaction.Free;
AConnection.Free;
end;
end;
IIRC: Make sure that the type of the parameter is not ftunknown.

How can I avoid storing duplicates in the database in this code?

I have a user registration process which stores user info to my database using MYDAC components. Currently it allow duplicate users, which is not my intention. My code is below, but I don't know where the problem is.
procedure TForm1.Button1Click(Sender: TObject);
begin
if (edit1.Text <> '') and (edit2.Text <> '') and (edit3.Text <> '') and
(edit4.Text <> '') then
begin
MyQuery1.Close;
MyQuery1.SQL.Text := 'select * from uyeler '+
'where nick=:0 and mail=:0 and site=:0';
MyQuery1.Params[0].AsString:=edit1.text;
MyQuery1.Params[0].AsString:=edit2.text;
MyQuery1.Params[0].AsString:=edit3.text;
MyQuery1.open;
if MyQuery1.RecordCount = 0 then
MessageDlg('The same information! Try again.', mtError, [mbOK], 0)
else
MyQuery1.Close;
MyQuery1.SQL.Text := 'INSERT INTO uyeler (nick, mail, site, sifre) VALUES '+
'(:nick, :mail, :site, :sifre)';
MyQuery1.ParamByName('nick').AsString := Edit1.text;
MyQuery1.ParamByName('mail').AsString := Edit2.text;
MyQuery1.ParamByName('site').AsString := Edit3.text;
MyQuery1.ParamByName('sifre').AsString := Edit4.text;
MyQuery1.Execute;
Button1.Enabled := False;
MessageDlg('Mission complate!', mtInformation, [mbOK], 0);
Edit1.Clear;
Edit2.Clear;
Edit3.clear;
Edit4.Clear;
PageControl2.Visible := False;
PageControl1.Visible := True;
end
else
begin
MessageDlg('Information is missing! Try again.', mtWarning,[mbOK],0);
end;
end;
How can I prevent signing up with the same? What should I do in this case?
I would typically use unique indexes on the underlying MySQL table to enforce this.
You're checking the wrong result. You need to change your test to
if MyQuery1.RecordCount > 0 then // At least one match found already
Better yet, if MyDac supports it, is to use
if not MyQuery1.IsEmpty then // row already exists.
Actually, you have more issues than that, though. You have a mismatched begin and end block, because right now you're always running the insert part of the method. As #TLama says, you're also using the same pameter multiple times, assigning nick, mail, and site the all the same value. Use named parameters instead (shown below in both the SQL and the parameter assignments).
procedure TForm1.Button1Click(Sender: TObject);
var
UserExists: Boolean;
begin
Button1.Enabled:=false;
if (edit1.Text <> '') and (edit2.Text <> '') and
(edit3.Text <> '') and (edit4.Text <> '') then
begin
MyQuery1.Close;
MyQuery1.SQL.Text :=' select* from uyeler '+
'where nick=:nick and mail=:mail and site=:site';
MyQuery1.ParamByName('nick').AsString:=edit1.text;
MyQuery1.ParamByName('mail').AsString:=edit2.text;
MyQuery1.ParamByName('site').AsString:=edit3.text;
MyQuery1.open;
try
UserExists := not MyQuery1.IsEmpty;
finally
MyQuery1.Close;
end;
if UserExists then
MessageDlg('The same information! Try again.', mtError,[mbOK],0)
else
begin // <<--- Added begin
MyQuery1.SQL.Text :=' INSERT INTO uyeler (nick, mail, site, sifre) VALUES '+
'(:nick, :mail, :site, :sifre)';
MyQuery1.ParamByName('nick').AsString := Edit1.text;
MyQuery1.ParamByName('mail').AsString := Edit2.text;
MyQuery1.ParamByName('site').AsString := Edit3.text;
MyQuery1.ParamByName('sifre').AsString := Edit4.text;
try
MyQuery1.Execute;
finally
MyQuery1.Close;
end;
end; // <------------ Moved end from below where marked
MessageDlg('Mission complate!', mtInformation,[mbOK],0);
Edit1.Clear;
Edit2.Clear;
Edit3.clear;
Edit4.Clear;
PageControl2.Visible:=false;
PageControl1.Visible:=true;
end // <------------- removed extra end caused by addition above
else
MessageDlg('Information is missing! Try again.', mtWarning,[mbOK],0);
end;
IMO the answer posted by #Ken White should work fine, but since you are finding troubles. I suggest you to try with this code. Its just the difference in the executing the queries.
I am considering the field datatypes to be Char or VarChar, hence " " while entering the data values
procedure TForm1.Button1Click(Sender: TObject);
begin
Button1.Enabled:=false;
if (edit1.Text <> '') and (edit2.Text <> '') and
(edit3.Text <> '') and (edit4.Text <> '') then
begin
MyQuery1.SQL.Clear;
MyQuery1.SQL.Add(' select* from uyeler where nick="'+edit1.text+'"' +
'and mail="'+edit2.text+'" and site="'+edit3.text+'"');
MyQuery1.Execute;
if not MyQuery1.IsEmpty then //--- can also use MyQuery1.RecordCount >0
MessageDlg('The same information! Try again.', mtError,[mbOK],0)
else
begin //--- no duplicates present
MyQuery1.SQL.Clear;
MyQuery1.SQL.Add(' INSERT INTO uyeler (nick, mail, site, sifre) VALUES '+
'("'+edit1.text+'", "'+edit2.text+'","'+edit3.text+'", "'+edit4.text+'")');
try
MyQuery1.Execute;
finally
MyQuery1.SQL.Clear;
end;
MessageDlg('Mission complate!', mtInformation,[mbOK],0);
Edit1.Clear;
Edit2.Clear;
Edit3.clear;
Edit4.Clear;
PageControl2.Visible:=false;
PageControl1.Visible:=true;
end;
end;
else
MessageDlg('Information is missing! Try again.', mtWarning,[mbOK],0);
end;

Return value of stored functions in MyDAC

I am working with Devart's MyDac and MySQL Server 5.0.41. Here is a section from the documentation on executing stored procedures with TMyConnection.ExecProc:
Note: Stored functions unlike stored procedures return result values that are obtained internally through the RESULT parameter. You will no longer have to provide anonymous value in the Params array to describe the result of the function. The stored function result is obtained from the Params[0] indexed property or with the ParamByName('RESULT') method call.
They also give an example on how to execute a stored function:
aStringVariable1 := TMyConnection.ExecProc('StoredFunctionName',['Param1','Param2']);
aStringVariable2 := TMyConnection.ParamByName('Result').AsString;
By Following these examples, my execution of the stored functions are returning Param1 in the variable aStringVariable2.The execution of the functions in the Query Browser returns the right results. Any pointers on the right way to execute stored functions in MyDAC with TMyConnection or TMyStoredProc will be appreciated.
Thanks in advance.
Here is the code we use to call stored procedures - hope it helps
function TDbControl.DatabaseStoredProc(FConnectionsAddr: integer; SpName: string;var Params: TDAParams): boolean;
var
MyStoredProc: TMyStoredProc;
PramsTxt: String;
Idx, Idx2: Integer;
begin
result := False;
MyStoredProc := nil;
try
try
MyStoredProc := TMyStoredProc.Create(nil);
MyStoredProc.Connection := TMyConnection(FConnectionsAddr);
MyStoredProc.StoredProcName := SpName;
MyStoredProc.ParamCheck := False;
if assigned(Params) then
begin
for Idx := 0 to Params.Count - 1 do
begin
MyStoredProc.ParamByName(Params[Idx].Name).DataType := Params[Idx].DataType;
MyStoredProc.ParamByName(Params[Idx].Name).Value := Params[Idx].Value;
end;
end;
MyStoredProc.Execute;
if assigned(Params) then
begin
for Idx := 0 to Params.Count - 1 do
begin
if (Params[Idx].ParamType = ptOutput ) then
Params[Idx].Value := MyStoredProc.ParamByName(Params[Idx].Name).Value;
end;
end;
result := True;
except
on E: Exception do
begin
PramsTxt := '';
if assigned(Params) then
begin
for Idx2 := 0 to Params.Count - 1 do
begin
PramsTxt := PramsTxt + Params.Items[Idx2].Name + '=' + Params[Idx2].AsString + ',';
end;
end;
LogText(FConnectionsAddr, 'DatabaseStoredProc Err:' + E.Message + ' SpName:' + SpName + ' Prams:' + PramsTxt);
raise ;
end;
end;
finally
FreeAndNil(MyStoredProc);
end;
end;