--- sidebar_position: 6 description: Ensure table 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 keywords: [1C, 1С, 1С:Enterprise, 1С:Enterprise 8.3, API, Integration, Services, Exchange, OneScript, CLI, MySQL] --- import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem'; # Ensure table Creates a new table if it does not exist or updates the composition of columns in an existing table `Function EnsureTable(Val Table, Val ColoumnsStruct, Val Connection = "", Val Tls = "") Export` | Parameter | CLI option | Type | Required | Description | |-|-|-|-|-| | Table | --table | String | ✔ | Table name | | ColoumnsStruct | --cols | Structure Of KeyAndValue | ✔ | Column structure: Key > Name, Value > Data type | | Connection | --dbc | String, Arbitrary | ✖ | Existing connection or database path | | Tls | --tls | Structure Of KeyAndValue | ✖ | TLS settings, if necessary. See GetTlsSettings | Returns: Map Of KeyAndValue - Result of query execution
:::tip As a result of changing the table structure, data may be lost! It is recommended to test this method on test data beforehand This function does not update the data type of existing columns :::
```bsl title="1C:Enterprise/OneScript code example" Address = FunctionParameters["PG_IP"]; Login = "bayselonarrend"; Password = FunctionParameters["PG_Password"]; Base = "testbase1"; 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; Table = "testtable"; ColoumnsStruct = New Structure; ColoumnsStruct.Insert("smallint_field" , "SMALLINT"); ColoumnsStruct.Insert("double_field" , "DOUBLE"); ColoumnsStruct.Insert("bigint_field" , "BIGINT"); ColoumnsStruct.Insert("custom_field" , "TEXT"); // When using the connection string, a new connection is initialised, // which will be closed after the function is executed. // If several operations are performed, it is desirable to use one connection, // previously created by the CreateConnection function() Result = OPI_MySQL.EnsureTable(Table, ColoumnsStruct, ConnectionString, TLSSettings); ``` ```bash # JSON data can also be passed as a path to a .json file oint mysql EnsureTable \ --table "test_new" \ --cols "{'smallint_field':'SMALLINT','double_field':'DOUBLE','bigint_field':'BIGINT','custom_field':'TEXT'}" \ --dbc "mysql://bayselonarrend:***@127.0.0.1:3306/" \ --tls "{'use_tls':true,'accept_invalid_certs':true}" ``` ```batch :: JSON data can also be passed as a path to a .json file oint mysql EnsureTable ^ --table "test_new" ^ --cols "{'smallint_field':'SMALLINT','double_field':'DOUBLE','bigint_field':'BIGINT','custom_field':'TEXT'}" ^ --dbc "mysql://bayselonarrend:***@127.0.0.1:3306/" ^ --tls "{'use_tls':true,'accept_invalid_certs':true}" ``` ```json title="Result" { "result": true, "commit": { "result": true } } ```