Skip to main content

Ensure records

Adds records or updates data of existing ones when key fields match

Function EnsureRecords(Val Table, Val DataArray, Val KeyFields = "", Val Transaction = True, Val Connection = "") Export

ParameterCLI optionTypeRequiredDescription
Table--tableStringTable name
DataArray--rowsArray of StructureAn array of string data structures: Key > field, Value > field value
KeyFields--uniqueArray Of StringName or names of key table fields for uniqueness validation
Transaction--trnBooleanTrue > adding records to transactions with rollback on error
Connection--dbString, ArbitraryExisting connection or database path
Returns

Map Of KeyAndValue - Result of query execution

tip

UNIQUE and PRIMARY KEY fields can be specified as key fields. If fields are not specified, uniqueness is determined by all suitable fields


1C:Enterprise/OneScript code example
Base = "/tmp/vnnmoosn.qqb.sqlite";
Table = "test_merge";

DataArray = New Array;

RowStructure2 = New Structure;
RowStructure2.Insert("id" , 1);
RowStructure2.Insert("name" , "Vitaly");
RowStructure2.Insert("age" , 25);
RowStructure2.Insert("salary", 1000.12);

RowStructure1 = New Structure;
RowStructure1.Insert("id" , 2);
RowStructure1.Insert("name" , "Lesha");
RowStructure1.Insert("age" , 20);
RowStructure1.Insert("salary", 200.20);

DataArray.Add(RowStructure2);
DataArray.Add(RowStructure1);

KeyFields = New Array;
KeyFields.Add("id");

Result = OPI_SQLite.EnsureRecords(Table, DataArray, KeyFields, , Base);
# JSON data can also be passed as a path to a .json file

oint sqlite EnsureRecords \
--table "test_merge" \
--rows "[{'id':'1','name':'Vitaly Updated','age':'25','salary':'1500.5'},{'id':'3','name':'Anton','age':'30','salary':'3000'}]" \
--unique "['id']" \
--db "/tmp/v3fibjxn.cs0.sqlite"