1
0
mirror of https://github.com/Bayselonarrend/OpenIntegrations.git synced 2025-11-25 22:12:29 +02:00
Files
OpenIntegrations/docs/en/md/MySQL/Common-methods/Execute-sql-query.mdx

155 lines
5.2 KiB
Plaintext
Raw Normal View History

2025-03-27 20:05:59 +03:00
---
sidebar_position: 4
2025-05-05 11:15:20 +03:00
description: Execute SQL query and other functions to work with MySQL in the Open Integration Package, a free open-source integration library for 1C:Enterprise 8, OneScript and CLI
2025-05-05 09:49:19 +03:00
keywords: [1C, 1С, 1С:Enterprise, 1С:Enterprise 8.3, API, Integration, Services, Exchange, OneScript, CLI, MySQL]
2025-03-27 20:05:59 +03:00
---
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
# Execute SQL query
Executes an arbitrary SQL query
`Function ExecuteSQLQuery(Val QueryText, Val Parameters = "", Val ForceResult = False, Val Connection = "", Val Tls = "") Export`
| Parameter | CLI option | Type | Required | Description |
|-|-|-|-|-|
| QueryText | --sql | String | ✔ | Database query text |
| Parameters | --params | Array Of Arbitrary | ✖ | Array of positional parameters of the request |
| ForceResult | --force | Boolean | ✖ | Includes an attempt to retrieve the result, even for nonSELECT queries |
| Connection | --dbc | String, Arbitrary | ✖ | Connection or connection string |
| Tls | --tls | Structure Of KeyAndValue | ✖ | TLS settings, if necessary. See GetTlsSettings |
Returns: Map Of KeyAndValue - Result of query execution
<br/>
:::tip
Query parameters are specified as an array of structures of the following type: `{'Type': 'Value'}`. The list of available types is described on the initial page of the MySQL library documentation
Without specifying the `ForcifyResult` flag, result data is returned only for queries beginning with `SELECT` keyword For other queries, `result:true` or `false` with error text is returned
:::
<br/>
```bsl title="1C:Enterprise/OneScript code example"
2025-06-29 14:35:33 +03:00
Image = "https://hut.openintegrations.dev/test_data/picture.jpg";
2025-03-27 20:05:59 +03:00
OPI_TypeConversion.GetBinaryData(Image); // Image - Type: BinaryData
2025-06-29 14:35:33 +03:00
Address = "127.0.0.1";
2025-03-27 20:05:59 +03:00
Login = "bayselonarrend";
2025-06-29 14:35:33 +03:00
Password = "12we...";
2025-03-27 20:05:59 +03:00
Base = "test_data";
2025-09-01 14:51:24 +03:00
TLS = True;
Port = 3306;
ConnectionString = OPI_MySQL.GenerateConnectionString(Address, Base, Login, Password, Port);
If TLS Then
TLSSettings = OPI_MySQL.GetTLSSettings(True);
Else
TLSSettings = Undefined;
EndIf;
Connection = OPI_MySQL.CreateConnection(ConnectionString, TLSSettings);
2025-03-27 20:05:59 +03:00
// CREATE
QueryText = "
|CREATE TABLE test_table (
|id INT AUTO_INCREMENT PRIMARY KEY,
|name VARCHAR(255),
|age INT,
|salary DOUBLE,
|amount FLOAT,
|type TINYINT UNSIGNED,
|date DATE,
|time TIME,
2025-04-02 14:56:42 +03:00
|data MEDIUMBLOB
2025-03-27 20:05:59 +03:00
|);";
Result = OPI_MySQL.ExecuteSQLQuery(QueryText, , , Connection);
// INSERT with parameters
QueryText = "
|INSERT INTO test_table (name, age, salary, amount, type, date, time, data)
|VALUES (?, ?, ?, ?, ?, ?, ?, ?);";
ParameterArray = New Array;
ParameterArray.Add(New Structure("TEXT" , "Vitaly"));
ParameterArray.Add(New Structure("INT" , 25));
ParameterArray.Add(New Structure("DOUBLE", 1000.12));
ParameterArray.Add(New Structure("FLOAT" , 1000.12));
ParameterArray.Add(New Structure("UINT" , 1));
ParameterArray.Add(New Structure("DATE" , OPI_Tools.GetCurrentDate()));
ParameterArray.Add(New Structure("TIME" , OPI_Tools.GetCurrentDate()));
ParameterArray.Add(New Structure("BYTES" , Image));
Result = OPI_MySQL.ExecuteSQLQuery(QueryText, ParameterArray, , Connection);
// SELECT (The result of this query is shown in the Result block)
QueryText = "SELECT name, age, salary, amount, type, date, time, data FROM test_table;";
Result = OPI_MySQL.ExecuteSQLQuery(QueryText, , , Connection);
2025-04-03 21:52:31 +03:00
// SQL query from file
2025-06-29 14:35:33 +03:00
SQLFile = "https://hut.openintegrations.dev/test_data/TEST_DATA2.sql"; // Binary Data, URL or path to file
2025-04-03 21:52:31 +03:00
Result = OPI_MySQL.ExecuteSQLQuery(SQLFile, , , Connection);
2025-03-27 20:05:59 +03:00
Closing = OPI_MySQL.CloseConnection(Connection);
```
2025-04-06 17:14:16 +03:00
<Tabs>
<TabItem value="bash" label="Bash" default>
```bash
2025-10-07 19:27:04 +03:00
# JSON data can also be passed as a path to a .json file
2025-04-06 17:14:16 +03:00
oint mysql ExecuteSQLQuery \
2025-10-07 19:27:04 +03:00
--sql "https://hut.openintegrations.dev/test_data/TEST_DATA2.sql" \
2025-09-16 09:07:33 +03:00
--dbc "mysql://bayselonarrend:***@127.0.0.1:3306/" \
2025-10-07 19:27:04 +03:00
--tls "{'accept_invalid_certs':true,'ca_cert_path':'','use_tls':true}"
2025-04-06 17:14:16 +03:00
```
</TabItem>
<TabItem value="bat" label="CMD/Bat" default>
```batch
2025-10-07 19:27:04 +03:00
:: JSON data can also be passed as a path to a .json file
2025-04-06 17:14:16 +03:00
oint mysql ExecuteSQLQuery ^
2025-10-07 19:27:04 +03:00
--sql "https://hut.openintegrations.dev/test_data/TEST_DATA2.sql" ^
2025-09-16 09:07:33 +03:00
--dbc "mysql://bayselonarrend:***@127.0.0.1:3306/" ^
2025-10-07 19:27:04 +03:00
--tls "{'accept_invalid_certs':true,'ca_cert_path':'','use_tls':true}"
2025-04-06 17:14:16 +03:00
```
</TabItem>
</Tabs>
```json title="Result"
{
"data": [
{
"age": 25,
"amount": 1000.11999511719,
"data": {
"BYTES": "Base64"
},
2025-10-15 14:32:00 +03:00
"date": "2025-10-15T00:00:00+00:00",
2025-04-06 17:14:16 +03:00
"name": "Vitaly",
"salary": 1000.12,
2025-10-15 14:32:00 +03:00
"time": "1970-01-01T09:20:08+00:00",
2025-04-06 17:14:16 +03:00
"type": 1
}
],
"result": true
}
```