1. Home
  2. Docs
  3. UniWP 1.5
  4. WP class
  5. DBQuery

DBQuery

The DBQuery is a client/server functionality that allows you to execute custom MySQL Query on your WordPress database from your Unity project. The aim of this functionality is to offer you a way for performing database related operations in order to get data from the WordPress and non-WordPress tables, insert and delete data, etc.


Creating MySQL Queries

For creating MySQL Queries, go to WordPress > Settings > TigerForge UniWP and, on the plugin page, click the DB QUERIES tab. You will have access to a simple interface for managing your custom Queries.

Writing a Query

In the text field under the DATABASE QUERY title, you can type a MySQL query without any kind of limitation. It’s up to you to keep attention on the query correction and validity. The system won’t perform any kind of check on what you typed.

Then, click the [+] button to insert the new Query into the Query List.

Inside your Query string, you can also include values from your Unity project. In this case, you have just to insert one or more markers inside the Query. A marker is a number, starting from 0, between double brackets.

Query List

Under the QUERY LIST title, you find the list of all the Queries. You can easily delete an unwanted query by clicking the red X icon on the right.

Every time you create a new Query or delete a Query, you must click the [Save Changes] button so as to save the list into WordPress.

ID

All your Queries have a unique ID. This is 6 digits random string that identifies that specific Query. When you need to use a query, you will have to use that ID.


DBQuery()

The DBQuery() method of the UniWP.WP class executes a Database Query, given its unique ID.

_ = wp.$DBQuery$(%queryID, parameters%).$Done$(%doneCallback%).$Error$(%errorCallback%).$Run$();

queryID|string
The unique ID of the Query to execute.
__
parameters|string[]
An array of strings to send to the UniWP WordPress plugin.
Each string will be integrated into the Query string following the order of the markers (the string at index 0 will be placed in the {{0}} marker).
__
doneCallback|function
The function to call when the operation is completed.
Note: the use of the Done() method is mandatory.
__
errorCallback|(function)
The function to call in case of error.
Note: the use of the Error() method is optional and can be omitted.

{{{#912c9a|WPReply|Data-type|Property or method description}}}
__
-|-
See the Returned Data paragraph below for the DBQuery() reply details.
__
.isOK|bool
true if everything is ok, false in case of error.
__
.isError|bool
true in case of error, false if everything is ok.
__
.error|string
The error message from the Server: “No users found.
__
errorType|HttpRequest.Error Enum
The kind of error:
NONE = no error.
HTTP_ERROR = communication error.
SYSTEM_ERROR = UniWP error.
WORDPRESS_ERROR = error message from WordPress REST APIs.

var wp = new UniWP.WP();

_ = wp.DBQuery("D3C7A5", new[] {"stev","gmail"})
    .Done((WPReply reply) => 
    {
        if (reply.isOK) Debug.Log("Query executed!");
        }
    })
    .Run()

Returned Data

A MySQL database returns different kinds of data depending on the executed query.

SELECT

The use of a SELECT query returns an array, in JSON format, in the WPReply.data property. This means that you have to convert the JSON string into an array and, accordingly, you have to prepare a class that represents the received data structure. If the result comes from a WordPress table, you can take advantage of the WPDBTable class that contains already prepared classes (here are more details).

You can also use the GetObjectsArray() method of the WPReply class that just requires the target class and it converts the JSON string for you (see the method here).

INSERT, UPDATE, DELETE

The other database operations just return the number of affected rows (inserted, updated or deleted). The INSERT query also returns the ID of the newly created record.

You can use the GetDBQueryInfo() method of the WPReply class for getting a DBQueryInfo object with the following properties:

.idThe ID of the record created by the INSERT query.
.rowsThe number of affected rows: inserted, updated, deleted.