Inserting Data
Use insert() to add a new row to a table. Pass the table name and a dictionary
where keys are column names and values are the data to insert.
db.insert(table, data) Parameters
| Parameter | Type | Description |
|---|---|---|
table | str | Name of the table |
data | dict | Column-value pairs to insert |
Example
from easymysql.mysql import mysql
db = mysql('localhost', 'root', 'secret', 'shop')
db.insert('products', {
'name': 'Laptop',
'price': 999.99,
'stock': 50,
})
This executes:
INSERT INTO products (name, price, stock) VALUES ('Laptop', 999.99, 50) Return value
insert() returns the lastrowid of the inserted row — the auto-increment
ID assigned by MySQL.
new_id = db.insert('products', {'name': 'Mouse', 'price': 29.99, 'stock': 200})
print(f"Inserted product with ID: {new_id}")