Skip to main content

insertInto

Inserts the content of the DataFrame into the specified table. Requires that the schema of the DataFrame is the same as the schema of the table.

Syntax

insertInto(tableName, overwrite=None)

Parameters

Parameter

Type

Description

tableName

str

The name of the target table.

overwrite

bool, optional

If True, overwrites existing data. Disabled by default.

Returns

None

Notes

Unlike DataFrameWriter.saveAsTable, DataFrameWriter.insertInto ignores column names and uses position-based resolution.

Examples

Insert data into a table using position-based resolution, ignoring column names.

Python
spark.sql("DROP TABLE IF EXISTS tblA")
df = spark.createDataFrame([
(100, "Alice"), (120, "Alice"), (140, "Bob")],
schema=["age", "name"]
)
df.write.saveAsTable("tblA")

df.selectExpr("age AS col1", "name AS col2").write.insertInto("tblA")
spark.read.table("tblA").sort("age").show()
# +---+------------+
# |age| name|
# +---+------------+
# |100|Alice|
# |100|Alice|
# |120|Alice|
# |120|Alice|
# |140| Bob|
# |140| Bob|
# +---+------------+

spark.sql("DROP TABLE tblA")