Chapter4. More about Insert

Except insert_record(), Sqlite3Engine also provides:

Before you proceed, first you should understand the Row Proxy, which offers you the ability to visit it’s value and define database row data like a Python dict.

Once you get understand Row Proxy, let’s see more features about Insert.

Insert a Row object

Insert one Row object at a time. If conflict with constrain, raise Errors.

... define a table = Table(...)

ins = table.insert()
row = Row.from_dict({
    "#column1": value1
    "#column2": value2
    ...
})
engine.insert_row(ins, row)

Insert many record tuple

Insert list of records, automatically skip Exceptions.

ins = table.insert()
record_list = [(value1, value2, ...) for i in range(10)]
engine.insert_many_record(ins, record_list)

Insert many Row object

Insert list of Row objects, automatically skip Exceptions.

ins = table.insert()
row_list = [
    {
        "#column1": value1
        "#column2": value2
        ...
    } for i in range(10)
]
engine.insert_many_row(ins, row_list)

Insert many record tuple in a generator stream

Similar to Insert many record tuple, but work with a record tuple generator.

def record_generator()
    record_list = [(value1, value2, ...) for i in range(10)]
    for record in record_list:
        yield record

ins = table.insert()
engine.insert_many_row(ins, record_generator())

Insert many Row object in a generator stream

Similar to Insert many Row object, but work with a Raw object generator.

def row_generator()
    row_list = [
        {
            "#column1": value1
            "#column2": value2
            ...
        } for i in range(10)
    ]
    for row in row_list:
        yield row

ins = table.insert()
engine.insert_many_row(ins, row_generator())