All database connection components except TIBDatabase let you execute SQL statements on the associated server by calling the Execute method. Although Execute can return a cursor when the statement is a SELECT statement, this use is not recommended. The preferred method for executing statements that return data is to use a dataset.
The Execute method is very convenient for executing simple SQL statements that do not return any records. Such statements include Data Definition Language (DDL) statements, which operate on or create a database's metadata, such as CREATE INDEX, ALTER TABLE, and DROP DOMAIN. Some Data Manipulation Language (DML) SQL statements also do not return a result set. The DML statements that perform an action on data but do not return a result set are: INSERT, DELETE, and UPDATE.
The syntax for the Execute method varies with the connection type:
procedure TForm1.CreateTableButtonClick(Sender: TObject); var SQLstmt: String; begin SQLConnection1.Connected := True; SQLstmt := 'CREATE TABLE NewCusts ' + '( " + ' CustNo INTEGER, ' + ' Company CHAR(40), ' + ' State CHAR(2), ' + ' PRIMARY KEY (CustNo) ' + ')'; SQLConnection1.Execute(SQLstmt, nil, nil); end;
void __fastcall TDataForm::CreateTableButtonClick(TObject *Sender) { SQLConnection1->Connected = true; AnsiString SQLstmt = "CREATE TABLE NewCusts " + "( " + " CustNo INTEGER, " + " Company CHAR(40), " + " State CHAR(2), " + " PRIMARY KEY (CustNo) " + ")"; SQLConnection1->Execute(SQLstmt, NULL, NULL); }
To use parameters, you must create a TParams object. For each parameter value, use the TParams.CreateParam method to add a TParam object. Then use properties of TParam to describe the parameter and set its value.
This process is illustrated in the following example, which uses TDatabase to execute an INSERT statement. The INSERT statement has a single parameter named: StateParam. A TParams object (called stmtParams) is created to supply a value of "CA" for that parameter.
procedure TForm1.INSERT_WithParamsButtonClick(Sender: TObject); var SQLstmt: String; stmtParams: TParams; begin stmtParams := TParams.Create; try Database1.Connected := True; stmtParams.CreateParam(ftString, 'StateParam', ptInput); stmtParams.ParamByName('StateParam').AsString := 'CA'; SQLstmt := 'INSERT INTO "Custom.db" '+ '(CustNo, Company, State) ' + 'VALUES (7777, "Robin Dabank Consulting", :StateParam)'; Database1.Execute(SQLstmt, stmtParams, False, nil); finally stmtParams.Free; end; end;
void __fastcall TForm1::INSERT_WithParamsButtonClick(TObject *Sender) { AnsiString SQLstmt; TParams *stmtParams = new TParams; try { Database1->Connected = true; stmtParams->CreateParam(ftString, "StateParam", ptInput); stmtParams->ParamByName("StateParam")->AsString = "CA"; SQLstmt = "INSERT INTO "Custom.db" "; SQLstmt += "(CustNo, Company, State) "; SQLstmt += "VALUES (7777, "Robin Dabank Consulting", :StateParam)"; Database1->Execute(SQLstmt, stmtParams, false, NULL); } __finally { delete stmtParams; } }
If the SQL statement includes a parameter but you do not supply a TParam object to provide its value, the SQL statement may cause an error when executed (this depends on the particular database back-end used). If a TParam object is provided but there is no corresponding parameter in the SQL statement, an exception is raised when the application attempts to use the TParam.
Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
|
What do you think about this topic? Send feedback!
|