MySQL Shell API 8.0.38
Unified development interface for MySQL Products
Methods | List of all members
TableInsert Class Reference

Handler for Insert operations on Tables. More...

Methods

TableInsert insert ()
 Initializes the insertion operation. More...
 
TableInsert insert (List columns)
 Initializes the insertion operation. More...
 
TableInsert insert (String column[, String column,...])
 Initializes the insertion operation. More...
 
TableInsert insert (JSON columns)
 Initializes the insertion operation. More...
 
TableInsert values (Value value[, Value value,...])
 Adds a new row to the insert operation with the given values. More...
 
Result execute ()
 Executes the insert operation. More...
 

Detailed Description

Handler for Insert operations on Tables.

Member Function Documentation

◆ insert() [1/4]

TableInsert insert ( )

Initializes the insertion operation.

Returns
This TableInsert object.

Initializes the insertion operation, the arguments provided for the values(Value value[, Value value, ...]) method must match the database columns in number and data types.

Method Chaining

After this function invocation, the following function can be invoked:

See also
Usage examples at execute().

◆ insert() [2/4]

TableInsert insert ( List  columns)

Initializes the insertion operation.

Returns
This TableInsert object.

Initializes the insertion operation, the arguments provided for the values(Value value[, Value value, ...]) method must match the specified column names in order and data type.

Method Chaining

After this function invocation, the following function can be invoked:

See also
Usage examples at execute().

◆ insert() [3/4]

TableInsert insert ( String  column[, String column,...])

Initializes the insertion operation.

Returns
This TableInsert object.

Initializes the insertion operation, the arguments provided for the values(Value value[, Value value, ...]) method must match the specified column names in order and data type.

Method Chaining

After this function invocation, the following function can be invoked:

See also
Usage examples at execute().

◆ insert() [4/4]

TableInsert insert ( JSON  columns)

Initializes the insertion operation.

Returns
This TableInsert object.

Initializes the insertion operation, it is ready to be completed and it will insert the associated values into the corresponding columns.

Method Chaining

After this function invocation, the following function can be invoked:

See also
Usage examples at execute().

◆ values()

TableInsert values ( Value  value[, Value value,...])

Adds a new row to the insert operation with the given values.

Returns
This TableInsert object.

Each parameter represents the value for a column in the target table.

If the columns were defined on the insert() function, the number of values on this function must match the number of defined columns.

If no column was defined, the number of parameters must match the number of columns on the target Table.

This function is not available when the insert() is called passing a JSON object with columns and values.

Using Expressions As Values

If a mysqlx.expr(...) object is defined as a value, it will be evaluated in the server, the resulting value will be inserted into the record.

Method Chaining

This function can be invoked multiple times after:

See also
Usage examples at execute().

◆ execute()

Result execute ( )

Executes the insert operation.

Returns
A Result object that can be used to retrieve the results of the operation.

Method Chaining

This function can be invoked after:

Examples

Inserting values without specifying the column names

result = table.insert().values('jack', 17, 'male').execute();
print("Affected Rows No Columns:", result.affectedItemsCount, "\n");

The column names given as a list of strings

result = table.insert(['age', 'name', 'gender']).values(21, 'john', 'male').execute();
print("Affected Rows Columns:", result.affectedItemsCount, "\n");

The column names given as a sequence of strings

var insert = table.insert('name', 'age', 'gender')
var insert = insert.values('clark', 22, 'male')
var insert = insert.values('mary', 13, 'female')
result = insert.execute()
print("Affected Rows Multiple Values:", result.affectedItemsCount, "\n");

The column names and corresponding values given as a JSON document

result = table.insert({ 'age': 14, 'name': 'jackie', 'gender': 'female' }).execute();
print("Affected Rows Document:", result.affectedItemsCount, "\n");