@MySQLQuery
Syntax
@MySQLQuery(MySQLHandle;QueryStr);
@MySQLQuery(MySQLHandle;QueryStr;MySQLResultHandle);
@MySQLQuery(MySQLHandle;QueryStr;MySQLResultHandle;AutoIncrementIDRet);
Description
Executes a MySQL query.
If the query returns a result, it can be stored in the VSPECMYSQLRES MySQLResultHandle.
If the query contains an INSERT statement and it affects an AUTOINCREMENT ID column, the generated ID is returned in AutoIncrementIDRet.
The VSPECMYSQL MySQLHandle is provided by @MySQLInit.
Return:
On success TRUE, otherwise @Error;
Example: @MySQLQuery(MySQLHandle;QueryStr;MySQLResultHandle);
/* Example: Simple MySQL query from the Engine.
The script establishes a connection to a MySQL database,
reads a record from the table testdb.amount,
stores the result values in variables, and writes them to the log. */
/* Initialize the MySQL handle. */
MySQLh:=@MySQLInit;
/* Initialize the error status: 1 means error or no successful connection yet. */
MySQLError:=1;
/* Check whether the MySQL handle was initialized successfully. */
IF(@IsError(MySQLh)){
@LogReport(MySQLh;3);
}ELSE{
/* Set or initialize MySQL options. */
@MySQLOptions(MySQLh);
/* Define the connection data for the MySQL server. */
MySQLUser:="MySQLConnector";
MySQLPassword:="ConnectorPassword";
MySQLDefaultDatabase:="testdb";
MySQLHost:="172.16.1.215";
/* Establish the connection to the MySQL database. */
Ret:=@MySQLConnect(MySQLh;MySQLHost;MySQLUser;MySQLPassword;MySQLDefaultDatabase);
/* If the connection fails, log the error. */
IF(@IsError(Ret)){
@LogReport(Ret);
}ELSE{
/* The connection was successful; reset the error status. */
MySQLError:=0;}
/* Execute the query only if the connection was established successfully. */
IF(!MySQLError){
/* Insert a record with a specific name into the MySQL table. */
InsertQueryStr:="INSERT INTO testdb.amount (Name,Amount,UID,ModTime) VALUES (\"Test User2\",123.45,\"1234567890ABCDEFABCD123456789000\",NOW());";
Ret:=@MySQLQuery(MySQLh;InsertQueryStr);
/* Was the INSERT successful? If not, write the error to the log. */
IF(@IsError(Ret)){
@LogReport(Ret;3);}
/* How many rows were affected? */
AffectedRows:=@MySQLGetAffectedRows(MySQLh);
@LogReport("AffectedRows INSERT :"+@Text(AffectedRows);3);
/* Read a record with a specific name from the MySQL table. */
QueryStr:="SELECT ID,Name,Amount,UID FROM testdb.amount WHERE Name=\"Test User2\";";
/* Execute the query and return the result handle in MySQLResH. */
Ret:=@MySQLQuery(MySQLh;QueryStr;MySQLResH);
IF(@IsError(Ret)){
@LogReport(Ret;3);}
/* Determine the number of rows returned by the query. */
NumRows:=@MySQLGetNumRows(MySQLResH);
@LogReport("NumRows found:"+@Text(NumRows);3);
/* Read exactly one expected record. */
IF(NumRows=1){
/* Read the first result row and store the column values in variables. */
Ret:=@MySQLFetchRow(MySQLResH;1;ThisQueryID;ThisQueryName;ThisQueryAmount;ThisQueryUID);
IF(@IsError(Ret)){
@LogReport(Ret;3);}
}ELSE{
/* If no unique record was found, set default values. */
ThisQueryID:=-1;
ThisQueryAmount:=0;
ThisQueryName:=ThisQueryUID:="";
}
/* Remove the record with the specific name from the MySQL table again. */
DeleteQueryStr:="DELETE FROM testdb.amount WHERE Name=\"Test User2\";";
Ret:=@MySQLQuery(MySQLh;DeleteQueryStr);
/* Was the DELETE successful? If not, write the error to the log. */
IF(@IsError(Ret)){
@LogReport(Ret;3);}
/* How many rows were affected? */
AffectedRows:=@MySQLGetAffectedRows(MySQLh);
@LogReport("AffectedRows DELETE :"+@Text(AffectedRows);3);
/* Free the result handle. */
MySQLResH:=@MySQLFreeResult(MySQLResH);
/* Output the retrieved or assigned result values. */
@LogReport("ThisQueryID :"+@Text(ThisQueryID);3);
@LogReport("ThisQueryName :"+ThisQueryName;3);
@LogReport("ThisQueryAmount:"+@Text(ThisQueryAmount);3);
@LogReport("ThisQueryUID :"+ThisQueryUID;3);
}
/* Close the MySQL connection. */
MySQLh:=@MySQLClose(MySQLh);
}
/* End or clean up the MySQL thread context. */
@MySQLThreadEnd;
MySQL DB for this example:
CREATE DATABASE testdb
CHARACTER SET latin1 COLLATE latin1_german1_ci;
CREATE TABLE testdb.amount (
`ID` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
`Name` VARCHAR(30),
`Amount` FLOAT,
`UID` VARCHAR(32),
`ModTime` DATETIME,
PRIMARY KEY(`ID`),
INDEX `Name` USING BTREE (`Name`),
INDEX `UID` USING BTREE (`UID`)
)
ENGINE = MYISAM
CHARACTER SET latin1 COLLATE latin1_german1_ci;
Output in the APILOG for this example:
AffectedRows INSERT :1
NumRows found:1
AffectedRows DELETE :1
ThisQueryID :10
ThisQueryName :Test User2
ThisQueryAmount:123.45
ThisQueryUID :1234567890ABCDEFABCD123456789000
A MySQL connection is established and a query is issued; afterwards, the result is output to the APILOG.
Note : This text was machine-translated and may contain inaccuracies.
